Debian Codegraph @

src/main/org/h2/tools/Script.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.tools;
7
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import org.h2.util.JdbcUtils;
12 import org.h2.util.StringUtils;
13 import org.h2.util.Tool;
14
15 /**
16 * Creates a SQL script file by extracting the schema and data of a database.
17 */
18 public class Script extends Tool {
19
20 /**
21 * Options are case sensitive.
22 * <table>
23 * <caption>Supported options</caption>
24 * <tr><td>[-help] or [-?]</td>
25 * <td>Print the list of options</td></tr>
26 * <tr><td>[-url "&lt;url&gt;"]</td>
27 * <td>The database URL (jdbc:...)</td></tr>
28 * <tr><td>[-user &lt;user&gt;]</td>
29 * <td>The user name (default: sa)</td></tr>
30 * <tr><td>[-password &lt;pwd&gt;]</td>
31 * <td>The password</td></tr>
32 * <tr><td>[-script &lt;file&gt;]</td>
33 * <td>The target script file name (default: backup.sql)</td></tr>
34 * <tr><td>[-options ...]</td>
35 * <td>A list of options (only for embedded H2, see SCRIPT)</td></tr>
36 * <tr><td>[-quiet]</td>
37 * <td>Do not print progress information</td></tr>
38 * </table>
39 *
40 * @param args the command line arguments
41 * @throws SQLException on failure
42 */
43 public static void main(String... args) throws SQLException {
44 new Script().runTool(args);
45 }
46
47 @Override
48 public void runTool(String... args) throws SQLException {
49 String url = null;
50 String user = "";
51 String password = "";
52 String file = "backup.sql";
53 String options1 = "";
54 String options2 = "";
55 for (int i = 0; args != null && i < args.length; i++) {
56 String arg = args[i];
57 if (arg.equals("-url")) {
58 url = args[++i];
59 } else if (arg.equals("-user")) {
60 user = args[++i];
61 } else if (arg.equals("-password")) {
62 password = args[++i];
63 } else if (arg.equals("-script")) {
64 file = args[++i];
65 } else if (arg.equals("-options")) {
66 StringBuilder buff1 = new StringBuilder();
67 StringBuilder buff2 = new StringBuilder();
68 i++;
69 for (; i < args.length; i++) {
70 String a = args[i];
71 String upper = StringUtils.toUpperEnglish(a);
72 if ("SIMPLE".equals(upper) || upper.startsWith("NO") || "DROP".equals(upper)) {
73 buff1.append(' ');
74 buff1.append(args[i]);
75 } else if ("BLOCKSIZE".equals(upper)) {
76 buff1.append(' ');
77 buff1.append(args[i]);
78 i++;
79 buff1.append(' ');
80 buff1.append(args[i]);
81 } else {
82 buff2.append(' ');
83 buff2.append(args[i]);
84 }
85 }
86 options1 = buff1.toString();
87 options2 = buff2.toString();
88 } else if (arg.equals("-help") || arg.equals("-?")) {
89 showUsage();
90 return;
91 } else {
92 showUsageAndThrowUnsupportedOption(arg);
93 }
94 }
95 if (url == null) {
96 showUsage();
97 throw new SQLException("URL not set");
98 }
99 process(url, user, password, file, options1, options2);
100 }
101
102 /**
103 * Backs up a database to a stream.
104 *
105 * @param url the database URL
106 * @param user the user name
107 * @param password the password
108 * @param fileName the target file name
109 * @param options1 the options before the file name (may be an empty string)
110 * @param options2 the options after the file name (may be an empty string)
111 * @throws SQLException on failure
112 */
113 public static void process(String url, String user, String password, String fileName, String options1,
114 String options2) throws SQLException {
115 try (Connection conn = JdbcUtils.getConnection(null, url, user, password)) {
116 process(conn, fileName, options1, options2);
117 }
118 }
119
120 /**
121 * Backs up a database to a stream. The stream is not closed.
122 * The connection is not closed.
123 *
124 * @param conn the connection
125 * @param fileName the target file name
126 * @param options1 the options before the file name
127 * @param options2 the options after the file name
128 * @throws SQLException on failure
129 */
130 public static void process(Connection conn,
131 String fileName, String options1, String options2) throws SQLException {
132
133 try (Statement stat = conn.createStatement()) {
134 String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
135 stat.execute(sql);
136 }
137 }
138
139 }