summaryrefslogtreecommitdiffstats
path: root/sql/src/main/java/SQLite/Shell.java
blob: 78d37a14c671eb54a5cee8ddb2da5a6cf33e076e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package SQLite;

import SQLite.*;
import java.io.*;
import java.util.*;

/**
 * SQLite command line shell. This is a partial reimplementaion
 * of sqlite/src/shell.c and can be invoked by:<P>
 *
 * <verb>
 *     java SQLite.Shell [OPTIONS] database [SHELLCMD]
 * or
 *     java -jar sqlite.jar [OPTIONS] database [SHELLCMD]
 * </verb>
 */

public class Shell implements Callback {
    Database db;
    boolean echo;
    int count;
    int mode;
    boolean showHeader;
    String tableName;
    String sep;
    String cols[];
    int colwidth[];
    String destTable;
    PrintWriter pw;
    PrintWriter err;

    static final int MODE_Line = 0;
    static final int MODE_Column = 1;
    static final int MODE_List = 2;
    static final int MODE_Semi = 3;
    static final int MODE_Html = 4;
    static final int MODE_Insert = 5;
    static final int MODE_Insert2 = 6;

    public Shell(PrintWriter pw, PrintWriter err) {
    this.pw = pw;
    this.err = err;
    }

    public Shell(PrintStream ps, PrintStream errs) {
    pw = new PrintWriter(ps);
    err = new PrintWriter(errs);
    }

    protected Object clone() {
        Shell s = new Shell(this.pw, this.err);
    s.db = db;
    s.echo = echo;
    s.mode = mode;
    s.count = 0;
    s.showHeader = showHeader;
    s.tableName = tableName;
    s.sep = sep;
    s.colwidth = colwidth;
    return s;
    }

    static public String sql_quote_dbl(String str) {
    if (str == null) {
        return "NULL";
    }
    int i, single = 0, dbl = 0;
    for (i = 0; i < str.length(); i++) {
        if (str.charAt(i) == '\'') {
        single++;
        } else if (str.charAt(i) == '"') {
        dbl++;
        }
    }
    if (dbl == 0) {
        return "\"" + str + "\"";
    }
    StringBuffer sb = new StringBuffer("\"");
    for (i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (c == '"') {
        sb.append("\"\"");
        } else {
        sb.append(c);
        }
    }
    return sb.toString();
    }

    static public String sql_quote(String str) {
    if (str == null) {
        return "NULL";
    }
    int i, single = 0, dbl = 0;
    for (i = 0; i < str.length(); i++) {
        if (str.charAt(i) == '\'') {
        single++;
        } else if (str.charAt(i) == '"') {
        dbl++;
        }
    }
    if (single == 0) {
        return "'" + str + "'";
    }
    if (dbl == 0) {
        return "\"" + str + "\"";
    }
    StringBuffer sb = new StringBuffer("'");
    for (i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (c == '\'') {
        sb.append("''");
        } else {
        sb.append(c);
        }
    }
    return sb.toString();
    }

    static String html_quote(String str) {
    if (str == null) {
        return "NULL";
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (c == '<') {
        sb.append("&lt;");
        } else if (c == '>') {
        sb.append("&gt;");
        } else if (c == '&') {
        sb.append("&amp;");
        } else {
        int x = c;
        if (x < 32 || x > 127) {
            sb.append("&#" + x + ";");
        } else {
            sb.append(c);
        }
        }
    }
    return sb.toString();
    }

    static boolean is_numeric(String str) {
    try {
        Double d = Double.valueOf(str);
    } catch (java.lang.Exception e) {
        return false;
    }
    return true;
    }

    void set_table_name(String str) {
    if (str == null) {
        tableName = "";
        return;
    }
    tableName = Shell.sql_quote(str);
    }

    public void columns(String args[]) {
    cols = args;
    }

    public void types(String args[]) {
    /* Empty body to satisfy SQLite.Callback interface. */
    }

    public boolean newrow(String args[]) {
    int i;
    String tname;
    switch (mode) {
    case Shell.MODE_Line:
        if (args.length == 0) {
        break;
        }
        if (count++ > 0) {
        pw.println("");
        }
        for (i = 0; i < args.length; i++) {
        pw.println(cols[i] + " = " +
               args[i] == null ? "NULL" : args[i]);
        }
        break;
    case Shell.MODE_Column:
        String csep = "";
        if (count++ == 0) {
        colwidth = new int[args.length];
        for (i = 0; i < args.length; i++) {
            int w, n;
            w = cols[i].length();
            if (w < 10) {
            w = 10;
            }
            colwidth[i] = w;
            if (showHeader) {
            pw.print(csep + cols[i]);
            csep = " ";
            }
        }
        if (showHeader) {
            pw.println("");
        }
        }
        if (args.length == 0) {
        break;
        }
        csep = "";
        for (i = 0; i < args.length; i++) {
        pw.print(csep + (args[i] == null ? "NULL" : args[i]));
        csep = " ";
        }
        pw.println("");
        break;
    case Shell.MODE_Semi:
    case Shell.MODE_List:
        if (count++ == 0 && showHeader) {
        for (i = 0; i < args.length; i++) {
            pw.print(cols[i] +
                 (i == args.length - 1 ? "\n" : sep));
        }
        }
        if (args.length == 0) {
        break;
        }
        for (i = 0; i < args.length; i++) {
        pw.print(args[i] == null ? "NULL" : args[i]);
        if (mode == Shell.MODE_Semi) {
            pw.print(";");
        } else if (i < args.length - 1) {
            pw.print(sep);
        }
        }
        pw.println("");
        break;
    case MODE_Html:
        if (count++ == 0 && showHeader) {
        pw.print("<TR>");
        for (i = 0; i < args.length; i++) {
            pw.print("<TH>" + html_quote(cols[i]) + "</TH>");
        }
        pw.println("</TR>");
        }
        if (args.length == 0) {
        break;
        }
        pw.print("<TR>");
        for (i = 0; i < args.length; i++) {
        pw.print("<TD>" + html_quote(args[i]) + "</TD>");
        }
        pw.println("</TR>");
        break;
    case MODE_Insert:
        if (args.length == 0) {
        break;
        }
        tname = tableName;
        if (destTable != null) {
            tname = destTable;
        }
        pw.print("INSERT INTO " + tname + " VALUES(");
        for (i = 0; i < args.length; i++) {
            String tsep = i > 0 ? "," : "";
        if (args[i] == null) {
            pw.print(tsep + "NULL");
        } else if (is_numeric(args[i])) {
            pw.print(tsep + args[i]);
        } else {
            pw.print(tsep + sql_quote(args[i]));
        }
        }
        pw.println(");");
        break;
    case MODE_Insert2:
        if (args.length == 0) {
        break;
        }
        tname = tableName;
        if (destTable != null) {
            tname = destTable;
        }
        pw.print("INSERT INTO " + tname + " VALUES(");
        for (i = 0; i < args.length; i++) {
            String tsep = i > 0 ? "," : "";
        pw.print(tsep + args[i]);
        }
        pw.println(");");
        break;
    }
    return false;
    }

    void do_meta(String line) {
        StringTokenizer st = new StringTokenizer(line.toLowerCase());
    int n = st.countTokens();
    if (n <= 0) {
        return;
    }
    String cmd = st.nextToken();
    String args[] = new String[n - 1];
    int i = 0;
    while (st.hasMoreTokens()) {
        args[i] = st.nextToken();
        ++i;
    }
    if (cmd.compareTo(".dump") == 0) {
        new DBDump(this, args);
        return;
    }
    if (cmd.compareTo(".echo") == 0) {
        if (args.length > 0 &&
        (args[0].startsWith("y") || args[0].startsWith("on"))) {
        echo = true;
        }
        return;
    }
    if (cmd.compareTo(".exit") == 0) {
        try {
        db.close();
        } catch (Exception e) {
        }
        System.exit(0);
    }
    if (cmd.compareTo(".header") == 0) {
        if (args.length > 0 &&
        (args[0].startsWith("y") || args[0].startsWith("on"))) {
        showHeader = true;
        }
        return;
    }
    if (cmd.compareTo(".help") == 0) {
        pw.println(".dump ?TABLE? ...  Dump database in text fmt");
        pw.println(".echo ON|OFF       Command echo on or off");
        pw.println(".enc ?NAME?        Change encoding");
        pw.println(".exit              Exit program");
        pw.println(".header ON|OFF     Display headers on or off");
        pw.println(".help              This message");
        pw.println(".mode MODE         Set output mode to\n" +
               "                   line, column, insert\n" +
               "                   list, or html");
        pw.println(".mode insert TABLE Generate SQL insert stmts");
        pw.println(".schema ?PATTERN?  List table schema");
        pw.println(".separator STRING  Set separator string");
        pw.println(".tables ?PATTERN?  List table names");
        return;
    }
    if (cmd.compareTo(".mode") == 0) {
        if (args.length > 0) {
        if (args[0].compareTo("line") == 0) {
            mode = Shell.MODE_Line;
        } else if (args[0].compareTo("column") == 0) {
            mode = Shell.MODE_Column;
        } else if (args[0].compareTo("list") == 0) {
            mode = Shell.MODE_List;
        } else if (args[0].compareTo("html") == 0) {
            mode = Shell.MODE_Html;
        } else if (args[0].compareTo("insert") == 0) {
            mode = Shell.MODE_Insert;
            if (args.length > 1) {
            destTable = args[1];
            }
        }
        }
        return;
    }
    if (cmd.compareTo(".separator") == 0) {
        if (args.length > 0) {
        sep = args[0];
        }
        return;
    }
    if (cmd.compareTo(".tables") == 0) {
        TableResult t = null;
        if (args.length > 0) {
        try {
            String qarg[] = new String[1];
            qarg[0] = args[0];
            t = db.get_table("SELECT name FROM sqlite_master " +
                     "WHERE type='table' AND " +
                     "name LIKE '%%%q%%' " +
                     "ORDER BY name", qarg);
        } catch (Exception e) {
            err.println("SQL Error: " + e);
            err.flush();
        }
        } else {
        try {
            t = db.get_table("SELECT name FROM sqlite_master " +
                     "WHERE type='table' ORDER BY name");
        } catch (Exception e) {
            err.println("SQL Error: " + e);
            err.flush();
        }
        }
        if (t != null) {
        for (i = 0; i < t.nrows; i++) {
            String tab = ((String[]) t.rows.elementAt(i))[0];
            if (tab != null) {
            pw.println(tab);
            }
        }
        }
        return;
    }
    if (cmd.compareTo(".schema") == 0) {
        if (args.length > 0) {
        try {
            String qarg[] = new String[1];
            qarg[0] = args[0];
            db.exec("SELECT sql FROM sqlite_master " +
                "WHERE type!='meta' AND " +
                "name LIKE '%%%q%%' AND " +
                "sql NOTNULL " +
                "ORDER BY type DESC, name",
                this, qarg);
        } catch (Exception e) {
            err.println("SQL Error: " + e);
            err.flush();
        }
        } else {
        try {
            db.exec("SELECT sql FROM sqlite_master " +
                "WHERE type!='meta' AND " +
                "sql NOTNULL " +
                "ORDER BY tbl_name, type DESC, name",
                this);
        } catch (Exception e) {
            err.println("SQL Error: " + e);
            err.flush();
        }
        }
        return;
    }
    if (cmd.compareTo(".enc") == 0) {
        try {
        db.set_encoding(args.length > 0 ? args[0] : null);
        } catch (Exception e) {
        err.println("" + e);
        err.flush();
        }
        return;
    }
    err.println("Unknown command '" + cmd + "'");
    err.flush();
    }

    String read_line(BufferedReader is, String prompt) {
    try {
        if (prompt != null) {
        pw.print(prompt);
        pw.flush();
        }
        String line = is.readLine();
        return line;
    } catch (IOException e) {
        return null;
    }
    }

    void do_input(BufferedReader is) {
    String line, sql = null;
    String prompt = "SQLITE> ";
    while ((line = read_line(is, prompt)) != null) {
        if (echo) {
        pw.println(line);
        }
        if (line.length() > 0 && line.charAt(0) == '.') {
            do_meta(line);
        } else {
        if (sql == null) {
            sql = line;
        } else {
            sql = sql + " " + line;
        }
        if (Database.complete(sql)) {
            try {
            db.exec(sql, this);
            } catch (Exception e) {
            if (!echo) {
                err.println(sql);
            }
            err.println("SQL Error: " + e);
            err.flush();
            }
            sql = null;
            prompt = "SQLITE> ";
        } else {
            prompt = "SQLITE? ";
        }
        }
        pw.flush();
    }
    if (sql != null) {
        err.println("Incomplete SQL: " + sql);
        err.flush();
    }
    }

    void do_cmd(String sql) {
        if (db == null) {
        return;
    }
        if (sql.length() > 0 && sql.charAt(0) == '.') {
        do_meta(sql);
    } else {
        try {
            db.exec(sql, this);
        } catch (Exception e) {
        err.println("SQL Error: " + e);
        err.flush();
        }
    }
    }

    public static void main(String args[]) {
    Shell s = new Shell(System.out, System.err);
    s.mode = Shell.MODE_List;
    s.sep = "|";
    s.showHeader = false;
    s.db = new Database();
    String dbname = null, sql = null;
    for (int i = 0; i < args.length; i++) {
        if(args[i].compareTo("-html") ==0) {
        s.mode = Shell.MODE_Html;
        } else if (args[i].compareTo("-list") == 0) {
        s.mode = Shell.MODE_List;
        } else if (args[i].compareTo("-line") == 0) {
        s.mode = Shell.MODE_Line;
        } else if (i < args.length - 1 &&
               args[i].compareTo("-separator") == 0) {
        ++i;
        s.sep = args[i];
        } else if (args[i].compareTo("-header") == 0) {
        s.showHeader = true;
        } else if (args[i].compareTo("-noheader") == 0) {
        s.showHeader = false;
        } else if (args[i].compareTo("-echo") == 0) {
        s.echo = true;
        } else if (dbname == null) {
        dbname = args[i];
        } else if (sql == null) {
        sql = args[i];
        } else {
        System.err.println("Arguments: ?OPTIONS? FILENAME ?SQL?");
        System.exit(1);
        }
    }
    if (dbname == null) {
        System.err.println("No database file given");
        System.exit(1);
    }
    try {
        s.db.open(dbname, 0);
    } catch (Exception e) {
        System.err.println("Unable to open database: " + e);
        System.exit(1);
    }
    if (sql != null) {
        s.do_cmd(sql);
    } else {
        // BEGIN android-modified
        BufferedReader is =
            new BufferedReader(new InputStreamReader(System.in), 8192);
        // END android-modified
        s.do_input(is);
    }
    try {
        s.db.close();
    } catch (Exception ee) {
    }
    }
}

/**
 * Internal class for dumping an entire database.
 * It contains a special callback interface to traverse the
 * tables of the current database and output create SQL statements
 * and for the data insert SQL statements.
 */

class DBDump implements Callback {
    Shell s;

    DBDump(Shell s, String tables[]) {
        this.s = s;
    s.pw.println("BEGIN TRANSACTION;");
        if (tables == null || tables.length == 0) {
        try {
            s.db.exec("SELECT name, type, sql FROM sqlite_master " +
              "WHERE type!='meta' AND sql NOT NULL " +
              "ORDER BY substr(type,2,1), name", this);
        } catch (Exception e) {
            s.err.println("SQL Error: " + e);
        s.err.flush();
        }
    } else {
        String arg[] = new String[1];
        for (int i = 0; i < tables.length; i++) {
            arg[0] = tables[i];
        try {
            s.db.exec("SELECT name, type, sql FROM sqlite_master " +
                  "WHERE tbl_name LIKE '%q' AND type!='meta' " +
                  " AND sql NOT NULL " +
                  " ORDER BY substr(type,2,1), name",
                  this, arg);
        } catch (Exception e) {
            s.err.println("SQL Error: " + e);
            s.err.flush();
        }
        }
    }
    s.pw.println("COMMIT;");
    }

    public void columns(String col[]) {
    /* Empty body to satisfy SQLite.Callback interface. */
    }

    public void types(String args[]) {
    /* Empty body to satisfy SQLite.Callback interface. */
    }

    public boolean newrow(String args[]) {
        if (args.length != 3) {
        return true;
    }
    s.pw.println(args[2] + ";");
    if (args[1].compareTo("table") == 0) {
        Shell s2 = (Shell) s.clone();
        s2.mode = Shell.MODE_Insert;
        s2.set_table_name(args[0]);
        String qargs[] = new String[1];
        qargs[0] = args[0];
        try {
            if (s2.db.is3()) {
            TableResult t = null;
            t = s2.db.get_table("PRAGMA table_info('%q')", qargs);
            String query;
            if (t != null) {
                StringBuffer sb = new StringBuffer();
            String sep = "";

            sb.append("SELECT ");
            for (int i = 0; i < t.nrows; i++) {
                String col = ((String[]) t.rows.elementAt(i))[1];
                sb.append(sep + "quote(" +
                      Shell.sql_quote_dbl(col) + ")");
                sep = ",";
            }
            sb.append(" from '%q'");
            query = sb.toString();
            s2.mode = Shell.MODE_Insert2;
            } else {
                query = "SELECT * from '%q'";
            }
            s2.db.exec(query, s2, qargs);
        } else {
            s2.db.exec("SELECT * from '%q'", s2, qargs);
        }
        } catch (Exception e) {
            s.err.println("SQL Error: " + e);
        s.err.flush();
        return true;
        }
    }
    return false;
    }
}