blob: a819a63e5e2865411d3b3822bc172a59577c7949 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14**
drhde7446b2009-05-31 17:16:09 +000015** $Id: shell.c,v 1.210 2009/05/31 17:16:10 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
shane18e526c2008-12-10 22:30:24 +000017#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
drh75897232000-05-29 14:26:00 +000022#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
danielk19772a02e332004-06-05 08:04:36 +000025#include <assert.h>
drh1d482dd2004-05-31 18:23:07 +000026#include "sqlite3.h"
drh75897232000-05-29 14:26:00 +000027#include <ctype.h>
drhb0603412007-02-28 04:47:26 +000028#include <stdarg.h>
persicom7e2dfdd2002-04-18 02:46:52 +000029
drh454ad582007-11-26 22:54:27 +000030#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
drh4c504392000-10-16 22:06:40 +000031# include <signal.h>
chw97185482008-11-17 08:05:31 +000032# if !defined(__RTP__) && !defined(_WRS_KERNEL)
33# include <pwd.h>
34# endif
drhdd45df82002-04-18 12:39:03 +000035# include <unistd.h>
36# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000037#endif
drh75897232000-05-29 14:26:00 +000038
drhcdb36b72006-06-12 12:57:45 +000039#ifdef __OS2__
40# include <unistd.h>
41#endif
42
drh16e59552000-07-31 11:57:37 +000043#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000044# include <readline/readline.h>
45# include <readline/history.h>
46#else
drh9347b202003-07-18 01:30:59 +000047# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000048# define add_history(X)
drh67505e72002-04-19 12:34:06 +000049# define read_history(X)
50# define write_history(X)
51# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000052#endif
53
adamd2e8464a2006-09-06 21:39:40 +000054#if defined(_WIN32) || defined(WIN32)
55# include <io.h>
shane18e526c2008-12-10 22:30:24 +000056#define isatty(h) _isatty(h)
57#define access(f,m) _access((f),(m))
adamd2e8464a2006-09-06 21:39:40 +000058#else
drh4328c8b2003-04-26 02:50:11 +000059/* Make sure isatty() has a prototype.
60*/
61extern int isatty();
adamd2e8464a2006-09-06 21:39:40 +000062#endif
drh4328c8b2003-04-26 02:50:11 +000063
chw65d3c132007-11-12 21:09:10 +000064#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
chw97185482008-11-17 08:05:31 +000072#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
drh3b1a9882007-11-02 12:53:03 +000073#include <sys/time.h>
74#include <sys/resource.h>
75
drhda108222009-02-25 19:07:24 +000076/* Saved resource information for the beginning of an operation */
77static struct rusage sBegin;
78
79/* True if the timer is enabled */
80static int enableTimer = 0;
81
82/*
83** Begin timing an operation
84*/
85static void beginTimer(void){
86 if( enableTimer ){
87 getrusage(RUSAGE_SELF, &sBegin);
88 }
89}
90
91/* Return the difference of two time_structs in seconds */
92static 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*/
100static 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}
shaneb320ccd2009-10-21 03:42:58 +0000109
drhda108222009-02-25 19:07:24 +0000110#define BEGIN_TIMER beginTimer()
111#define END_TIMER endTimer()
112#define HAS_TIMER 1
shaneb320ccd2009-10-21 03:42:58 +0000113
114#elif (defined(_WIN32) || defined(WIN32))
115
116#include <windows.h>
117
118/* Saved resource information for the beginning of an operation */
119static HANDLE hProcess;
120static FILETIME ftKernelBegin;
121static FILETIME ftUserBegin;
122typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
123static GETPROCTIMES getProcessTimesAddr = NULL;
124
125/* True if the timer is enabled */
126static 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*/
132static 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*/
158static 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 */
166static 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*/
175static 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
drhda108222009-02-25 19:07:24 +0000189#else
190#define BEGIN_TIMER
191#define END_TIMER
192#define HAS_TIMER 0
193#endif
194
shanec0688ea2009-03-05 03:48:06 +0000195/*
196** Used to prevent warnings about unused parameters
197*/
198#define UNUSED_PARAMETER(x) (void)(x)
199
danielk1977c8c70692009-02-25 15:22:02 +0000200
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
210static int genfkey_create_triggers(sqlite3 *, const char *, void *,
211 int (*)(void *, int, const char *)
212);
213
214struct GenfkeyCb {
215 void *pCtx;
216 int eType;
217 int (*xData)(void *, int, const char *);
218};
219typedef 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
276typedef struct SchemaTable SchemaTable;
shane16f954c2009-10-21 13:53:58 +0000277static struct SchemaTable {
danielk1977c8c70692009-02-25 15:22:02 +0000278 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
291typedef struct schema_vtab schema_vtab;
292typedef struct schema_cursor schema_cursor;
293
294/* A schema table object */
295struct schema_vtab {
296 sqlite3_vtab base;
297 sqlite3 *db;
298 SchemaTable *pType;
299};
300
301/* A schema table cursor object */
302struct 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*/
313static int schemaDestroy(sqlite3_vtab *pVtab){
314 sqlite3_free(pVtab);
315 return 0;
316}
317
318/*
319** Table constructor for the schema module.
320*/
321static 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
shanec0688ea2009-03-05 03:48:06 +0000332 UNUSED_PARAMETER(pzErr);
danielk1977c8c70692009-02-25 15:22:02 +0000333 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*/
360static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
361 int rc = SQLITE_NOMEM;
362 schema_cursor *pCur;
shanec0688ea2009-03-05 03:48:06 +0000363 UNUSED_PARAMETER(pVTab);
danielk1977c8c70692009-02-25 15:22:02 +0000364 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*/
376static 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
385static 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*/
407static 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*/
426static 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
432static int finalize(sqlite3_stmt **ppStmt){
433 int rc = sqlite3_finalize(*ppStmt);
434 *ppStmt = 0;
435 return rc;
436}
437
438static 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*/
446static 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
510next_exit:
511 /* TODO: Handle rc */
512 return rc;
513}
514
515/*
516** Reset a schema table cursor.
517*/
518static 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;
shanec0688ea2009-03-05 03:48:06 +0000526 UNUSED_PARAMETER(idxNum);
527 UNUSED_PARAMETER(idxStr);
528 UNUSED_PARAMETER(argc);
529 UNUSED_PARAMETER(argv);
danielk1977c8c70692009-02-25 15:22:02 +0000530 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*/
541static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
shanec0688ea2009-03-05 03:48:06 +0000542 UNUSED_PARAMETER(tab);
543 UNUSED_PARAMETER(pIdxInfo);
danielk1977c8c70692009-02-25 15:22:02 +0000544 return SQLITE_OK;
545}
546
547/*
548** A virtual table module that merely echos method calls into TCL
549** variables.
550*/
551static 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*/
577static 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*/
605struct StrBuffer {
606 char *zBuf;
607};
608typedef struct StrBuffer StrBuffer;
609static 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}
615static void joinStep(
616 sqlite3_context *context,
617 int argc,
618 sqlite3_value **argv
619){
620 StrBuffer *p;
shanec0688ea2009-03-05 03:48:06 +0000621 UNUSED_PARAMETER(argc);
danielk1977c8c70692009-02-25 15:22:02 +0000622 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*/
644static 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
shanec0688ea2009-03-05 03:48:06 +0000655 UNUSED_PARAMETER(argc);
danielk1977c8c70692009-02-25 15:22:02 +0000656 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*/
675static 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 ){
drhde7446b2009-05-31 17:16:09 +0000703 char *zNew;
danielk19779365c672009-03-13 15:32:53 +0000704 nMalloc = 16 + (nOut+nCopy)*2;
drhde7446b2009-05-31 17:16:09 +0000705 zNew = (char*)sqlite3_realloc(zOut, nMalloc);
706 if( zNew==0 ){
707 sqlite3_result_error_nomem(context);
708 return;
709 }else{
710 zOut = zNew;
711 }
danielk1977c8c70692009-02-25 15:22:02 +0000712 }
danielk19779365c672009-03-13 15:32:53 +0000713 assert( nMalloc>=(nOut+nCopy) );
danielk1977c8c70692009-02-25 15:22:02 +0000714 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*/
727static int invokeCallback(void *p, int nArg, char **azArg, char **azCol){
728 GenfkeyCb *pCb = (GenfkeyCb *)p;
shanec0688ea2009-03-05 03:48:06 +0000729 UNUSED_PARAMETER(nArg);
730 UNUSED_PARAMETER(azCol);
danielk1977c8c70692009-02-25 15:22:02 +0000731 return pCb->xData(pCb->pCtx, pCb->eType, azArg[0]);
732}
733
shane16f954c2009-10-21 13:53:58 +0000734static int detectSchemaProblem(
danielk1977c8c70692009-02-25 15:22:02 +0000735 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*/
768static 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
879static 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
dan1da40a32009-09-19 17:00:31 +0000931 /* The "AFTER UPDATE ON <referenced>" trigger. This trigger's job
danielk1977c8c70692009-02-25 15:22:02 +0000932 ** 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
dan8b6d37d2009-10-08 13:42:28 +0000956 ", '/fkey_list/', sj(dq(to_col), ', ')"
957 ", '/rkey_list/', sj(dq(from_col), ', ')"
danielk1977c8c70692009-02-25 15:22:02 +0000958
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/;' "
dan8b6d37d2009-10-08 13:42:28 +0000971 ", '/setlist/', sj(dq(from_col)||' = NULL',', ')"
danielk1977c8c70692009-02-25 15:22:02 +0000972 ", '/tbl/', dq(from_tbl)"
dan8b6d37d2009-10-08 13:42:28 +0000973 ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
danielk1977c8c70692009-02-25 15:22:02 +0000974 ")"
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/;' "
dan8b6d37d2009-10-08 13:42:28 +0000988 ", '/setlist/', sj(dq(from_col)||' = NULL',', ')"
danielk1977c8c70692009-02-25 15:22:02 +0000989 ", '/tbl/', dq(from_tbl)"
dan8b6d37d2009-10-08 13:42:28 +0000990 ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
danielk1977c8c70692009-02-25 15:22:02 +0000991 ")"
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
shanec0688ea2009-03-05 03:48:06 +00001013 UNUSED_PARAMETER(zDb);
1014
danielk1977c8c70692009-02-25 15:22:02 +00001015 /* 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
1051genfkey_exit:
1052 sqlite3_close(db);
1053 return rc;
1054}
1055
1056
1057#endif
1058/* End genfkey logic. */
1059/*************************************************************************/
1060/*************************************************************************/
1061
drhe91d16b2008-12-08 18:27:31 +00001062/*
drhc49f44e2006-10-26 18:15:42 +00001063** If the following flag is set, then command execution stops
1064** at an error if we are not interactive.
1065*/
1066static int bail_on_error = 0;
1067
1068/*
drhc28490c2006-10-26 14:25:58 +00001069** Threat stdin as an interactive input if the following variable
1070** is true. Otherwise, assume stdin is connected to a file or pipe.
1071*/
1072static int stdin_is_interactive = 1;
1073
1074/*
drh4c504392000-10-16 22:06:40 +00001075** 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*/
danielk197792f9a1b2004-06-19 09:08:16 +00001079static sqlite3 *db = 0;
drh4c504392000-10-16 22:06:40 +00001080
1081/*
drh67505e72002-04-19 12:34:06 +00001082** True if an interrupt (Control-C) has been received.
1083*/
drh43617e92006-03-06 20:55:46 +00001084static volatile int seenInterrupt = 0;
drh67505e72002-04-19 12:34:06 +00001085
1086/*
persicom7e2dfdd2002-04-18 02:46:52 +00001087** 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*/
1090static char *Argv0;
1091
1092/*
1093** Prompt strings. Initialized in main. Settable with
1094** .prompt main continue
1095*/
1096static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
1097static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
1098
drhb0603412007-02-28 04:47:26 +00001099/*
1100** Write I/O traces to the following stream.
1101*/
rsebe0a9092007-07-30 18:24:38 +00001102#ifdef SQLITE_ENABLE_IOTRACE
drhb0603412007-02-28 04:47:26 +00001103static FILE *iotrace = 0;
rsebe0a9092007-07-30 18:24:38 +00001104#endif
drhb0603412007-02-28 04:47:26 +00001105
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*/
rsebe0a9092007-07-30 18:24:38 +00001112#ifdef SQLITE_ENABLE_IOTRACE
drhb0603412007-02-28 04:47:26 +00001113static void iotracePrintf(const char *zFormat, ...){
1114 va_list ap;
drhf075cd02007-02-28 06:14:25 +00001115 char *z;
drhb0603412007-02-28 04:47:26 +00001116 if( iotrace==0 ) return;
1117 va_start(ap, zFormat);
drhf075cd02007-02-28 06:14:25 +00001118 z = sqlite3_vmprintf(zFormat, ap);
drhb0603412007-02-28 04:47:26 +00001119 va_end(ap);
drhf075cd02007-02-28 06:14:25 +00001120 fprintf(iotrace, "%s", z);
1121 sqlite3_free(z);
drhb0603412007-02-28 04:47:26 +00001122}
rsebe0a9092007-07-30 18:24:38 +00001123#endif
drhb0603412007-02-28 04:47:26 +00001124
drh44c2eb12003-04-30 11:38:26 +00001125
persicom7e2dfdd2002-04-18 02:46:52 +00001126/*
drh83965662003-04-17 02:54:13 +00001127** Determines if a string is a number of not.
1128*/
danielk19772e588c72005-12-09 14:25:08 +00001129static int isNumber(const char *z, int *realnum){
drhc8d74412004-08-31 23:41:26 +00001130 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}
drh83965662003-04-17 02:54:13 +00001152
1153/*
danielk1977bc6ada42004-06-30 08:20:16 +00001154** 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*/
1161static const char *zShellStatic = 0;
1162static void shellstaticFunc(
1163 sqlite3_context *context,
1164 int argc,
1165 sqlite3_value **argv
1166){
1167 assert( 0==argc );
1168 assert( zShellStatic );
shaned87897d2009-01-30 05:40:27 +00001169 UNUSED_PARAMETER(argc);
drh902b9ee2008-12-05 17:17:07 +00001170 UNUSED_PARAMETER(argv);
danielk1977bc6ada42004-06-30 08:20:16 +00001171 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
1172}
1173
1174
1175/*
drhfeac5f82004-08-01 00:10:45 +00001176** This routine reads a line of text from FILE in, stores
drh8e7e7a22000-05-30 18:45:23 +00001177** 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*/
drh9347b202003-07-18 01:30:59 +00001184static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +00001185 char *zLine;
1186 int nLine;
drh8e7e7a22000-05-30 18:45:23 +00001187 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 }
drhdaffd0e2001-04-11 14:28:42 +00001205 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +00001206 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/*
drhc28490c2006-10-26 14:25:58 +00001226** Retrieve a single line of input text.
drh8e7e7a22000-05-30 18:45:23 +00001227**
1228** zPrior is a string of prior text retrieved. If not the empty
1229** string, then issue a continuation prompt.
1230*/
drhdaffd0e2001-04-11 14:28:42 +00001231static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +00001232 char *zPrompt;
1233 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +00001234 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +00001235 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +00001236 }
1237 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +00001238 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +00001239 }else{
persicom7e2dfdd2002-04-18 02:46:52 +00001240 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +00001241 }
1242 zResult = readline(zPrompt);
danielk19774af00c62005-01-23 23:43:21 +00001243#if defined(HAVE_READLINE) && HAVE_READLINE==1
drheb741d52006-06-03 17:37:25 +00001244 if( zResult && *zResult ) add_history(zResult);
danielk19774af00c62005-01-23 23:43:21 +00001245#endif
drh8e7e7a22000-05-30 18:45:23 +00001246 return zResult;
1247}
1248
persicom7e2dfdd2002-04-18 02:46:52 +00001249struct previous_mode_data {
1250 int valid; /* Is there legit data in here? */
1251 int mode;
1252 int showHeader;
1253 int colWidth[100];
1254};
drh45e29d82006-11-20 16:21:10 +00001255
drh8e7e7a22000-05-30 18:45:23 +00001256/*
drh75897232000-05-29 14:26:00 +00001257** 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*/
1261struct callback_data {
shane626a6e42009-10-22 17:30:15 +00001262 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +00001263 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +00001264 int cnt; /* Number of records displayed so far */
1265 FILE *out; /* Write results here */
1266 int mode; /* An output mode setting */
drh45e29d82006-11-20 16:21:10 +00001267 int writableSchema; /* True if PRAGMA writable_schema=ON */
drh28bd4bc2000-06-15 15:57:22 +00001268 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +00001269 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +00001270 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +00001271 int colWidth[100]; /* Requested width of each column when in column mode*/
1272 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +00001273 char nullvalue[20]; /* The text to print when a NULL comes back from
1274 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +00001275 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +00001276 /* Holds the mode information just before
1277 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +00001278 char outfile[FILENAME_MAX]; /* Filename for *out */
1279 const char *zDbFilename; /* name of the database file */
shane626a6e42009-10-22 17:30:15 +00001280 sqlite3_stmt *pStmt; /* Current statement if any. */
drh75897232000-05-29 14:26:00 +00001281};
1282
1283/*
1284** These are the allowed modes.
1285*/
drh967e8b72000-06-21 13:59:10 +00001286#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +00001287#define MODE_Column 1 /* One record per line in neat columns */
1288#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +00001289#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1290#define MODE_Html 4 /* Generate an XHTML table */
1291#define MODE_Insert 5 /* Generate SQL "insert" statements */
drhfeac5f82004-08-01 00:10:45 +00001292#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
drh8e64d1c2004-10-07 00:32:39 +00001293#define MODE_Csv 7 /* Quote strings, numbers are plain */
drh66ce4d02008-02-15 17:38:06 +00001294#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */
persicom7e2dfdd2002-04-18 02:46:52 +00001295
drh66ce4d02008-02-15 17:38:06 +00001296static const char *modeDescr[] = {
persicom7e2dfdd2002-04-18 02:46:52 +00001297 "line",
1298 "column",
1299 "list",
1300 "semi",
1301 "html",
drhfeac5f82004-08-01 00:10:45 +00001302 "insert",
1303 "tcl",
drh8e64d1c2004-10-07 00:32:39 +00001304 "csv",
drh66ce4d02008-02-15 17:38:06 +00001305 "explain",
persicom7e2dfdd2002-04-18 02:46:52 +00001306};
drh75897232000-05-29 14:26:00 +00001307
1308/*
1309** Number of elements in an array
1310*/
drh902b9ee2008-12-05 17:17:07 +00001311#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
drh75897232000-05-29 14:26:00 +00001312
1313/*
drhea678832008-12-10 19:26:22 +00001314** Compute a string length that is limited to what can be stored in
1315** lower 30 bits of a 32-bit signed integer.
1316*/
drh4f21c4a2008-12-10 22:15:00 +00001317static int strlen30(const char *z){
drhea678832008-12-10 19:26:22 +00001318 const char *z2 = z;
1319 while( *z2 ){ z2++; }
1320 return 0x3fffffff & (int)(z2 - z);
1321}
1322
1323/*
shane626a6e42009-10-22 17:30:15 +00001324** Output the given string as a hex-encoded blob (eg. X'1234' )
1325*/
1326static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1327 int i;
1328 char *zBlob = (char *)pBlob;
1329 fprintf(out,"X'");
1330 for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }
1331 fprintf(out,"'");
1332}
1333
1334/*
drh28bd4bc2000-06-15 15:57:22 +00001335** Output the given string as a quoted string using SQL quoting conventions.
1336*/
1337static void output_quoted_string(FILE *out, const char *z){
1338 int i;
1339 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +00001340 for(i=0; z[i]; i++){
1341 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +00001342 }
1343 if( nSingle==0 ){
1344 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +00001345 }else{
1346 fprintf(out,"'");
1347 while( *z ){
1348 for(i=0; z[i] && z[i]!='\''; i++){}
1349 if( i==0 ){
1350 fprintf(out,"''");
1351 z++;
1352 }else if( z[i]=='\'' ){
1353 fprintf(out,"%.*s''",i,z);
1354 z += i+1;
1355 }else{
drhcd7d2732002-02-26 23:24:26 +00001356 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +00001357 break;
1358 }
1359 }
drhcd7d2732002-02-26 23:24:26 +00001360 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +00001361 }
1362}
1363
1364/*
drhfeac5f82004-08-01 00:10:45 +00001365** Output the given string as a quoted according to C or TCL quoting rules.
1366*/
1367static void output_c_string(FILE *out, const char *z){
1368 unsigned int c;
1369 fputc('"', out);
1370 while( (c = *(z++))!=0 ){
1371 if( c=='\\' ){
1372 fputc(c, out);
1373 fputc(c, out);
1374 }else if( c=='\t' ){
1375 fputc('\\', out);
1376 fputc('t', out);
1377 }else if( c=='\n' ){
1378 fputc('\\', out);
1379 fputc('n', out);
1380 }else if( c=='\r' ){
1381 fputc('\\', out);
1382 fputc('r', out);
1383 }else if( !isprint(c) ){
drh0a8640d2005-08-30 20:12:02 +00001384 fprintf(out, "\\%03o", c&0xff);
drhfeac5f82004-08-01 00:10:45 +00001385 }else{
1386 fputc(c, out);
1387 }
1388 }
1389 fputc('"', out);
1390}
1391
1392/*
drhc08a4f12000-06-15 16:49:48 +00001393** Output the given string with characters that are special to
1394** HTML escaped.
1395*/
1396static void output_html_string(FILE *out, const char *z){
1397 int i;
1398 while( *z ){
shane43d9cb22009-10-21 14:11:48 +00001399 for(i=0; z[i]
1400 && z[i]!='<'
1401 && z[i]!='&'
1402 && z[i]!='>'
1403 && z[i]!='\"'
1404 && z[i]!='\'';
1405 i++){}
drhc08a4f12000-06-15 16:49:48 +00001406 if( i>0 ){
1407 fprintf(out,"%.*s",i,z);
1408 }
1409 if( z[i]=='<' ){
1410 fprintf(out,"&lt;");
1411 }else if( z[i]=='&' ){
1412 fprintf(out,"&amp;");
shane43d9cb22009-10-21 14:11:48 +00001413 }else if( z[i]=='>' ){
1414 fprintf(out,"&gt;");
1415 }else if( z[i]=='\"' ){
1416 fprintf(out,"&quot;");
1417 }else if( z[i]=='\'' ){
1418 fprintf(out,"&#39;");
drhc08a4f12000-06-15 16:49:48 +00001419 }else{
1420 break;
1421 }
1422 z += i + 1;
1423 }
1424}
1425
1426/*
drhc49f44e2006-10-26 18:15:42 +00001427** If a field contains any character identified by a 1 in the following
1428** array, then the string must be quoted for CSV.
1429*/
1430static const char needCsvQuote[] = {
1431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1432 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1433 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1434 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1435 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1437 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1439 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1440 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1441 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1442 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1443 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1444 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1445 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1446 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1447};
1448
1449/*
drh8e64d1c2004-10-07 00:32:39 +00001450** Output a single term of CSV. Actually, p->separator is used for
1451** the separator, which may or may not be a comma. p->nullvalue is
1452** the null value. Strings are quoted using ANSI-C rules. Numbers
1453** appear outside of quotes.
1454*/
1455static void output_csv(struct callback_data *p, const char *z, int bSep){
drhc49f44e2006-10-26 18:15:42 +00001456 FILE *out = p->out;
drh8e64d1c2004-10-07 00:32:39 +00001457 if( z==0 ){
drhc49f44e2006-10-26 18:15:42 +00001458 fprintf(out,"%s",p->nullvalue);
drh8e64d1c2004-10-07 00:32:39 +00001459 }else{
drhc49f44e2006-10-26 18:15:42 +00001460 int i;
drh4f21c4a2008-12-10 22:15:00 +00001461 int nSep = strlen30(p->separator);
drhc49f44e2006-10-26 18:15:42 +00001462 for(i=0; z[i]; i++){
drhc85375d2007-12-18 15:41:44 +00001463 if( needCsvQuote[((unsigned char*)z)[i]]
1464 || (z[i]==p->separator[0] &&
1465 (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
drhc49f44e2006-10-26 18:15:42 +00001466 i = 0;
1467 break;
1468 }
1469 }
1470 if( i==0 ){
1471 putc('"', out);
1472 for(i=0; z[i]; i++){
1473 if( z[i]=='"' ) putc('"', out);
1474 putc(z[i], out);
1475 }
1476 putc('"', out);
1477 }else{
1478 fprintf(out, "%s", z);
1479 }
drh8e64d1c2004-10-07 00:32:39 +00001480 }
1481 if( bSep ){
drhd0e77882008-01-14 15:20:08 +00001482 fprintf(p->out, "%s", p->separator);
drh8e64d1c2004-10-07 00:32:39 +00001483 }
1484}
1485
danielk19774af00c62005-01-23 23:43:21 +00001486#ifdef SIGINT
drh8e64d1c2004-10-07 00:32:39 +00001487/*
drh4c504392000-10-16 22:06:40 +00001488** This routine runs when the user presses Ctrl-C
1489*/
1490static void interrupt_handler(int NotUsed){
drh902b9ee2008-12-05 17:17:07 +00001491 UNUSED_PARAMETER(NotUsed);
drh67505e72002-04-19 12:34:06 +00001492 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +00001493 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +00001494}
danielk19774af00c62005-01-23 23:43:21 +00001495#endif
drh4c504392000-10-16 22:06:40 +00001496
1497/*
shane626a6e42009-10-22 17:30:15 +00001498** This is the callback routine that the shell
drh75897232000-05-29 14:26:00 +00001499** invokes for each row of a query result.
1500*/
shane626a6e42009-10-22 17:30:15 +00001501static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
drh75897232000-05-29 14:26:00 +00001502 int i;
1503 struct callback_data *p = (struct callback_data*)pArg;
1504 switch( p->mode ){
1505 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +00001506 int w = 5;
drh6a535342001-10-19 16:44:56 +00001507 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +00001508 for(i=0; i<nArg; i++){
drh4f21c4a2008-12-10 22:15:00 +00001509 int len = strlen30(azCol[i] ? azCol[i] : "");
drhe3710332000-09-29 13:30:53 +00001510 if( len>w ) w = len;
1511 }
drh75897232000-05-29 14:26:00 +00001512 if( p->cnt++>0 ) fprintf(p->out,"\n");
1513 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +00001514 fprintf(p->out,"%*s = %s\n", w, azCol[i],
drha69d9162003-04-17 22:57:53 +00001515 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +00001516 }
1517 break;
1518 }
danielk19770d78bae2008-01-03 07:09:48 +00001519 case MODE_Explain:
drh75897232000-05-29 14:26:00 +00001520 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +00001521 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +00001522 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +00001523 int w, n;
1524 if( i<ArraySize(p->colWidth) ){
danielk19770d78bae2008-01-03 07:09:48 +00001525 w = p->colWidth[i];
drh75897232000-05-29 14:26:00 +00001526 }else{
danielk19770d78bae2008-01-03 07:09:48 +00001527 w = 0;
drh75897232000-05-29 14:26:00 +00001528 }
drha0c66f52000-07-29 13:20:21 +00001529 if( w<=0 ){
drh4f21c4a2008-12-10 22:15:00 +00001530 w = strlen30(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +00001531 if( w<10 ) w = 10;
drh4f21c4a2008-12-10 22:15:00 +00001532 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +00001533 if( w<n ) w = n;
1534 }
1535 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +00001536 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +00001537 }
1538 if( p->showHeader ){
1539 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
1540 }
1541 }
1542 if( p->showHeader ){
1543 for(i=0; i<nArg; i++){
1544 int w;
1545 if( i<ArraySize(p->actualWidth) ){
1546 w = p->actualWidth[i];
1547 }else{
1548 w = 10;
1549 }
1550 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
1551 "----------------------------------------------------------",
1552 i==nArg-1 ? "\n": " ");
1553 }
drh75897232000-05-29 14:26:00 +00001554 }
1555 }
drh6a535342001-10-19 16:44:56 +00001556 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +00001557 for(i=0; i<nArg; i++){
1558 int w;
drha0c66f52000-07-29 13:20:21 +00001559 if( i<ArraySize(p->actualWidth) ){
1560 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +00001561 }else{
1562 w = 10;
1563 }
drhea678832008-12-10 19:26:22 +00001564 if( p->mode==MODE_Explain && azArg[i] &&
drh4f21c4a2008-12-10 22:15:00 +00001565 strlen30(azArg[i])>w ){
1566 w = strlen30(azArg[i]);
danielk19770d78bae2008-01-03 07:09:48 +00001567 }
drhc61053b2000-06-04 12:58:36 +00001568 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +00001569 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +00001570 }
1571 break;
1572 }
drhe3710332000-09-29 13:30:53 +00001573 case MODE_Semi:
drh75897232000-05-29 14:26:00 +00001574 case MODE_List: {
1575 if( p->cnt++==0 && p->showHeader ){
1576 for(i=0; i<nArg; i++){
1577 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
1578 }
1579 }
drh6a535342001-10-19 16:44:56 +00001580 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +00001581 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +00001582 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +00001583 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +00001584 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +00001585 if( i<nArg-1 ){
1586 fprintf(p->out, "%s", p->separator);
1587 }else if( p->mode==MODE_Semi ){
1588 fprintf(p->out, ";\n");
1589 }else{
1590 fprintf(p->out, "\n");
1591 }
drh75897232000-05-29 14:26:00 +00001592 }
1593 break;
1594 }
drh1e5d0e92000-05-31 23:33:17 +00001595 case MODE_Html: {
1596 if( p->cnt++==0 && p->showHeader ){
mihailim57c591a2008-06-23 21:26:05 +00001597 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +00001598 for(i=0; i<nArg; i++){
shane43d9cb22009-10-21 14:11:48 +00001599 fprintf(p->out,"<TH>");
1600 output_html_string(p->out, azCol[i]);
1601 fprintf(p->out,"</TH>\n");
drh1e5d0e92000-05-31 23:33:17 +00001602 }
mihailim57c591a2008-06-23 21:26:05 +00001603 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +00001604 }
drh6a535342001-10-19 16:44:56 +00001605 if( azArg==0 ) break;
mihailim57c591a2008-06-23 21:26:05 +00001606 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +00001607 for(i=0; i<nArg; i++){
mihailim57c591a2008-06-23 21:26:05 +00001608 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +00001609 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
mihailim57c591a2008-06-23 21:26:05 +00001610 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +00001611 }
mihailim57c591a2008-06-23 21:26:05 +00001612 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +00001613 break;
1614 }
drhfeac5f82004-08-01 00:10:45 +00001615 case MODE_Tcl: {
1616 if( p->cnt++==0 && p->showHeader ){
1617 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +00001618 output_c_string(p->out,azCol[i] ? azCol[i] : "");
drhfeac5f82004-08-01 00:10:45 +00001619 fprintf(p->out, "%s", p->separator);
1620 }
1621 fprintf(p->out,"\n");
1622 }
1623 if( azArg==0 ) break;
1624 for(i=0; i<nArg; i++){
1625 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
1626 fprintf(p->out, "%s", p->separator);
1627 }
1628 fprintf(p->out,"\n");
1629 break;
1630 }
drh8e64d1c2004-10-07 00:32:39 +00001631 case MODE_Csv: {
1632 if( p->cnt++==0 && p->showHeader ){
1633 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +00001634 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
drh8e64d1c2004-10-07 00:32:39 +00001635 }
1636 fprintf(p->out,"\n");
1637 }
1638 if( azArg==0 ) break;
1639 for(i=0; i<nArg; i++){
1640 output_csv(p, azArg[i], i<nArg-1);
1641 }
1642 fprintf(p->out,"\n");
1643 break;
1644 }
drh28bd4bc2000-06-15 15:57:22 +00001645 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +00001646 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +00001647 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +00001648 for(i=0; i<nArg; i++){
1649 char *zSep = i>0 ? ",": "";
shanead6b8d02009-10-22 18:12:58 +00001650 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
drh28bd4bc2000-06-15 15:57:22 +00001651 fprintf(p->out,"%sNULL",zSep);
shanead6b8d02009-10-22 18:12:58 +00001652 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1653 if( zSep[0] ) fprintf(p->out,"%s",zSep);
1654 output_quoted_string(p->out, azArg[i]);
1655 }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){
1656 fprintf(p->out,"%s%s",zSep, azArg[i]);
shane626a6e42009-10-22 17:30:15 +00001657 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1658 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1659 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1660 if( zSep[0] ) fprintf(p->out,"%s",zSep);
1661 output_hex_blob(p->out, pBlob, nBlob);
drhc8d74412004-08-31 23:41:26 +00001662 }else if( isNumber(azArg[i], 0) ){
drh28bd4bc2000-06-15 15:57:22 +00001663 fprintf(p->out,"%s%s",zSep, azArg[i]);
1664 }else{
1665 if( zSep[0] ) fprintf(p->out,"%s",zSep);
1666 output_quoted_string(p->out, azArg[i]);
1667 }
1668 }
1669 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +00001670 break;
drh28bd4bc2000-06-15 15:57:22 +00001671 }
persicom1d0b8722002-04-18 02:53:04 +00001672 }
drh75897232000-05-29 14:26:00 +00001673 return 0;
1674}
1675
1676/*
shane626a6e42009-10-22 17:30:15 +00001677** This is the callback routine that the SQLite library
1678** invokes for each row of a query result.
1679*/
1680static int callback(void *pArg, int nArg, char **azArg, char **azCol){
1681 /* since we don't have type info, call the shell_callback with a NULL value */
1682 return shell_callback(pArg, nArg, azArg, azCol, NULL);
1683}
1684
1685/*
drh33048c02001-10-01 14:29:22 +00001686** Set the destination table field of the callback_data structure to
1687** the name of the table given. Escape any quote characters in the
1688** table name.
1689*/
1690static void set_table_name(struct callback_data *p, const char *zName){
1691 int i, n;
1692 int needQuote;
1693 char *z;
1694
1695 if( p->zDestTable ){
1696 free(p->zDestTable);
1697 p->zDestTable = 0;
1698 }
1699 if( zName==0 ) return;
drh4c755c02004-08-08 20:22:17 +00001700 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
drh33048c02001-10-01 14:29:22 +00001701 for(i=n=0; zName[i]; i++, n++){
drh4c755c02004-08-08 20:22:17 +00001702 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
drh33048c02001-10-01 14:29:22 +00001703 needQuote = 1;
1704 if( zName[i]=='\'' ) n++;
1705 }
1706 }
1707 if( needQuote ) n += 2;
1708 z = p->zDestTable = malloc( n+1 );
1709 if( z==0 ){
1710 fprintf(stderr,"Out of memory!\n");
1711 exit(1);
1712 }
1713 n = 0;
1714 if( needQuote ) z[n++] = '\'';
1715 for(i=0; zName[i]; i++){
1716 z[n++] = zName[i];
1717 if( zName[i]=='\'' ) z[n++] = '\'';
1718 }
1719 if( needQuote ) z[n++] = '\'';
1720 z[n] = 0;
1721}
1722
danielk19772a02e332004-06-05 08:04:36 +00001723/* zIn is either a pointer to a NULL-terminated string in memory obtained
1724** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1725** added to zIn, and the result returned in memory obtained from malloc().
1726** zIn, if it was not NULL, is freed.
1727**
1728** If the third argument, quote, is not '\0', then it is used as a
1729** quote character for zAppend.
1730*/
drhc28490c2006-10-26 14:25:58 +00001731static char *appendText(char *zIn, char const *zAppend, char quote){
danielk19772a02e332004-06-05 08:04:36 +00001732 int len;
1733 int i;
drh4f21c4a2008-12-10 22:15:00 +00001734 int nAppend = strlen30(zAppend);
1735 int nIn = (zIn?strlen30(zIn):0);
danielk19772a02e332004-06-05 08:04:36 +00001736
1737 len = nAppend+nIn+1;
1738 if( quote ){
1739 len += 2;
1740 for(i=0; i<nAppend; i++){
1741 if( zAppend[i]==quote ) len++;
1742 }
1743 }
1744
1745 zIn = (char *)realloc(zIn, len);
1746 if( !zIn ){
1747 return 0;
1748 }
1749
1750 if( quote ){
1751 char *zCsr = &zIn[nIn];
1752 *zCsr++ = quote;
1753 for(i=0; i<nAppend; i++){
1754 *zCsr++ = zAppend[i];
1755 if( zAppend[i]==quote ) *zCsr++ = quote;
1756 }
1757 *zCsr++ = quote;
1758 *zCsr++ = '\0';
1759 assert( (zCsr-zIn)==len );
1760 }else{
1761 memcpy(&zIn[nIn], zAppend, nAppend);
1762 zIn[len-1] = '\0';
1763 }
1764
1765 return zIn;
1766}
1767
drhdd3d4592004-08-30 01:54:05 +00001768
1769/*
1770** Execute a query statement that has a single result column. Print
1771** that result column on a line by itself with a semicolon terminator.
drh45e29d82006-11-20 16:21:10 +00001772**
1773** This is used, for example, to show the schema of the database by
1774** querying the SQLITE_MASTER table.
drhdd3d4592004-08-30 01:54:05 +00001775*/
drh157e29a2009-05-21 15:15:00 +00001776static int run_table_dump_query(
1777 FILE *out, /* Send output here */
1778 sqlite3 *db, /* Database to query */
1779 const char *zSelect, /* SELECT statement to extract content */
1780 const char *zFirstRow /* Print before first row, if not NULL */
1781){
drhdd3d4592004-08-30 01:54:05 +00001782 sqlite3_stmt *pSelect;
1783 int rc;
1784 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
1785 if( rc!=SQLITE_OK || !pSelect ){
1786 return rc;
1787 }
1788 rc = sqlite3_step(pSelect);
1789 while( rc==SQLITE_ROW ){
drh157e29a2009-05-21 15:15:00 +00001790 if( zFirstRow ){
1791 fprintf(out, "%s", zFirstRow);
1792 zFirstRow = 0;
1793 }
drhdd3d4592004-08-30 01:54:05 +00001794 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
1795 rc = sqlite3_step(pSelect);
1796 }
1797 return sqlite3_finalize(pSelect);
1798}
1799
shane626a6e42009-10-22 17:30:15 +00001800/*
1801** Allocate space and save off current error string.
1802*/
1803static char *save_err_msg(
1804 sqlite3 *db /* Database to query */
1805){
1806 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
1807 char *zErrMsg = sqlite3_malloc(nErrMsg);
1808 if( zErrMsg ){
1809 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
1810 }
1811 return zErrMsg;
1812}
1813
1814/*
1815** Execute a statement or set of statements. Print
1816** any result rows/columns depending on the current mode
1817** set via the supplied callback.
1818**
1819** This is very similar to SQLite's built-in sqlite3_exec()
1820** function except it takes a slightly different callback
1821** and callback data argument.
1822*/
1823static int shell_exec(
1824 sqlite3 *db, /* An open database */
1825 const char *zSql, /* SQL to be evaluated */
1826 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1827 /* (not the same as sqlite3_exec) */
1828 struct callback_data *pArg, /* Pointer to struct callback_data */
1829 char **pzErrMsg /* Error msg written here */
1830){
1831 sqlite3_stmt *pStmt = NULL;
1832 int rc, rc2;
1833
1834 if( pzErrMsg ){
1835 *pzErrMsg = NULL;
1836 }
1837
shanead6b8d02009-10-22 18:12:58 +00001838 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
shane626a6e42009-10-22 17:30:15 +00001839 if( (SQLITE_OK != rc) || !pStmt ){
1840 if( pzErrMsg ){
1841 *pzErrMsg = save_err_msg(db);
1842 }
1843 }else{
1844 /* perform the first step. this will tell us if we
1845 ** have a result set or not and how wide it is.
1846 */
1847 rc = sqlite3_step(pStmt);
1848 /* if we have a result set... */
1849 if( SQLITE_ROW == rc ){
1850 /* if callback... */
1851 if( xCallback ){
1852 /* allocate space for col name ptr, value ptr, and type */
1853 int nCol = sqlite3_column_count(pStmt);
1854 void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
1855 if( !pData ){
1856 rc = SQLITE_NOMEM;
1857 }else{
1858 char **azCols = (char **)pData; /* Names of result columns */
1859 char **azVals = &azCols[nCol]; /* Results */
1860 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
1861 int i;
1862 assert(sizeof(int) <= sizeof(char *));
1863 /* save off ptrs to column names */
1864 for(i=0; i<nCol; i++){
1865 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
1866 }
1867 /* save off the prepared statment handle */
1868 if( pArg ){
1869 pArg->pStmt = pStmt;
1870 }
1871 do{
1872 /* extract the data and data types */
1873 for(i=0; i<nCol; i++){
1874 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
1875 aiTypes[i] = sqlite3_column_type(pStmt, i);
1876 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
1877 rc = SQLITE_NOMEM;
1878 break; /* from for */
1879 }
1880 } /* end for */
1881
1882 /* if data and types extracted successfully... */
1883 if( SQLITE_ROW == rc ){
1884 /* call the supplied callback with the result row data */
1885 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
1886 rc = SQLITE_ABORT;
1887 }else{
1888 rc = sqlite3_step(pStmt);
1889 }
1890 }
1891 } while( SQLITE_ROW == rc );
1892 sqlite3_free(pData);
1893 if( pArg ){
1894 pArg->pStmt = NULL;
1895 }
1896 }
1897 }else{
1898 do{
1899 rc = sqlite3_step(pStmt);
1900 } while( rc == SQLITE_ROW );
1901 }
1902 }
1903
1904 /* if the last sqlite3_step() didn't complete successfully... */
1905 if( (SQLITE_OK != rc) && (SQLITE_DONE != rc) ){
1906 if( pzErrMsg ){
1907 *pzErrMsg = save_err_msg(db);
1908 }
1909 }else{
1910 rc = SQLITE_OK;
1911 }
1912
1913 rc2 = sqlite3_finalize(pStmt);
1914 /* if the last sqlite3_finalize() didn't complete successfully
1915 ** AND we don't have a save error from sqlite3_step ... */
1916 if( (SQLITE_OK != rc2) && (SQLITE_OK == rc) ){
1917 rc = rc2;
1918 if( pzErrMsg ){
1919 *pzErrMsg = save_err_msg(db);
1920 }
1921 }
1922 }
1923
1924 return rc;
1925}
1926
drhdd3d4592004-08-30 01:54:05 +00001927
drh33048c02001-10-01 14:29:22 +00001928/*
drh4c653a02000-06-07 01:27:47 +00001929** This is a different callback routine used for dumping the database.
1930** Each row received by this callback consists of a table name,
1931** the table type ("index" or "table") and SQL to create the table.
1932** This routine should print text sufficient to recreate the table.
1933*/
1934static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +00001935 int rc;
1936 const char *zTable;
1937 const char *zType;
1938 const char *zSql;
drh157e29a2009-05-21 15:15:00 +00001939 const char *zPrepStmt = 0;
drhdaffd0e2001-04-11 14:28:42 +00001940 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +00001941
drh902b9ee2008-12-05 17:17:07 +00001942 UNUSED_PARAMETER(azCol);
drh4c653a02000-06-07 01:27:47 +00001943 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +00001944 zTable = azArg[0];
1945 zType = azArg[1];
1946 zSql = azArg[2];
1947
drh00b950d2005-09-11 02:03:03 +00001948 if( strcmp(zTable, "sqlite_sequence")==0 ){
drh157e29a2009-05-21 15:15:00 +00001949 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
drh00b950d2005-09-11 02:03:03 +00001950 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1951 fprintf(p->out, "ANALYZE sqlite_master;\n");
1952 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1953 return 0;
drh45e29d82006-11-20 16:21:10 +00001954 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1955 char *zIns;
1956 if( !p->writableSchema ){
1957 fprintf(p->out, "PRAGMA writable_schema=ON;\n");
1958 p->writableSchema = 1;
1959 }
1960 zIns = sqlite3_mprintf(
1961 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
1962 "VALUES('table','%q','%q',0,'%q');",
1963 zTable, zTable, zSql);
1964 fprintf(p->out, "%s\n", zIns);
1965 sqlite3_free(zIns);
1966 return 0;
drh00b950d2005-09-11 02:03:03 +00001967 }else{
1968 fprintf(p->out, "%s;\n", zSql);
drhf8eb96a2005-02-03 00:42:34 +00001969 }
danielk19772a02e332004-06-05 08:04:36 +00001970
1971 if( strcmp(zType, "table")==0 ){
1972 sqlite3_stmt *pTableInfo = 0;
danielk19772a02e332004-06-05 08:04:36 +00001973 char *zSelect = 0;
1974 char *zTableInfo = 0;
1975 char *zTmp = 0;
drh157e29a2009-05-21 15:15:00 +00001976 int nRow = 0;
danielk19772a02e332004-06-05 08:04:36 +00001977
1978 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
1979 zTableInfo = appendText(zTableInfo, zTable, '"');
1980 zTableInfo = appendText(zTableInfo, ");", 0);
1981
1982 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
drh157e29a2009-05-21 15:15:00 +00001983 free(zTableInfo);
danielk19772a02e332004-06-05 08:04:36 +00001984 if( rc!=SQLITE_OK || !pTableInfo ){
1985 return 1;
1986 }
1987
1988 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
1989 zTmp = appendText(zTmp, zTable, '"');
1990 if( zTmp ){
1991 zSelect = appendText(zSelect, zTmp, '\'');
1992 }
1993 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
1994 rc = sqlite3_step(pTableInfo);
1995 while( rc==SQLITE_ROW ){
danielk19772e588c72005-12-09 14:25:08 +00001996 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
danielk19773f41e972004-06-08 00:39:01 +00001997 zSelect = appendText(zSelect, "quote(", 0);
danielk19772e588c72005-12-09 14:25:08 +00001998 zSelect = appendText(zSelect, zText, '"');
danielk19772a02e332004-06-05 08:04:36 +00001999 rc = sqlite3_step(pTableInfo);
2000 if( rc==SQLITE_ROW ){
drh45e29d82006-11-20 16:21:10 +00002001 zSelect = appendText(zSelect, ") || ',' || ", 0);
danielk19772a02e332004-06-05 08:04:36 +00002002 }else{
2003 zSelect = appendText(zSelect, ") ", 0);
2004 }
drh157e29a2009-05-21 15:15:00 +00002005 nRow++;
danielk19772a02e332004-06-05 08:04:36 +00002006 }
2007 rc = sqlite3_finalize(pTableInfo);
drh157e29a2009-05-21 15:15:00 +00002008 if( rc!=SQLITE_OK || nRow==0 ){
2009 free(zSelect);
danielk19772a02e332004-06-05 08:04:36 +00002010 return 1;
2011 }
2012 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
2013 zSelect = appendText(zSelect, zTable, '"');
2014
drh157e29a2009-05-21 15:15:00 +00002015 rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt);
drhdd3d4592004-08-30 01:54:05 +00002016 if( rc==SQLITE_CORRUPT ){
2017 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
drh157e29a2009-05-21 15:15:00 +00002018 rc = run_table_dump_query(p->out, p->db, zSelect, 0);
drhdd3d4592004-08-30 01:54:05 +00002019 }
danielk19772a02e332004-06-05 08:04:36 +00002020 if( zSelect ) free(zSelect);
drh4c653a02000-06-07 01:27:47 +00002021 }
drh4c653a02000-06-07 01:27:47 +00002022 return 0;
2023}
2024
2025/*
drh45e29d82006-11-20 16:21:10 +00002026** Run zQuery. Use dump_callback() as the callback routine so that
2027** the contents of the query are output as SQL statements.
2028**
drhdd3d4592004-08-30 01:54:05 +00002029** If we get a SQLITE_CORRUPT error, rerun the query after appending
2030** "ORDER BY rowid DESC" to the end.
2031*/
2032static int run_schema_dump_query(
2033 struct callback_data *p,
2034 const char *zQuery,
2035 char **pzErrMsg
2036){
2037 int rc;
2038 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
2039 if( rc==SQLITE_CORRUPT ){
2040 char *zQ2;
drh4f21c4a2008-12-10 22:15:00 +00002041 int len = strlen30(zQuery);
drhdd3d4592004-08-30 01:54:05 +00002042 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
2043 zQ2 = malloc( len+100 );
2044 if( zQ2==0 ) return rc;
drh5bb3eb92007-05-04 13:15:55 +00002045 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
drhdd3d4592004-08-30 01:54:05 +00002046 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
2047 free(zQ2);
2048 }
2049 return rc;
2050}
2051
danielk1977c8c70692009-02-25 15:22:02 +00002052#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
2053struct GenfkeyCmd {
2054 sqlite3 *db; /* Database handle */
2055 struct callback_data *pCb; /* Callback data */
2056 int isIgnoreErrors; /* True for --ignore-errors */
2057 int isExec; /* True for --exec */
2058 int isNoDrop; /* True for --no-drop */
2059 int nErr; /* Number of errors seen so far */
2060};
2061typedef struct GenfkeyCmd GenfkeyCmd;
2062
2063static int genfkeyParseArgs(GenfkeyCmd *p, char **azArg, int nArg){
2064 int ii;
2065 memset(p, 0, sizeof(GenfkeyCmd));
2066
2067 for(ii=0; ii<nArg; ii++){
drh93a989c2009-03-16 10:59:44 +00002068 int n = strlen30(azArg[ii]);
danielk1977c8c70692009-02-25 15:22:02 +00002069
2070 if( n>2 && n<10 && 0==strncmp(azArg[ii], "--no-drop", n) ){
2071 p->isNoDrop = 1;
2072 }else if( n>2 && n<16 && 0==strncmp(azArg[ii], "--ignore-errors", n) ){
2073 p->isIgnoreErrors = 1;
2074 }else if( n>2 && n<7 && 0==strncmp(azArg[ii], "--exec", n) ){
2075 p->isExec = 1;
2076 }else{
2077 fprintf(stderr, "unknown option: %s\n", azArg[ii]);
2078 return -1;
2079 }
2080 }
2081
2082 return SQLITE_OK;
2083}
2084
2085static int genfkeyCmdCb(void *pCtx, int eType, const char *z){
2086 GenfkeyCmd *p = (GenfkeyCmd *)pCtx;
2087 if( eType==GENFKEY_ERROR && !p->isIgnoreErrors ){
2088 p->nErr++;
2089 fprintf(stderr, "%s\n", z);
2090 }
2091
2092 if( p->nErr==0 && (
2093 (eType==GENFKEY_CREATETRIGGER)
2094 || (eType==GENFKEY_DROPTRIGGER && !p->isNoDrop)
2095 )){
2096 if( p->isExec ){
2097 sqlite3_exec(p->db, z, 0, 0, 0);
2098 }else{
2099 char *zCol = "sql";
2100 callback((void *)p->pCb, 1, (char **)&z, (char **)&zCol);
2101 }
2102 }
2103
2104 return SQLITE_OK;
2105}
2106#endif
2107
drhdd3d4592004-08-30 01:54:05 +00002108/*
drh75897232000-05-29 14:26:00 +00002109** Text of a help message
2110*/
persicom1d0b8722002-04-18 02:53:04 +00002111static char zHelp[] =
drh9ff849f2009-02-04 20:55:57 +00002112 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
drh20f99c42007-01-08 14:31:35 +00002113 ".bail ON|OFF Stop after hitting an error. Default OFF\n"
jplyon6a65bb32003-05-04 07:25:57 +00002114 ".databases List names and files of attached databases\n"
drhb860bc92004-08-04 15:16:55 +00002115 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
drhdaffd0e2001-04-11 14:28:42 +00002116 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +00002117 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +00002118 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
danielk1977c8c70692009-02-25 15:22:02 +00002119#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
2120 ".genfkey ?OPTIONS? Options are:\n"
2121 " --no-drop: Do not drop old fkey triggers.\n"
2122 " --ignore-errors: Ignore tables with fkey errors\n"
2123 " --exec: Execute generated SQL immediately\n"
danielk1977e6320042009-02-25 15:43:57 +00002124 " See file tool/genfkey.README in the source \n"
2125 " distribution for further information.\n"
danielk1977c8c70692009-02-25 15:22:02 +00002126#endif
persicom7e2dfdd2002-04-18 02:46:52 +00002127 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +00002128 ".help Show this message\n"
drhb860bc92004-08-04 15:16:55 +00002129 ".import FILE TABLE Import data from FILE into TABLE\n"
drh75897232000-05-29 14:26:00 +00002130 ".indices TABLE Show names of all indices on TABLE\n"
drhae5e4452007-05-03 17:18:36 +00002131#ifdef SQLITE_ENABLE_IOTRACE
2132 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
2133#endif
drh70df4fe2006-06-13 15:12:21 +00002134#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh1e397f82006-06-08 15:28:43 +00002135 ".load FILE ?ENTRY? Load an extension library\n"
drh70df4fe2006-06-13 15:12:21 +00002136#endif
danielk19776b77a362005-01-13 11:10:25 +00002137 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
drh3b584fa2004-09-24 12:50:03 +00002138 " csv Comma-separated values\n"
drhb860bc92004-08-04 15:16:55 +00002139 " column Left-aligned columns. (See .width)\n"
2140 " html HTML <table> code\n"
2141 " insert SQL insert statements for TABLE\n"
2142 " line One value per line\n"
2143 " list Values delimited by .separator string\n"
2144 " tabs Tab-separated values\n"
2145 " tcl TCL list elements\n"
2146 ".nullvalue STRING Print STRING in place of NULL values\n"
drh75897232000-05-29 14:26:00 +00002147 ".output FILENAME Send output to FILENAME\n"
2148 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +00002149 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +00002150 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +00002151 ".read FILENAME Execute SQL in FILENAME\n"
drh9ff849f2009-02-04 20:55:57 +00002152 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
drh75897232000-05-29 14:26:00 +00002153 ".schema ?TABLE? Show the CREATE statements\n"
drhb860bc92004-08-04 15:16:55 +00002154 ".separator STRING Change separator used by output mode and .import\n"
drhdd45df82002-04-18 12:39:03 +00002155 ".show Show the current values for various settings\n"
drhfeac5f82004-08-01 00:10:45 +00002156 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
drh2dfbbca2000-07-28 14:32:48 +00002157 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +00002158 ".width NUM NUM ... Set column widths for \"column\" mode\n"
2159;
2160
shaneb320ccd2009-10-21 03:42:58 +00002161static char zTimerHelp[] =
2162 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
2163;
2164
drhdaffd0e2001-04-11 14:28:42 +00002165/* Forward reference */
drhc28490c2006-10-26 14:25:58 +00002166static int process_input(struct callback_data *p, FILE *in);
drhdaffd0e2001-04-11 14:28:42 +00002167
drh75897232000-05-29 14:26:00 +00002168/*
drh44c2eb12003-04-30 11:38:26 +00002169** Make sure the database is open. If it is not, then open it. If
2170** the database fails to open, print an error message and exit.
2171*/
2172static void open_db(struct callback_data *p){
2173 if( p->db==0 ){
danielk19774f057f92004-06-08 00:02:33 +00002174 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +00002175 db = p->db;
drh4cea5ba2008-05-05 16:27:24 +00002176 if( db && sqlite3_errcode(db)==SQLITE_OK ){
2177 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
2178 shellstaticFunc, 0, 0);
2179 }
2180 if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
danielk197780290862004-05-22 09:21:21 +00002181 fprintf(stderr,"Unable to open database \"%s\": %s\n",
2182 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +00002183 exit(1);
drh44c2eb12003-04-30 11:38:26 +00002184 }
drhc2e87a32006-06-27 15:16:14 +00002185#ifndef SQLITE_OMIT_LOAD_EXTENSION
2186 sqlite3_enable_load_extension(p->db, 1);
2187#endif
drh44c2eb12003-04-30 11:38:26 +00002188 }
2189}
2190
2191/*
drhfeac5f82004-08-01 00:10:45 +00002192** Do C-language style dequoting.
2193**
2194** \t -> tab
2195** \n -> newline
2196** \r -> carriage return
2197** \NNN -> ascii character NNN in octal
2198** \\ -> backslash
2199*/
2200static void resolve_backslashes(char *z){
shane7d3846a2008-12-11 02:58:26 +00002201 int i, j;
2202 char c;
drhfeac5f82004-08-01 00:10:45 +00002203 for(i=j=0; (c = z[i])!=0; i++, j++){
2204 if( c=='\\' ){
2205 c = z[++i];
2206 if( c=='n' ){
2207 c = '\n';
2208 }else if( c=='t' ){
2209 c = '\t';
2210 }else if( c=='r' ){
2211 c = '\r';
2212 }else if( c>='0' && c<='7' ){
drhaa816082005-12-29 12:53:09 +00002213 c -= '0';
drhfeac5f82004-08-01 00:10:45 +00002214 if( z[i+1]>='0' && z[i+1]<='7' ){
2215 i++;
2216 c = (c<<3) + z[i] - '0';
2217 if( z[i+1]>='0' && z[i+1]<='7' ){
2218 i++;
2219 c = (c<<3) + z[i] - '0';
2220 }
2221 }
2222 }
2223 }
2224 z[j] = c;
2225 }
2226 z[j] = 0;
2227}
2228
2229/*
drhc28490c2006-10-26 14:25:58 +00002230** Interpret zArg as a boolean value. Return either 0 or 1.
2231*/
2232static int booleanValue(char *zArg){
2233 int val = atoi(zArg);
2234 int j;
2235 for(j=0; zArg[j]; j++){
shane7d3846a2008-12-11 02:58:26 +00002236 zArg[j] = (char)tolower(zArg[j]);
drhc28490c2006-10-26 14:25:58 +00002237 }
2238 if( strcmp(zArg,"on")==0 ){
2239 val = 1;
2240 }else if( strcmp(zArg,"yes")==0 ){
2241 val = 1;
2242 }
2243 return val;
2244}
2245
2246/*
drh75897232000-05-29 14:26:00 +00002247** If an input line begins with "." then invoke this routine to
2248** process that line.
drh67505e72002-04-19 12:34:06 +00002249**
drh47ad6842006-11-08 12:25:42 +00002250** Return 1 on error, 2 to exit, and 0 otherwise.
drh75897232000-05-29 14:26:00 +00002251*/
drh44c2eb12003-04-30 11:38:26 +00002252static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +00002253 int i = 1;
2254 int nArg = 0;
2255 int n, c;
drh67505e72002-04-19 12:34:06 +00002256 int rc = 0;
drh75897232000-05-29 14:26:00 +00002257 char *azArg[50];
2258
2259 /* Parse the input line into tokens.
2260 */
2261 while( zLine[i] && nArg<ArraySize(azArg) ){
drh4c755c02004-08-08 20:22:17 +00002262 while( isspace((unsigned char)zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +00002263 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +00002264 if( zLine[i]=='\'' || zLine[i]=='"' ){
2265 int delim = zLine[i++];
2266 azArg[nArg++] = &zLine[i];
2267 while( zLine[i] && zLine[i]!=delim ){ i++; }
2268 if( zLine[i]==delim ){
2269 zLine[i++] = 0;
2270 }
drhfeac5f82004-08-01 00:10:45 +00002271 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +00002272 }else{
2273 azArg[nArg++] = &zLine[i];
drh4c755c02004-08-08 20:22:17 +00002274 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +00002275 if( zLine[i] ) zLine[i++] = 0;
drhfeac5f82004-08-01 00:10:45 +00002276 resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +00002277 }
2278 }
2279
2280 /* Process the input line.
2281 */
drh67505e72002-04-19 12:34:06 +00002282 if( nArg==0 ) return rc;
drh4f21c4a2008-12-10 22:15:00 +00002283 n = strlen30(azArg[0]);
drh75897232000-05-29 14:26:00 +00002284 c = azArg[0][0];
drh9ff849f2009-02-04 20:55:57 +00002285 if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 ){
2286 const char *zDestFile;
2287 const char *zDb;
2288 sqlite3 *pDest;
2289 sqlite3_backup *pBackup;
2290 int rc;
2291 if( nArg==2 ){
2292 zDestFile = azArg[1];
2293 zDb = "main";
2294 }else{
2295 zDestFile = azArg[2];
2296 zDb = azArg[1];
2297 }
2298 rc = sqlite3_open(zDestFile, &pDest);
2299 if( rc!=SQLITE_OK ){
2300 fprintf(stderr, "Error: cannot open %s\n", zDestFile);
2301 sqlite3_close(pDest);
2302 return 1;
2303 }
drhdc2c4912009-02-04 22:46:47 +00002304 open_db(p);
drh9ff849f2009-02-04 20:55:57 +00002305 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
2306 if( pBackup==0 ){
2307 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
2308 sqlite3_close(pDest);
2309 return 1;
2310 }
2311 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
2312 sqlite3_backup_finish(pBackup);
2313 if( rc==SQLITE_DONE ){
2314 rc = SQLITE_OK;
2315 }else{
2316 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
2317 }
2318 sqlite3_close(pDest);
2319 }else
2320
2321 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){
drhc49f44e2006-10-26 18:15:42 +00002322 bail_on_error = booleanValue(azArg[1]);
2323 }else
2324
jplyon6a65bb32003-05-04 07:25:57 +00002325 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +00002326 struct callback_data data;
2327 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +00002328 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +00002329 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +00002330 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +00002331 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +00002332 data.colWidth[0] = 3;
2333 data.colWidth[1] = 15;
2334 data.colWidth[2] = 58;
drh0b2110c2004-10-26 00:08:10 +00002335 data.cnt = 0;
danielk19776f8a5032004-05-10 10:34:51 +00002336 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +00002337 if( zErrMsg ){
2338 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00002339 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +00002340 }
2341 }else
2342
drh4c653a02000-06-07 01:27:47 +00002343 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
2344 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00002345 open_db(p);
drhf1dfc4f2009-09-23 15:51:35 +00002346 /* When playing back a "dump", the content might appear in an order
2347 ** which causes immediate foreign key constraints to be violated.
2348 ** So disable foreign-key constraint enforcement to prevent problems. */
2349 fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
drh33048c02001-10-01 14:29:22 +00002350 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh45e29d82006-11-20 16:21:10 +00002351 p->writableSchema = 0;
drh93f41e52008-08-11 19:12:34 +00002352 sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0);
drh4c653a02000-06-07 01:27:47 +00002353 if( nArg==1 ){
drhdd3d4592004-08-30 01:54:05 +00002354 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +00002355 "SELECT name, type, sql FROM sqlite_master "
drh4f324762009-05-21 14:51:03 +00002356 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0
2357 );
2358 run_schema_dump_query(p,
2359 "SELECT name, type, sql FROM sqlite_master "
2360 "WHERE name=='sqlite_sequence'", 0
drh0b9a5942006-09-13 20:22:02 +00002361 );
2362 run_table_dump_query(p->out, p->db,
2363 "SELECT sql FROM sqlite_master "
drh157e29a2009-05-21 15:15:00 +00002364 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
drha18c5682000-10-08 22:20:57 +00002365 );
drh4c653a02000-06-07 01:27:47 +00002366 }else{
2367 int i;
drhdd3d4592004-08-30 01:54:05 +00002368 for(i=1; i<nArg; i++){
danielk1977bc6ada42004-06-30 08:20:16 +00002369 zShellStatic = azArg[i];
drhdd3d4592004-08-30 01:54:05 +00002370 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +00002371 "SELECT name, type, sql FROM sqlite_master "
drhdd3d4592004-08-30 01:54:05 +00002372 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
drh45e29d82006-11-20 16:21:10 +00002373 " AND sql NOT NULL", 0);
drh0b9a5942006-09-13 20:22:02 +00002374 run_table_dump_query(p->out, p->db,
2375 "SELECT sql FROM sqlite_master "
drh45e29d82006-11-20 16:21:10 +00002376 "WHERE sql NOT NULL"
2377 " AND type IN ('index','trigger','view')"
drh157e29a2009-05-21 15:15:00 +00002378 " AND tbl_name LIKE shellstatic()", 0
drh0b9a5942006-09-13 20:22:02 +00002379 );
danielk1977bc6ada42004-06-30 08:20:16 +00002380 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +00002381 }
2382 }
drh45e29d82006-11-20 16:21:10 +00002383 if( p->writableSchema ){
2384 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
2385 p->writableSchema = 0;
2386 }
drh93f41e52008-08-11 19:12:34 +00002387 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0);
drh4c653a02000-06-07 01:27:47 +00002388 if( zErrMsg ){
2389 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00002390 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +00002391 }else{
2392 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +00002393 }
2394 }else
drh75897232000-05-29 14:26:00 +00002395
drhdaffd0e2001-04-11 14:28:42 +00002396 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
drhc28490c2006-10-26 14:25:58 +00002397 p->echoOn = booleanValue(azArg[1]);
drhdaffd0e2001-04-11 14:28:42 +00002398 }else
2399
drh75897232000-05-29 14:26:00 +00002400 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh47ad6842006-11-08 12:25:42 +00002401 rc = 2;
drh75897232000-05-29 14:26:00 +00002402 }else
2403
drhdd45df82002-04-18 12:39:03 +00002404 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
drhc28490c2006-10-26 14:25:58 +00002405 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
persicom7e2dfdd2002-04-18 02:46:52 +00002406 if(val == 1) {
2407 if(!p->explainPrev.valid) {
2408 p->explainPrev.valid = 1;
2409 p->explainPrev.mode = p->mode;
2410 p->explainPrev.showHeader = p->showHeader;
2411 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2412 }
2413 /* We could put this code under the !p->explainValid
2414 ** condition so that it does not execute if we are already in
2415 ** explain mode. However, always executing it allows us an easy
2416 ** was to reset to explain mode in case the user previously
2417 ** did an .explain followed by a .width, .mode or .header
2418 ** command.
2419 */
danielk19770d78bae2008-01-03 07:09:48 +00002420 p->mode = MODE_Explain;
persicom7e2dfdd2002-04-18 02:46:52 +00002421 p->showHeader = 1;
2422 memset(p->colWidth,0,ArraySize(p->colWidth));
danielk19770d78bae2008-01-03 07:09:48 +00002423 p->colWidth[0] = 4; /* addr */
drh60a713c2008-01-21 16:22:45 +00002424 p->colWidth[1] = 13; /* opcode */
2425 p->colWidth[2] = 4; /* P1 */
2426 p->colWidth[3] = 4; /* P2 */
2427 p->colWidth[4] = 4; /* P3 */
2428 p->colWidth[5] = 13; /* P4 */
danielk19770d78bae2008-01-03 07:09:48 +00002429 p->colWidth[6] = 2; /* P5 */
drh60a713c2008-01-21 16:22:45 +00002430 p->colWidth[7] = 13; /* Comment */
persicom7e2dfdd2002-04-18 02:46:52 +00002431 }else if (p->explainPrev.valid) {
2432 p->explainPrev.valid = 0;
2433 p->mode = p->explainPrev.mode;
2434 p->showHeader = p->explainPrev.showHeader;
2435 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2436 }
drh75897232000-05-29 14:26:00 +00002437 }else
2438
danielk1977c8c70692009-02-25 15:22:02 +00002439#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
2440 if( c=='g' && strncmp(azArg[0], "genfkey", n)==0 ){
2441 GenfkeyCmd cmd;
2442 if( 0==genfkeyParseArgs(&cmd, &azArg[1], nArg-1) ){
2443 cmd.db = p->db;
2444 cmd.pCb = p;
2445 genfkey_create_triggers(p->db, "main", (void *)&cmd, genfkeyCmdCb);
2446 }
2447 }else
2448#endif
2449
drhc28490c2006-10-26 14:25:58 +00002450 if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
persicom7e2dfdd2002-04-18 02:46:52 +00002451 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drhc28490c2006-10-26 14:25:58 +00002452 p->showHeader = booleanValue(azArg[1]);
drh75897232000-05-29 14:26:00 +00002453 }else
2454
2455 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
drha81c64a2009-01-14 23:38:02 +00002456 fprintf(stderr,"%s",zHelp);
shaneb320ccd2009-10-21 03:42:58 +00002457 if( HAS_TIMER ){
2458 fprintf(stderr,"%s",zTimerHelp);
2459 }
drh75897232000-05-29 14:26:00 +00002460 }else
2461
drhfeac5f82004-08-01 00:10:45 +00002462 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
2463 char *zTable = azArg[2]; /* Insert data into this table */
2464 char *zFile = azArg[1]; /* The file from which to extract data */
2465 sqlite3_stmt *pStmt; /* A statement */
2466 int rc; /* Result code */
2467 int nCol; /* Number of columns in the table */
2468 int nByte; /* Number of bytes in an SQL string */
2469 int i, j; /* Loop counters */
2470 int nSep; /* Number of bytes in p->separator[] */
2471 char *zSql; /* An SQL statement */
2472 char *zLine; /* A single line of input from the file */
2473 char **azCol; /* zLine[] broken up into columns */
2474 char *zCommit; /* How to commit changes */
drhb860bc92004-08-04 15:16:55 +00002475 FILE *in; /* The input file */
2476 int lineno = 0; /* Line number of input file */
drhfeac5f82004-08-01 00:10:45 +00002477
drha543c822006-06-08 16:10:14 +00002478 open_db(p);
drh4f21c4a2008-12-10 22:15:00 +00002479 nSep = strlen30(p->separator);
drhfeac5f82004-08-01 00:10:45 +00002480 if( nSep==0 ){
2481 fprintf(stderr, "non-null separator required for import\n");
2482 return 0;
2483 }
2484 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
2485 if( zSql==0 ) return 0;
drh4f21c4a2008-12-10 22:15:00 +00002486 nByte = strlen30(zSql);
drh5e6078b2006-01-31 19:07:22 +00002487 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
drhfeac5f82004-08-01 00:10:45 +00002488 sqlite3_free(zSql);
2489 if( rc ){
2490 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
2491 nCol = 0;
drh47ad6842006-11-08 12:25:42 +00002492 rc = 1;
drhfeac5f82004-08-01 00:10:45 +00002493 }else{
2494 nCol = sqlite3_column_count(pStmt);
2495 }
2496 sqlite3_finalize(pStmt);
2497 if( nCol==0 ) return 0;
2498 zSql = malloc( nByte + 20 + nCol*2 );
2499 if( zSql==0 ) return 0;
2500 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
drh4f21c4a2008-12-10 22:15:00 +00002501 j = strlen30(zSql);
drhfeac5f82004-08-01 00:10:45 +00002502 for(i=1; i<nCol; i++){
2503 zSql[j++] = ',';
2504 zSql[j++] = '?';
2505 }
2506 zSql[j++] = ')';
2507 zSql[j] = 0;
drh5e6078b2006-01-31 19:07:22 +00002508 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
drhfeac5f82004-08-01 00:10:45 +00002509 free(zSql);
2510 if( rc ){
2511 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
2512 sqlite3_finalize(pStmt);
drh47ad6842006-11-08 12:25:42 +00002513 return 1;
drhfeac5f82004-08-01 00:10:45 +00002514 }
2515 in = fopen(zFile, "rb");
2516 if( in==0 ){
2517 fprintf(stderr, "cannot open file: %s\n", zFile);
2518 sqlite3_finalize(pStmt);
2519 return 0;
2520 }
2521 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
drh43617e92006-03-06 20:55:46 +00002522 if( azCol==0 ){
2523 fclose(in);
2524 return 0;
2525 }
drhfeac5f82004-08-01 00:10:45 +00002526 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
2527 zCommit = "COMMIT";
2528 while( (zLine = local_getline(0, in))!=0 ){
2529 char *z;
2530 i = 0;
drhb860bc92004-08-04 15:16:55 +00002531 lineno++;
drhfeac5f82004-08-01 00:10:45 +00002532 azCol[0] = zLine;
drh36d4e972004-10-06 14:39:06 +00002533 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
drhfeac5f82004-08-01 00:10:45 +00002534 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
2535 *z = 0;
2536 i++;
drhb860bc92004-08-04 15:16:55 +00002537 if( i<nCol ){
2538 azCol[i] = &z[nSep];
2539 z += nSep-1;
2540 }
drhfeac5f82004-08-01 00:10:45 +00002541 }
2542 }
drh1cd7f832005-08-05 18:50:51 +00002543 *z = 0;
drhb860bc92004-08-04 15:16:55 +00002544 if( i+1!=nCol ){
2545 fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
2546 zFile, lineno, nCol, i+1);
2547 zCommit = "ROLLBACK";
drh1822eee2008-12-04 12:26:00 +00002548 free(zLine);
drhb860bc92004-08-04 15:16:55 +00002549 break;
2550 }
drhfeac5f82004-08-01 00:10:45 +00002551 for(i=0; i<nCol; i++){
2552 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
2553 }
2554 sqlite3_step(pStmt);
2555 rc = sqlite3_reset(pStmt);
2556 free(zLine);
2557 if( rc!=SQLITE_OK ){
2558 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
2559 zCommit = "ROLLBACK";
drh47ad6842006-11-08 12:25:42 +00002560 rc = 1;
drhfeac5f82004-08-01 00:10:45 +00002561 break;
2562 }
2563 }
2564 free(azCol);
2565 fclose(in);
2566 sqlite3_finalize(pStmt);
drhb860bc92004-08-04 15:16:55 +00002567 sqlite3_exec(p->db, zCommit, 0, 0, 0);
drhfeac5f82004-08-01 00:10:45 +00002568 }else
2569
drh75897232000-05-29 14:26:00 +00002570 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
2571 struct callback_data data;
2572 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00002573 open_db(p);
drh75897232000-05-29 14:26:00 +00002574 memcpy(&data, p, sizeof(data));
2575 data.showHeader = 0;
2576 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +00002577 zShellStatic = azArg[1];
2578 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +00002579 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00002580 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00002581 "UNION ALL "
2582 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00002583 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00002584 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00002585 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +00002586 );
danielk1977bc6ada42004-06-30 08:20:16 +00002587 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +00002588 if( zErrMsg ){
2589 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00002590 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00002591 }
2592 }else
2593
drhae5e4452007-05-03 17:18:36 +00002594#ifdef SQLITE_ENABLE_IOTRACE
drhb0603412007-02-28 04:47:26 +00002595 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
mlcreech3a00f902008-03-04 17:45:01 +00002596 extern void (*sqlite3IoTrace)(const char*, ...);
drhb0603412007-02-28 04:47:26 +00002597 if( iotrace && iotrace!=stdout ) fclose(iotrace);
2598 iotrace = 0;
2599 if( nArg<2 ){
mlcreech3a00f902008-03-04 17:45:01 +00002600 sqlite3IoTrace = 0;
drhb0603412007-02-28 04:47:26 +00002601 }else if( strcmp(azArg[1], "-")==0 ){
mlcreech3a00f902008-03-04 17:45:01 +00002602 sqlite3IoTrace = iotracePrintf;
drhb0603412007-02-28 04:47:26 +00002603 iotrace = stdout;
2604 }else{
2605 iotrace = fopen(azArg[1], "w");
2606 if( iotrace==0 ){
2607 fprintf(stderr, "cannot open \"%s\"\n", azArg[1]);
mlcreech3a00f902008-03-04 17:45:01 +00002608 sqlite3IoTrace = 0;
drhb0603412007-02-28 04:47:26 +00002609 }else{
mlcreech3a00f902008-03-04 17:45:01 +00002610 sqlite3IoTrace = iotracePrintf;
drhb0603412007-02-28 04:47:26 +00002611 }
2612 }
2613 }else
drhae5e4452007-05-03 17:18:36 +00002614#endif
drhb0603412007-02-28 04:47:26 +00002615
drh70df4fe2006-06-13 15:12:21 +00002616#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh1e397f82006-06-08 15:28:43 +00002617 if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
2618 const char *zFile, *zProc;
2619 char *zErrMsg = 0;
2620 int rc;
2621 zFile = azArg[1];
2622 zProc = nArg>=3 ? azArg[2] : 0;
2623 open_db(p);
2624 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
2625 if( rc!=SQLITE_OK ){
2626 fprintf(stderr, "%s\n", zErrMsg);
2627 sqlite3_free(zErrMsg);
drh47ad6842006-11-08 12:25:42 +00002628 rc = 1;
drh1e397f82006-06-08 15:28:43 +00002629 }
2630 }else
drh70df4fe2006-06-13 15:12:21 +00002631#endif
drh1e397f82006-06-08 15:28:43 +00002632
drh28bd4bc2000-06-15 15:57:22 +00002633 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh4f21c4a2008-12-10 22:15:00 +00002634 int n2 = strlen30(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00002635 if( strncmp(azArg[1],"line",n2)==0
2636 ||
2637 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +00002638 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +00002639 }else if( strncmp(azArg[1],"column",n2)==0
2640 ||
2641 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +00002642 p->mode = MODE_Column;
2643 }else if( strncmp(azArg[1],"list",n2)==0 ){
2644 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +00002645 }else if( strncmp(azArg[1],"html",n2)==0 ){
2646 p->mode = MODE_Html;
drhfeac5f82004-08-01 00:10:45 +00002647 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
2648 p->mode = MODE_Tcl;
2649 }else if( strncmp(azArg[1],"csv",n2)==0 ){
drh8e64d1c2004-10-07 00:32:39 +00002650 p->mode = MODE_Csv;
drh5bb3eb92007-05-04 13:15:55 +00002651 sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
drhfeac5f82004-08-01 00:10:45 +00002652 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
2653 p->mode = MODE_List;
drh5bb3eb92007-05-04 13:15:55 +00002654 sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
drh28bd4bc2000-06-15 15:57:22 +00002655 }else if( strncmp(azArg[1],"insert",n2)==0 ){
2656 p->mode = MODE_Insert;
2657 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +00002658 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +00002659 }else{
drh33048c02001-10-01 14:29:22 +00002660 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +00002661 }
drhdaffd0e2001-04-11 14:28:42 +00002662 }else {
drhcf68ae92006-12-19 18:47:41 +00002663 fprintf(stderr,"mode should be one of: "
drhfeac5f82004-08-01 00:10:45 +00002664 "column csv html insert line list tabs tcl\n");
drh75897232000-05-29 14:26:00 +00002665 }
2666 }else
2667
persicom7e2dfdd2002-04-18 02:46:52 +00002668 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
drh5bb3eb92007-05-04 13:15:55 +00002669 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
2670 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00002671 }else
2672
drh75897232000-05-29 14:26:00 +00002673 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
2674 if( p->out!=stdout ){
2675 fclose(p->out);
2676 }
2677 if( strcmp(azArg[1],"stdout")==0 ){
2678 p->out = stdout;
drh5bb3eb92007-05-04 13:15:55 +00002679 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
drh75897232000-05-29 14:26:00 +00002680 }else{
drha1f9b5e2004-02-14 16:31:02 +00002681 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +00002682 if( p->out==0 ){
2683 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
2684 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00002685 } else {
drh5bb3eb92007-05-04 13:15:55 +00002686 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
drh75897232000-05-29 14:26:00 +00002687 }
2688 }
2689 }else
2690
drhdd45df82002-04-18 12:39:03 +00002691 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +00002692 if( nArg >= 2) {
2693 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
2694 }
2695 if( nArg >= 3) {
2696 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
2697 }
2698 }else
2699
2700 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drh47ad6842006-11-08 12:25:42 +00002701 rc = 2;
persicom7e2dfdd2002-04-18 02:46:52 +00002702 }else
2703
drh9ff849f2009-02-04 20:55:57 +00002704 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +00002705 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +00002706 if( alt==0 ){
2707 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
2708 }else{
2709 process_input(p, alt);
2710 fclose(alt);
2711 }
2712 }else
2713
drh9ff849f2009-02-04 20:55:57 +00002714 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 ){
2715 const char *zSrcFile;
2716 const char *zDb;
2717 sqlite3 *pSrc;
2718 sqlite3_backup *pBackup;
2719 int rc;
drhdc2c4912009-02-04 22:46:47 +00002720 int nTimeout = 0;
2721
drh9ff849f2009-02-04 20:55:57 +00002722 if( nArg==2 ){
2723 zSrcFile = azArg[1];
2724 zDb = "main";
2725 }else{
2726 zSrcFile = azArg[2];
2727 zDb = azArg[1];
2728 }
2729 rc = sqlite3_open(zSrcFile, &pSrc);
2730 if( rc!=SQLITE_OK ){
2731 fprintf(stderr, "Error: cannot open %s\n", zSrcFile);
2732 sqlite3_close(pSrc);
2733 return 1;
2734 }
drhdc2c4912009-02-04 22:46:47 +00002735 open_db(p);
drh9ff849f2009-02-04 20:55:57 +00002736 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
2737 if( pBackup==0 ){
2738 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2739 sqlite3_close(pSrc);
2740 return 1;
2741 }
drhdc2c4912009-02-04 22:46:47 +00002742 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
2743 || rc==SQLITE_BUSY ){
2744 if( rc==SQLITE_BUSY ){
2745 if( nTimeout++ >= 3 ) break;
2746 sqlite3_sleep(100);
drh9ff849f2009-02-04 20:55:57 +00002747 }
2748 }
2749 sqlite3_backup_finish(pBackup);
2750 if( rc==SQLITE_DONE ){
2751 rc = SQLITE_OK;
drhdc2c4912009-02-04 22:46:47 +00002752 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
2753 fprintf(stderr, "source database is busy\n");
drh9ff849f2009-02-04 20:55:57 +00002754 }else{
2755 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2756 }
2757 sqlite3_close(pSrc);
2758 }else
2759
drh75897232000-05-29 14:26:00 +00002760 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
2761 struct callback_data data;
2762 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00002763 open_db(p);
drh75897232000-05-29 14:26:00 +00002764 memcpy(&data, p, sizeof(data));
2765 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +00002766 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +00002767 if( nArg>1 ){
drhc8d74412004-08-31 23:41:26 +00002768 int i;
shane7d3846a2008-12-11 02:58:26 +00002769 for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]);
drhc8d74412004-08-31 23:41:26 +00002770 if( strcmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +00002771 char *new_argv[2], *new_colv[2];
2772 new_argv[0] = "CREATE TABLE sqlite_master (\n"
2773 " type text,\n"
2774 " name text,\n"
2775 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +00002776 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00002777 " sql text\n"
2778 ")";
2779 new_argv[1] = 0;
2780 new_colv[0] = "sql";
2781 new_colv[1] = 0;
2782 callback(&data, 1, new_argv, new_colv);
drhc8d74412004-08-31 23:41:26 +00002783 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00002784 char *new_argv[2], *new_colv[2];
2785 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
2786 " type text,\n"
2787 " name text,\n"
2788 " tbl_name text,\n"
2789 " rootpage integer,\n"
2790 " sql text\n"
2791 ")";
2792 new_argv[1] = 0;
2793 new_colv[0] = "sql";
2794 new_colv[1] = 0;
2795 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00002796 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00002797 zShellStatic = azArg[1];
2798 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00002799 "SELECT sql FROM "
drh8f800a72009-01-14 23:17:55 +00002800 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
2801 " FROM sqlite_master UNION ALL"
2802 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +00002803 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00002804 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +00002805 callback, &data, &zErrMsg);
2806 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +00002807 }
drh75897232000-05-29 14:26:00 +00002808 }else{
danielk19776f8a5032004-05-10 10:34:51 +00002809 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00002810 "SELECT sql FROM "
drh8f800a72009-01-14 23:17:55 +00002811 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
2812 " FROM sqlite_master UNION ALL"
2813 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
drh0c356672005-09-10 22:40:53 +00002814 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00002815 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00002816 callback, &data, &zErrMsg
2817 );
drh75897232000-05-29 14:26:00 +00002818 }
drh75897232000-05-29 14:26:00 +00002819 if( zErrMsg ){
2820 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00002821 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00002822 }
2823 }else
2824
2825 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
drh5bb3eb92007-05-04 13:15:55 +00002826 sqlite3_snprintf(sizeof(p->separator), p->separator,
2827 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
drh75897232000-05-29 14:26:00 +00002828 }else
2829
persicom7e2dfdd2002-04-18 02:46:52 +00002830 if( c=='s' && strncmp(azArg[0], "show", n)==0){
2831 int i;
2832 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00002833 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00002834 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00002835 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
drhfeac5f82004-08-01 00:10:45 +00002836 fprintf(p->out,"%9.9s: ", "nullvalue");
2837 output_c_string(p->out, p->nullvalue);
2838 fprintf(p->out, "\n");
drh67505e72002-04-19 12:34:06 +00002839 fprintf(p->out,"%9.9s: %s\n","output",
drh4f21c4a2008-12-10 22:15:00 +00002840 strlen30(p->outfile) ? p->outfile : "stdout");
drhfeac5f82004-08-01 00:10:45 +00002841 fprintf(p->out,"%9.9s: ", "separator");
2842 output_c_string(p->out, p->separator);
2843 fprintf(p->out, "\n");
persicom7e2dfdd2002-04-18 02:46:52 +00002844 fprintf(p->out,"%9.9s: ","width");
2845 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
drhfeac5f82004-08-01 00:10:45 +00002846 fprintf(p->out,"%d ",p->colWidth[i]);
persicom7e2dfdd2002-04-18 02:46:52 +00002847 }
drhfeac5f82004-08-01 00:10:45 +00002848 fprintf(p->out,"\n");
persicom7e2dfdd2002-04-18 02:46:52 +00002849 }else
2850
drh2dfbbca2000-07-28 14:32:48 +00002851 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00002852 char **azResult;
2853 int nRow, rc;
2854 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00002855 open_db(p);
drha50da102000-08-08 20:19:09 +00002856 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00002857 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00002858 "SELECT name FROM sqlite_master "
drh0c356672005-09-10 22:40:53 +00002859 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00002860 "UNION ALL "
2861 "SELECT name FROM sqlite_temp_master "
2862 "WHERE type IN ('table','view') "
2863 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00002864 &azResult, &nRow, 0, &zErrMsg
2865 );
drha50da102000-08-08 20:19:09 +00002866 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00002867 zShellStatic = azArg[1];
2868 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00002869 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00002870 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00002871 "UNION ALL "
2872 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00002873 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00002874 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00002875 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00002876 );
danielk1977bc6ada42004-06-30 08:20:16 +00002877 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00002878 }
drh75897232000-05-29 14:26:00 +00002879 if( zErrMsg ){
2880 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00002881 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00002882 }
drhe3710332000-09-29 13:30:53 +00002883 if( rc==SQLITE_OK ){
2884 int len, maxlen = 0;
2885 int i, j;
2886 int nPrintCol, nPrintRow;
2887 for(i=1; i<=nRow; i++){
2888 if( azResult[i]==0 ) continue;
drh4f21c4a2008-12-10 22:15:00 +00002889 len = strlen30(azResult[i]);
drhe3710332000-09-29 13:30:53 +00002890 if( len>maxlen ) maxlen = len;
2891 }
2892 nPrintCol = 80/(maxlen+2);
2893 if( nPrintCol<1 ) nPrintCol = 1;
2894 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
2895 for(i=0; i<nPrintRow; i++){
2896 for(j=i+1; j<=nRow; j+=nPrintRow){
2897 char *zSp = j<=nPrintRow ? "" : " ";
2898 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
2899 }
2900 printf("\n");
2901 }
drh47ad6842006-11-08 12:25:42 +00002902 }else{
2903 rc = 1;
drhe3710332000-09-29 13:30:53 +00002904 }
danielk19776f8a5032004-05-10 10:34:51 +00002905 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00002906 }else
2907
drh3b1a9882007-11-02 12:53:03 +00002908 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00002909 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00002910 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
shaneb320ccd2009-10-21 03:42:58 +00002911 }else if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){
drh3b1a9882007-11-02 12:53:03 +00002912 enableTimer = booleanValue(azArg[1]);
shaneb320ccd2009-10-21 03:42:58 +00002913 }else if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
drh75897232000-05-29 14:26:00 +00002914 int j;
drh43617e92006-03-06 20:55:46 +00002915 assert( nArg<=ArraySize(azArg) );
drh75897232000-05-29 14:26:00 +00002916 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
2917 p->colWidth[j-1] = atoi(azArg[j]);
2918 }
2919 }else
2920
drh3b1a9882007-11-02 12:53:03 +00002921
drh75897232000-05-29 14:26:00 +00002922 {
drh67505e72002-04-19 12:34:06 +00002923 fprintf(stderr, "unknown command or invalid arguments: "
2924 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00002925 }
drh67505e72002-04-19 12:34:06 +00002926
2927 return rc;
drh75897232000-05-29 14:26:00 +00002928}
2929
drh67505e72002-04-19 12:34:06 +00002930/*
drh91a66392007-09-07 01:12:32 +00002931** Return TRUE if a semicolon occurs anywhere in the first N characters
2932** of string z[].
drh324ccef2003-02-05 14:06:20 +00002933*/
drh91a66392007-09-07 01:12:32 +00002934static int _contains_semicolon(const char *z, int N){
2935 int i;
2936 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
2937 return 0;
drh324ccef2003-02-05 14:06:20 +00002938}
2939
2940/*
drh70c7a4b2003-04-26 03:03:06 +00002941** Test to see if a line consists entirely of whitespace.
2942*/
2943static int _all_whitespace(const char *z){
2944 for(; *z; z++){
drh4c755c02004-08-08 20:22:17 +00002945 if( isspace(*(unsigned char*)z) ) continue;
drh70c7a4b2003-04-26 03:03:06 +00002946 if( *z=='/' && z[1]=='*' ){
2947 z += 2;
2948 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
2949 if( *z==0 ) return 0;
2950 z++;
2951 continue;
2952 }
2953 if( *z=='-' && z[1]=='-' ){
2954 z += 2;
2955 while( *z && *z!='\n' ){ z++; }
2956 if( *z==0 ) return 1;
2957 continue;
2958 }
2959 return 0;
2960 }
2961 return 1;
2962}
2963
2964/*
drha9b17162003-04-29 18:01:28 +00002965** Return TRUE if the line typed in is an SQL command terminator other
2966** than a semi-colon. The SQL Server style "go" command is understood
2967** as is the Oracle "/".
2968*/
2969static int _is_command_terminator(const char *zLine){
drh4c755c02004-08-08 20:22:17 +00002970 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
drh233a5312008-12-18 22:25:13 +00002971 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2972 return 1; /* Oracle */
2973 }
drhc8d74412004-08-31 23:41:26 +00002974 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
2975 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00002976 return 1; /* SQL Server */
2977 }
2978 return 0;
2979}
2980
2981/*
drh233a5312008-12-18 22:25:13 +00002982** Return true if zSql is a complete SQL statement. Return false if it
2983** ends in the middle of a string literal or C-style comment.
2984*/
2985static int _is_complete(char *zSql, int nSql){
2986 int rc;
2987 if( zSql==0 ) return 1;
2988 zSql[nSql] = ';';
2989 zSql[nSql+1] = 0;
2990 rc = sqlite3_complete(zSql);
2991 zSql[nSql] = 0;
2992 return rc;
2993}
2994
2995/*
drh67505e72002-04-19 12:34:06 +00002996** Read input from *in and process it. If *in==0 then input
2997** is interactive - the user is typing it it. Otherwise, input
2998** is coming from a file or device. A prompt is issued and history
2999** is saved only if input is interactive. An interrupt signal will
3000** cause this routine to exit immediately, unless input is interactive.
drhc28490c2006-10-26 14:25:58 +00003001**
3002** Return the number of errors.
drh67505e72002-04-19 12:34:06 +00003003*/
drhc28490c2006-10-26 14:25:58 +00003004static int process_input(struct callback_data *p, FILE *in){
danielk19772ac27622007-07-03 05:31:16 +00003005 char *zLine = 0;
drhdaffd0e2001-04-11 14:28:42 +00003006 char *zSql = 0;
3007 int nSql = 0;
drh91a66392007-09-07 01:12:32 +00003008 int nSqlPrior = 0;
drhdaffd0e2001-04-11 14:28:42 +00003009 char *zErrMsg;
drhc49f44e2006-10-26 18:15:42 +00003010 int rc;
3011 int errCnt = 0;
drhc28490c2006-10-26 14:25:58 +00003012 int lineno = 0;
3013 int startline = 0;
drhc49f44e2006-10-26 18:15:42 +00003014
3015 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
3016 fflush(p->out);
danielk19772ac27622007-07-03 05:31:16 +00003017 free(zLine);
drhc49f44e2006-10-26 18:15:42 +00003018 zLine = one_input_line(zSql, in);
3019 if( zLine==0 ){
3020 break; /* We have reached EOF */
3021 }
drh67505e72002-04-19 12:34:06 +00003022 if( seenInterrupt ){
3023 if( in!=0 ) break;
3024 seenInterrupt = 0;
3025 }
drhc28490c2006-10-26 14:25:58 +00003026 lineno++;
drhdaffd0e2001-04-11 14:28:42 +00003027 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00003028 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00003029 if( zLine && zLine[0]=='.' && nSql==0 ){
drhc49f44e2006-10-26 18:15:42 +00003030 rc = do_meta_command(zLine, p);
drh47ad6842006-11-08 12:25:42 +00003031 if( rc==2 ){
3032 break;
3033 }else if( rc ){
drhc49f44e2006-10-26 18:15:42 +00003034 errCnt++;
3035 }
drhdaffd0e2001-04-11 14:28:42 +00003036 continue;
3037 }
drh233a5312008-12-18 22:25:13 +00003038 if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
drh5bb3eb92007-05-04 13:15:55 +00003039 memcpy(zLine,";",2);
drha9b17162003-04-29 18:01:28 +00003040 }
drh91a66392007-09-07 01:12:32 +00003041 nSqlPrior = nSql;
drhdaffd0e2001-04-11 14:28:42 +00003042 if( zSql==0 ){
3043 int i;
drh4c755c02004-08-08 20:22:17 +00003044 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
drhdaffd0e2001-04-11 14:28:42 +00003045 if( zLine[i]!=0 ){
drh4f21c4a2008-12-10 22:15:00 +00003046 nSql = strlen30(zLine);
drh233a5312008-12-18 22:25:13 +00003047 zSql = malloc( nSql+3 );
drhc1f44942006-05-10 14:39:13 +00003048 if( zSql==0 ){
3049 fprintf(stderr, "out of memory\n");
3050 exit(1);
3051 }
drh5bb3eb92007-05-04 13:15:55 +00003052 memcpy(zSql, zLine, nSql+1);
drhc28490c2006-10-26 14:25:58 +00003053 startline = lineno;
drhdaffd0e2001-04-11 14:28:42 +00003054 }
3055 }else{
drh4f21c4a2008-12-10 22:15:00 +00003056 int len = strlen30(zLine);
drh233a5312008-12-18 22:25:13 +00003057 zSql = realloc( zSql, nSql + len + 4 );
drhdaffd0e2001-04-11 14:28:42 +00003058 if( zSql==0 ){
3059 fprintf(stderr,"%s: out of memory!\n", Argv0);
3060 exit(1);
3061 }
drh5bb3eb92007-05-04 13:15:55 +00003062 zSql[nSql++] = '\n';
3063 memcpy(&zSql[nSql], zLine, len+1);
drhdaffd0e2001-04-11 14:28:42 +00003064 nSql += len;
3065 }
drh91a66392007-09-07 01:12:32 +00003066 if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
3067 && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00003068 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00003069 open_db(p);
drh3b1a9882007-11-02 12:53:03 +00003070 BEGIN_TIMER;
shane626a6e42009-10-22 17:30:15 +00003071 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
drh3b1a9882007-11-02 12:53:03 +00003072 END_TIMER;
drh7f953e22002-07-13 17:33:45 +00003073 if( rc || zErrMsg ){
drhc28490c2006-10-26 14:25:58 +00003074 char zPrefix[100];
3075 if( in!=0 || !stdin_is_interactive ){
drh5bb3eb92007-05-04 13:15:55 +00003076 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
3077 "SQL error near line %d:", startline);
drhc28490c2006-10-26 14:25:58 +00003078 }else{
drh5bb3eb92007-05-04 13:15:55 +00003079 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "SQL error:");
drhc28490c2006-10-26 14:25:58 +00003080 }
drh7f953e22002-07-13 17:33:45 +00003081 if( zErrMsg!=0 ){
shaned2bed1c2009-10-21 03:56:54 +00003082 fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00003083 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00003084 zErrMsg = 0;
3085 }else{
shaned2bed1c2009-10-21 03:56:54 +00003086 fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00003087 }
drhc49f44e2006-10-26 18:15:42 +00003088 errCnt++;
drhdaffd0e2001-04-11 14:28:42 +00003089 }
3090 free(zSql);
3091 zSql = 0;
3092 nSql = 0;
3093 }
3094 }
3095 if( zSql ){
drhdfef4992008-11-11 18:55:03 +00003096 if( !_all_whitespace(zSql) ) fprintf(stderr, "Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00003097 free(zSql);
3098 }
danielk19772ac27622007-07-03 05:31:16 +00003099 free(zLine);
drhc49f44e2006-10-26 18:15:42 +00003100 return errCnt;
drhdaffd0e2001-04-11 14:28:42 +00003101}
3102
drh67505e72002-04-19 12:34:06 +00003103/*
3104** Return a pathname which is the user's home directory. A
3105** 0 return indicates an error of some kind. Space to hold the
3106** resulting string is obtained from malloc(). The calling
3107** function should free the result.
3108*/
3109static char *find_home_dir(void){
3110 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00003111
chw97185482008-11-17 08:05:31 +00003112#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
drh67505e72002-04-19 12:34:06 +00003113 struct passwd *pwent;
3114 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00003115 if( (pwent=getpwuid(uid)) != NULL) {
3116 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00003117 }
3118#endif
3119
chw65d3c132007-11-12 21:09:10 +00003120#if defined(_WIN32_WCE)
3121 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
3122 */
3123 home_dir = strdup("/");
3124#else
3125
drh164a1b62006-08-19 11:15:20 +00003126#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
3127 if (!home_dir) {
3128 home_dir = getenv("USERPROFILE");
3129 }
3130#endif
3131
drh67505e72002-04-19 12:34:06 +00003132 if (!home_dir) {
3133 home_dir = getenv("HOME");
drh67505e72002-04-19 12:34:06 +00003134 }
3135
drhcdb36b72006-06-12 12:57:45 +00003136#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
drhe98d4fa2002-04-21 19:06:22 +00003137 if (!home_dir) {
drh164a1b62006-08-19 11:15:20 +00003138 char *zDrive, *zPath;
3139 int n;
3140 zDrive = getenv("HOMEDRIVE");
3141 zPath = getenv("HOMEPATH");
3142 if( zDrive && zPath ){
drh4f21c4a2008-12-10 22:15:00 +00003143 n = strlen30(zDrive) + strlen30(zPath) + 1;
drh164a1b62006-08-19 11:15:20 +00003144 home_dir = malloc( n );
3145 if( home_dir==0 ) return 0;
3146 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
3147 return home_dir;
3148 }
3149 home_dir = "c:\\";
drhe98d4fa2002-04-21 19:06:22 +00003150 }
3151#endif
3152
chw65d3c132007-11-12 21:09:10 +00003153#endif /* !_WIN32_WCE */
3154
drh67505e72002-04-19 12:34:06 +00003155 if( home_dir ){
drh4f21c4a2008-12-10 22:15:00 +00003156 int n = strlen30(home_dir) + 1;
drh5bb3eb92007-05-04 13:15:55 +00003157 char *z = malloc( n );
3158 if( z ) memcpy(z, home_dir, n);
drh67505e72002-04-19 12:34:06 +00003159 home_dir = z;
3160 }
drhe98d4fa2002-04-21 19:06:22 +00003161
drh67505e72002-04-19 12:34:06 +00003162 return home_dir;
3163}
3164
3165/*
3166** Read input from the file given by sqliterc_override. Or if that
3167** parameter is NULL, take input from ~/.sqliterc
3168*/
drh22fbcb82004-02-01 01:22:50 +00003169static void process_sqliterc(
3170 struct callback_data *p, /* Configuration data */
3171 const char *sqliterc_override /* Name of config file. NULL to use default */
3172){
persicom7e2dfdd2002-04-18 02:46:52 +00003173 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00003174 const char *sqliterc = sqliterc_override;
drh43617e92006-03-06 20:55:46 +00003175 char *zBuf = 0;
persicom7e2dfdd2002-04-18 02:46:52 +00003176 FILE *in = NULL;
drha959ac42007-06-20 13:10:00 +00003177 int nBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00003178
3179 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00003180 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00003181 if( home_dir==0 ){
chw97185482008-11-17 08:05:31 +00003182#if !defined(__RTP__) && !defined(_WRS_KERNEL)
drhe98d4fa2002-04-21 19:06:22 +00003183 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
chw97185482008-11-17 08:05:31 +00003184#endif
drhe98d4fa2002-04-21 19:06:22 +00003185 return;
3186 }
drh4f21c4a2008-12-10 22:15:00 +00003187 nBuf = strlen30(home_dir) + 16;
drha959ac42007-06-20 13:10:00 +00003188 zBuf = malloc( nBuf );
drh22fbcb82004-02-01 01:22:50 +00003189 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00003190 fprintf(stderr,"%s: out of memory!\n", Argv0);
3191 exit(1);
3192 }
drha959ac42007-06-20 13:10:00 +00003193 sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00003194 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00003195 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00003196 }
drha1f9b5e2004-02-14 16:31:02 +00003197 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00003198 if( in ){
drhc28490c2006-10-26 14:25:58 +00003199 if( stdin_is_interactive ){
drhb695aca2007-07-30 20:41:52 +00003200 printf("-- Loading resources from %s\n",sqliterc);
drh22fbcb82004-02-01 01:22:50 +00003201 }
persicom7e2dfdd2002-04-18 02:46:52 +00003202 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00003203 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00003204 }
drh43617e92006-03-06 20:55:46 +00003205 free(zBuf);
persicom7e2dfdd2002-04-18 02:46:52 +00003206 return;
3207}
3208
drh67505e72002-04-19 12:34:06 +00003209/*
drhe1e38c42003-05-04 18:30:59 +00003210** Show available command line options
3211*/
3212static const char zOptions[] =
3213 " -init filename read/process named file\n"
3214 " -echo print commands before execution\n"
3215 " -[no]header turn headers on or off\n"
drhc49f44e2006-10-26 18:15:42 +00003216 " -bail stop after hitting an error\n"
3217 " -interactive force interactive I/O\n"
3218 " -batch force batch I/O\n"
drhe1e38c42003-05-04 18:30:59 +00003219 " -column set output mode to 'column'\n"
drhc49f44e2006-10-26 18:15:42 +00003220 " -csv set output mode to 'csv'\n"
drhe1e38c42003-05-04 18:30:59 +00003221 " -html set output mode to HTML\n"
3222 " -line set output mode to 'line'\n"
3223 " -list set output mode to 'list'\n"
3224 " -separator 'x' set output field separator (|)\n"
3225 " -nullvalue 'text' set text string for NULL values\n"
3226 " -version show SQLite version\n"
drhe1e38c42003-05-04 18:30:59 +00003227;
3228static void usage(int showDetail){
drh80e8be92006-08-29 12:04:19 +00003229 fprintf(stderr,
3230 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
3231 "FILENAME is the name of an SQLite database. A new database is created\n"
3232 "if the file does not previously exist.\n", Argv0);
drhe1e38c42003-05-04 18:30:59 +00003233 if( showDetail ){
drh80e8be92006-08-29 12:04:19 +00003234 fprintf(stderr, "OPTIONS include:\n%s", zOptions);
drhe1e38c42003-05-04 18:30:59 +00003235 }else{
3236 fprintf(stderr, "Use the -help option for additional information\n");
3237 }
3238 exit(1);
3239}
3240
3241/*
drh67505e72002-04-19 12:34:06 +00003242** Initialize the state information in data
3243*/
drh0850b532006-01-31 19:31:43 +00003244static void main_init(struct callback_data *data) {
persicom7e2dfdd2002-04-18 02:46:52 +00003245 memset(data, 0, sizeof(*data));
3246 data->mode = MODE_List;
drh5bb3eb92007-05-04 13:15:55 +00003247 memcpy(data->separator,"|", 2);
persicom7e2dfdd2002-04-18 02:46:52 +00003248 data->showHeader = 0;
drh5bb3eb92007-05-04 13:15:55 +00003249 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3250 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
persicom7e2dfdd2002-04-18 02:46:52 +00003251}
3252
drh75897232000-05-29 14:26:00 +00003253int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00003254 char *zErrMsg = 0;
3255 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00003256 const char *zInitFile = 0;
3257 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00003258 int i;
drhc28490c2006-10-26 14:25:58 +00003259 int rc = 0;
drh75897232000-05-29 14:26:00 +00003260
drhdaffd0e2001-04-11 14:28:42 +00003261 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00003262 main_init(&data);
drhc28490c2006-10-26 14:25:58 +00003263 stdin_is_interactive = isatty(0);
persicom7e2dfdd2002-04-18 02:46:52 +00003264
drh44c2eb12003-04-30 11:38:26 +00003265 /* Make sure we have a valid signal handler early, before anything
3266 ** else is done.
3267 */
drh4c504392000-10-16 22:06:40 +00003268#ifdef SIGINT
3269 signal(SIGINT, interrupt_handler);
3270#endif
drh44c2eb12003-04-30 11:38:26 +00003271
drh22fbcb82004-02-01 01:22:50 +00003272 /* Do an initial pass through the command-line argument to locate
3273 ** the name of the database file, the name of the initialization file,
3274 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00003275 */
drh22fbcb82004-02-01 01:22:50 +00003276 for(i=1; i<argc-1; i++){
drhc28490c2006-10-26 14:25:58 +00003277 char *z;
drh44c2eb12003-04-30 11:38:26 +00003278 if( argv[i][0]!='-' ) break;
drhc28490c2006-10-26 14:25:58 +00003279 z = argv[i];
3280 if( z[0]=='-' && z[1]=='-' ) z++;
drh44c2eb12003-04-30 11:38:26 +00003281 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
3282 i++;
drh22fbcb82004-02-01 01:22:50 +00003283 }else if( strcmp(argv[i],"-init")==0 ){
3284 i++;
3285 zInitFile = argv[i];
drh44c2eb12003-04-30 11:38:26 +00003286 }
3287 }
drh22fbcb82004-02-01 01:22:50 +00003288 if( i<argc ){
danielk197729bafea2008-06-26 10:41:19 +00003289#if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
pweilbacherd190be82008-04-15 18:50:02 +00003290 data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
3291#else
drh22fbcb82004-02-01 01:22:50 +00003292 data.zDbFilename = argv[i++];
pweilbacherd190be82008-04-15 18:50:02 +00003293#endif
drh22fbcb82004-02-01 01:22:50 +00003294 }else{
danielk197703aded42004-11-22 05:26:27 +00003295#ifndef SQLITE_OMIT_MEMORYDB
drh22fbcb82004-02-01 01:22:50 +00003296 data.zDbFilename = ":memory:";
danielk197703aded42004-11-22 05:26:27 +00003297#else
3298 data.zDbFilename = 0;
3299#endif
drh22fbcb82004-02-01 01:22:50 +00003300 }
3301 if( i<argc ){
3302 zFirstCmd = argv[i++];
3303 }
drh44c2eb12003-04-30 11:38:26 +00003304 data.out = stdout;
3305
drh01b41712005-08-29 23:06:23 +00003306#ifdef SQLITE_OMIT_MEMORYDB
3307 if( data.zDbFilename==0 ){
3308 fprintf(stderr,"%s: no database filename specified\n", argv[0]);
3309 exit(1);
3310 }
3311#endif
3312
drh44c2eb12003-04-30 11:38:26 +00003313 /* Go ahead and open the database file if it already exists. If the
3314 ** file does not exist, delay opening it. This prevents empty database
3315 ** files from being created if a user mistypes the database name argument
3316 ** to the sqlite command-line tool.
3317 */
drhc8d74412004-08-31 23:41:26 +00003318 if( access(data.zDbFilename, 0)==0 ){
drh44c2eb12003-04-30 11:38:26 +00003319 open_db(&data);
3320 }
3321
drh22fbcb82004-02-01 01:22:50 +00003322 /* Process the initialization file if there is one. If no -init option
3323 ** is given on the command line, look for a file named ~/.sqliterc and
3324 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00003325 */
drh22fbcb82004-02-01 01:22:50 +00003326 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00003327
drh22fbcb82004-02-01 01:22:50 +00003328 /* Make a second pass through the command-line argument and set
3329 ** options. This second pass is delayed until after the initialization
3330 ** file is processed so that the command-line arguments will override
3331 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00003332 */
drh22fbcb82004-02-01 01:22:50 +00003333 for(i=1; i<argc && argv[i][0]=='-'; i++){
3334 char *z = argv[i];
drhc28490c2006-10-26 14:25:58 +00003335 if( z[1]=='-' ){ z++; }
drh2e584cd2006-09-25 13:09:22 +00003336 if( strcmp(z,"-init")==0 ){
drh22fbcb82004-02-01 01:22:50 +00003337 i++;
3338 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00003339 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00003340 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00003341 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00003342 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00003343 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00003344 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00003345 data.mode = MODE_Column;
drhc49f44e2006-10-26 18:15:42 +00003346 }else if( strcmp(z,"-csv")==0 ){
3347 data.mode = MODE_Csv;
drh5bb3eb92007-05-04 13:15:55 +00003348 memcpy(data.separator,",",2);
drh22fbcb82004-02-01 01:22:50 +00003349 }else if( strcmp(z,"-separator")==0 ){
3350 i++;
drh5bb3eb92007-05-04 13:15:55 +00003351 sqlite3_snprintf(sizeof(data.separator), data.separator,
3352 "%.*s",(int)sizeof(data.separator)-1,argv[i]);
drh22fbcb82004-02-01 01:22:50 +00003353 }else if( strcmp(z,"-nullvalue")==0 ){
3354 i++;
drh5bb3eb92007-05-04 13:15:55 +00003355 sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
3356 "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
drh22fbcb82004-02-01 01:22:50 +00003357 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00003358 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00003359 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00003360 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00003361 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00003362 data.echoOn = 1;
drhc49f44e2006-10-26 18:15:42 +00003363 }else if( strcmp(z,"-bail")==0 ){
3364 bail_on_error = 1;
drh22fbcb82004-02-01 01:22:50 +00003365 }else if( strcmp(z,"-version")==0 ){
drhc8d74412004-08-31 23:41:26 +00003366 printf("%s\n", sqlite3_libversion());
drh151e3e12006-06-06 12:32:21 +00003367 return 0;
drhc28490c2006-10-26 14:25:58 +00003368 }else if( strcmp(z,"-interactive")==0 ){
3369 stdin_is_interactive = 1;
3370 }else if( strcmp(z,"-batch")==0 ){
3371 stdin_is_interactive = 0;
drh80e8be92006-08-29 12:04:19 +00003372 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00003373 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00003374 }else{
drh22fbcb82004-02-01 01:22:50 +00003375 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00003376 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00003377 return 1;
3378 }
3379 }
drh44c2eb12003-04-30 11:38:26 +00003380
drh22fbcb82004-02-01 01:22:50 +00003381 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00003382 /* Run just the command that follows the database name
3383 */
drh22fbcb82004-02-01 01:22:50 +00003384 if( zFirstCmd[0]=='.' ){
3385 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00003386 exit(0);
3387 }else{
3388 int rc;
drh44c2eb12003-04-30 11:38:26 +00003389 open_db(&data);
shane626a6e42009-10-22 17:30:15 +00003390 rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00003391 if( rc!=0 && zErrMsg!=0 ){
3392 fprintf(stderr,"SQL error: %s\n", zErrMsg);
3393 exit(1);
3394 }
drh75897232000-05-29 14:26:00 +00003395 }
3396 }else{
drh44c2eb12003-04-30 11:38:26 +00003397 /* Run commands received from standard input
3398 */
drhc28490c2006-10-26 14:25:58 +00003399 if( stdin_is_interactive ){
drh67505e72002-04-19 12:34:06 +00003400 char *zHome;
3401 char *zHistory = 0;
drh5bb3eb92007-05-04 13:15:55 +00003402 int nHistory;
drh75897232000-05-29 14:26:00 +00003403 printf(
drhb217a572000-08-22 13:40:18 +00003404 "SQLite version %s\n"
mihailim65df9db2008-06-28 11:29:22 +00003405 "Enter \".help\" for instructions\n"
3406 "Enter SQL statements terminated with a \";\"\n",
drhc8d74412004-08-31 23:41:26 +00003407 sqlite3_libversion()
drh75897232000-05-29 14:26:00 +00003408 );
drh67505e72002-04-19 12:34:06 +00003409 zHome = find_home_dir();
drhea678832008-12-10 19:26:22 +00003410 if( zHome ){
drh4f21c4a2008-12-10 22:15:00 +00003411 nHistory = strlen30(zHome) + 20;
drhea678832008-12-10 19:26:22 +00003412 if( (zHistory = malloc(nHistory))!=0 ){
3413 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
3414 }
drh67505e72002-04-19 12:34:06 +00003415 }
danielk19774af00c62005-01-23 23:43:21 +00003416#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh67505e72002-04-19 12:34:06 +00003417 if( zHistory ) read_history(zHistory);
danielk19774af00c62005-01-23 23:43:21 +00003418#endif
drhc28490c2006-10-26 14:25:58 +00003419 rc = process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00003420 if( zHistory ){
3421 stifle_history(100);
3422 write_history(zHistory);
adamd0a3daa32006-07-28 20:16:14 +00003423 free(zHistory);
drh67505e72002-04-19 12:34:06 +00003424 }
adamd0a3daa32006-07-28 20:16:14 +00003425 free(zHome);
drhdaffd0e2001-04-11 14:28:42 +00003426 }else{
drhc28490c2006-10-26 14:25:58 +00003427 rc = process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00003428 }
3429 }
drh33048c02001-10-01 14:29:22 +00003430 set_table_name(&data, 0);
adamd0a3daa32006-07-28 20:16:14 +00003431 if( db ){
3432 if( sqlite3_close(db)!=SQLITE_OK ){
3433 fprintf(stderr,"error closing database: %s\n", sqlite3_errmsg(db));
3434 }
3435 }
drhc28490c2006-10-26 14:25:58 +00003436 return rc;
drh75897232000-05-29 14:26:00 +00003437}