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
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.tools;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.message.DbException;
import org.h2.store.fs.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils;
import org.h2.util.Tool;
/**
* Creates a SQL script file by extracting the schema and data of a database.
* @h2.resource
*/
public class Script extends Tool {
/**
* Options are case sensitive. Supported options are:
* <table>
* <tr><td>[-help] or [-?]</td>
* <td>Print the list of options</td></tr>
* <tr><td>[-url "<url>"]</td>
* <td>The database URL (jdbc:...)</td></tr>
* <tr><td>[-user <user>]</td>
* <td>The user name (default: sa)</td></tr>
* <tr><td>[-password <pwd>]</td>
* <td>The password</td></tr>
* <tr><td>[-script <file>]</td>
* <td>The target script file name (default: backup.sql)</td></tr>
* <tr><td>[-options ...]</td>
* <td>A list of options (only for embedded H2, see SCRIPT)</td></tr>
* <tr><td>[-quiet]</td>
* <td>Do not print progress information</td></tr>
* </table>
* @h2.resource
*
* @param args the command line arguments
*/
public static void main(String... args) throws SQLException {
new Script().runTool(args);
}
public void runTool(String... args) throws SQLException {
String url = null;
String user = "sa";
String password = "";
String file = "backup.sql";
String options1 = null, options2 = null;
for (int i = 0; args != null && i < args.length; i++) {
String arg = args[i];
if (arg.equals("-url")) {
url = args[++i];
} else if (arg.equals("-user")) {
user = args[++i];
} else if (arg.equals("-password")) {
password = args[++i];
} else if (arg.equals("-script")) {
file = args[++i];
} else if (arg.equals("-options")) {
StringBuilder buff1 = new StringBuilder();
StringBuilder buff2 = new StringBuilder();
i++;
for (; i < args.length; i++) {
String a = args[i];
String upper = StringUtils.toUpperEnglish(a);
if ("SIMPLE".equals(upper) || upper.startsWith("NO") || "DROP".equals(upper)) {
buff1.append(' ');
buff1.append(args[i]);
} else {
buff2.append(' ');
buff2.append(args[i]);
}
}
options1 = buff1.toString();
options2 = buff2.toString();
} else if (arg.equals("-help") || arg.equals("-?")) {
showUsage();
return;
} else {
showUsageAndThrowUnsupportedOption(arg);
}
}
if (url == null) {
showUsage();
throw new SQLException("URL not set");
}
if (options1 != null) {
processScript(url, user, password, file, options1, options2);
} else {
execute(url, user, password, file);
}
}
private static void processScript(String url, String user, String password,
String fileName, String options1, String options2) throws SQLException {
Connection conn = null;
Statement stat = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
stat.execute(sql);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
}
}
/**
* Backs up a database to a SQL script file.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param fileName the script file
*/
public static void execute(String url, String user, String password, String fileName) throws SQLException {
OutputStream o = null;
try {
o = FileUtils.newOutputStream(fileName, false);
execute(url, user, password, o);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
IOUtils.closeSilently(o);
}
}
/**
* Backs up a database to a stream. The stream is not closed.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param out the output stream
*/
public static void execute(String url, String user, String password, OutputStream out) throws SQLException {
Connection conn = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
process(conn, out);
} finally {
JdbcUtils.closeSilently(conn);
}
}
/**
* Backs up a database to a stream. The stream is not closed.
* The connection is not closed.
*
* @param conn the connection
* @param out the output stream
*/
static void process(Connection conn, OutputStream out) throws SQLException {
Statement stat = null;
try {
stat = conn.createStatement();
PrintWriter writer = new PrintWriter(IOUtils.getBufferedWriter(out));
ResultSet rs = stat.executeQuery("SCRIPT");
while (rs.next()) {
String s = rs.getString(1);
writer.println(s);
}
writer.flush();
} finally {
JdbcUtils.closeSilently(stat);
}
}
}