File: SQLDump.java

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 158,500 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (96 lines) | stat: -rw-r--r-- 2,196 bytes parent folder | download | duplicates (7)
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
package SQLite;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * SQLite SQL dump utility.
 */

public class SQLDump implements Callback {
    PrintWriter pw;
    PrintWriter err;
    Database db;
    Shell s;

    public SQLDump(PrintWriter pw, Database db) {
	this.pw = pw;
	this.err = this.pw;
	this.db = db;
	s = new Shell(this.pw, this.err);
	s.mode = Shell.MODE_Insert;
	s.db = db;
    }

    public SQLDump(PrintStream ps, Database db) {
	this.pw = new PrintWriter(ps);
	this.err = this.pw;
	this.db = db;
	s = new Shell(this.pw, this.err);
	s.mode = Shell.MODE_Insert;
	s.db = db;
    }

    public void dump() throws SQLite.Exception {
	pw.println("BEGIN TRANSACTION;");
	db.exec("SELECT name, type, sql FROM sqlite_master " +
		"WHERE type!='meta' AND sql NOT NULL " +
		"ORDER BY substr(type,2,1), name", this);
	pw.println("COMMIT;");
	pw.flush();
    }

    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;
	}
	pw.println(args[2] + ";");
	if (args[1].compareTo("table") == 0) {
	    s.mode = Shell.MODE_Insert;
	    s.set_table_name(args[0]);
	    String qargs[] = new String[1];
	    qargs[0] = args[0];
	    try {
		if (s.db.is3()) {
		    TableResult t = null;
		    t = s.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();
			s.mode = Shell.MODE_Insert2;
		    } else {
			query = "SELECT * from '%q'";
		    }
		    s.db.exec(query, s, qargs);
		    pw.flush();
		} else {
		    s.db.exec("SELECT * from '%q'", s, qargs);
		    pw.flush();
		}
	    } catch (Exception e) {
		return true;
	    }
	}
	s.mode = Shell.MODE_Line;
	return false;
    }
}