File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\Script.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 import java.io.OutputStream;
10 import java.io.PrintWriter;
11 import java.sql.Connection;
12 import java.sql.DriverManager;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16 import org.h2.store.fs.FileUtils;
17 import org.h2.util.IOUtils;
18 import org.h2.util.JdbcUtils;
19 import org.h2.util.StringUtils;
20 import org.h2.util.Tool;
21
22 /**
23 * Creates a SQL script file by extracting the schema and data of a database.
24 * @h2.resource
25 */
26 public class Script extends Tool {
27
28 /**
29 * Options are case sensitive. Supported options are:
30 * <table>
31 * <tr><td>[-help] or [-?]</td>
32 * <td>Print the list of options</td></tr>
33 * <tr><td>[-url "&lt;url&gt;"]</td>
34 * <td>The database URL (jdbc:...)</td></tr>
35 * <tr><td>[-user &lt;user&gt;]</td>
36 * <td>The user name (default: sa)</td></tr>
37 * <tr><td>[-password &lt;pwd&gt;]</td>
38 * <td>The password</td></tr>
39 * <tr><td>[-script &lt;file&gt;]</td>
40 * <td>The target script file name (default: backup.sql)</td></tr>
41 * <tr><td>[-options ...]</td>
42 * <td>A list of options (only for embedded H2, see SCRIPT)</td></tr>
43 * <tr><td>[-quiet]</td>
44 * <td>Do not print progress information</td></tr>
45 * </table>
46 * @h2.resource
47 *
48 * @param args the command line arguments
49 */
50 public static void main(String... args) throws SQLException {
51 new Script().runTool(args);
52 }
53
54 public void runTool(String... args) throws SQLException {
55 String url = null;
56 String user = "sa";
57 String password = "";
58 String file = "backup.sql";
59 String options1 = null, options2 = null;
60 for (int i = 0; args != null && i < args.length; i++) {
61 String arg = args[i];
62 if (arg.equals("-url")) {
63 url = args[++i];
64 } else if (arg.equals("-user")) {
65 user = args[++i];
66 } else if (arg.equals("-password")) {
67 password = args[++i];
68 } else if (arg.equals("-script")) {
69 file = args[++i];
70 } else if (arg.equals("-options")) {
71 StringBuilder buff1 = new StringBuilder();
72 StringBuilder buff2 = new StringBuilder();
73 i++;
74 for (; i < args.length; i++) {
75 String a = args[i];
76 String upper = StringUtils.toUpperEnglish(a);
77 if ("SIMPLE".equals(upper) || upper.startsWith("NO") || "DROP".equals(upper)) {
78 buff1.append(' ');
79 buff1.append(args[i]);
80 } else {
81 buff2.append(' ');
82 buff2.append(args[i]);
83 }
84 }
85 options1 = buff1.toString();
86 options2 = buff2.toString();
87 } else if (arg.equals("-help") || arg.equals("-?")) {
88 showUsage();
89 return;
90 } else {
91 showUsageAndThrowUnsupportedOption(arg);
92 }
93 }
94 if (url == null) {
95 showUsage();
96 throw new SQLException("URL not set");
97 }
98 if (options1 != null) {
99 processScript(url, user, password, file, options1, options2);
100 } else {
101 execute(url, user, password, file);
102 }
103 }
104
105 private static void processScript(String url, String user, String password,
106 String fileName, String options1, String options2) throws SQLException {
107 Connection conn = null;
108 Statement stat = null;
109 try {
110 org.h2.Driver.load();
111 conn = DriverManager.getConnection(url, user, password);
112 stat = conn.createStatement();
113 String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
114 stat.execute(sql);
115 } finally {
116 JdbcUtils.closeSilently(stat);
117 JdbcUtils.closeSilently(conn);
118 }
119 }
120
121 /**
122 * Backs up a database to a SQL script file.
123 *
124 * @param url the database URL
125 * @param user the user name
126 * @param password the password
127 * @param fileName the script file
128 */
129 public static void execute(String url, String user, String password, String fileName) throws SQLException {
130 OutputStream o = null;
131 try {
132 o = FileUtils.newOutputStream(fileName, false);
133 execute(url, user, password, o);
134 } finally {
135 IOUtils.closeSilently(o);
136 }
137 }
138
139
140 /**
141 * Backs up a database to a stream. The stream is not closed.
142 *
143 * @param url the database URL
144 * @param user the user name
145 * @param password the password
146 * @param out the output stream
147 */
148 public static void execute(String url, String user, String password, OutputStream out) throws SQLException {
149 Connection conn = null;
150 try {
151 org.h2.Driver.load();
152 conn = DriverManager.getConnection(url, user, password);
153 process(conn, out);
154 } finally {
155 JdbcUtils.closeSilently(conn);
156 }
157 }
158
159
160 /**
161 * Backs up a database to a stream. The stream is not closed.
162 * The connection is not closed.
163 *
164 * @param conn the connection
165 * @param out the output stream
166 */
167 static void process(Connection conn, OutputStream out) throws SQLException {
168 Statement stat = null;
169 try {
170 stat = conn.createStatement();
171 PrintWriter writer = new PrintWriter(IOUtils.getBufferedWriter(out));
172 ResultSet rs = stat.executeQuery("SCRIPT");
173 while (rs.next()) {
174 String s = rs.getString(1);
175 writer.println(s);
176 }
177 writer.flush();
178 } finally {
179 JdbcUtils.closeSilently(stat);
180 }
181 }
182
183 }
184