001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.tools;
007:
008: import java.io.PrintWriter;
009: import java.io.Writer;
010: import java.sql.Connection;
011: import java.sql.DriverManager;
012: import java.sql.ResultSet;
013: import java.sql.SQLException;
014: import java.sql.Statement;
015:
016: import org.h2.util.FileUtils;
017: import org.h2.util.IOUtils;
018: import org.h2.util.JdbcUtils;
019: import org.h2.util.StringUtils;
020:
021: /**
022: * Creates a SQL script file by extracting the schema and data of a database.
023: */
024: public class Script {
025:
026: private void showUsage() {
027: System.out
028: .println("java "
029: + getClass().getName()
030: + " -url <url> -user <user> [-password <pwd>] [-script <filename>] [-options <option> ...]");
031: System.out
032: .println("See also http://h2database.com/javadoc/org/h2/tools/Script.html");
033: }
034:
035: /**
036: * The command line interface for this tool.
037: * The options must be split into strings like this: "-user", "sa",...
038: * Options are case sensitive. The following options are supported:
039: * <ul>
040: * <li>-help or -? (print the list of options)
041: * </li><li>-url jdbc:h2:... (database URL)
042: * </li><li>-user username
043: * </li><li>-password password
044: * </li><li>-script filename (default file name is backup.sql)
045: * </li><li>-options to specify a list of options (only for H2)
046: * </li></ul>
047: *
048: * @param args the command line arguments
049: * @throws SQLException
050: */
051: public static void main(String[] args) throws SQLException {
052: new Script().run(args);
053: }
054:
055: private void run(String[] args) throws SQLException {
056: String url = null;
057: String user = null;
058: String password = "";
059: String file = "backup.sql";
060: String options1 = null, options2 = null;
061: for (int i = 0; args != null && i < args.length; i++) {
062: if (args[i].equals("-url")) {
063: url = args[++i];
064: } else if (args[i].equals("-user")) {
065: user = args[++i];
066: } else if (args[i].equals("-password")) {
067: password = args[++i];
068: } else if (args[i].equals("-script")) {
069: file = args[++i];
070: } else if (args[i].equals("-options")) {
071: StringBuffer buff1 = new StringBuffer();
072: StringBuffer buff2 = new StringBuffer();
073: i++;
074: for (; i < args.length; i++) {
075: String a = args[i];
076: String upper = StringUtils.toUpperEnglish(a);
077: if (upper.startsWith("NO") || "DROP".equals(upper)) {
078: buff1.append(' ');
079: buff1.append(args[i]);
080: } else {
081: buff2.append(' ');
082: buff2.append(args[i]);
083: }
084: }
085: options1 = buff1.toString();
086: options2 = buff2.toString();
087: } else {
088: showUsage();
089: return;
090: }
091: }
092: if (url == null || user == null || file == null) {
093: showUsage();
094: return;
095: }
096: if (options1 != null) {
097: executeScript(url, user, password, file, options1, options2);
098: } else {
099: execute(url, user, password, file);
100: }
101: }
102:
103: /**
104: * INTERNAL
105: */
106: public static void executeScript(String url, String user,
107: String password, String fileName, String options1,
108: String options2) throws SQLException {
109: Connection conn = null;
110: Statement stat = null;
111: try {
112: org.h2.Driver.load();
113: conn = DriverManager.getConnection(url, user, password);
114: stat = conn.createStatement();
115: String sql = "SCRIPT " + options1 + " TO '" + fileName
116: + "' " + options2;
117: stat.execute(sql);
118: } finally {
119: JdbcUtils.closeSilently(stat);
120: JdbcUtils.closeSilently(conn);
121: }
122: }
123:
124: /**
125: * Backs up a database to a file.
126: *
127: * @param url the database URL
128: * @param user the user name
129: * @param password the password
130: * @param fileName the script file
131: * @throws SQLException
132: */
133: public static void execute(String url, String user,
134: String password, String fileName) throws SQLException {
135: Connection conn = null;
136: Statement stat = null;
137: Writer fileWriter = null;
138: try {
139: org.h2.Driver.load();
140: conn = DriverManager.getConnection(url, user, password);
141: stat = conn.createStatement();
142: fileWriter = FileUtils.openFileWriter(fileName, false);
143: PrintWriter writer = new PrintWriter(fileWriter);
144: ResultSet rs = stat.executeQuery("SCRIPT");
145: while (rs.next()) {
146: String s = rs.getString(1);
147: writer.println(s);
148: }
149: writer.close();
150: } finally {
151: JdbcUtils.closeSilently(stat);
152: JdbcUtils.closeSilently(conn);
153: IOUtils.closeSilently(fileWriter);
154: }
155: }
156:
157: }
|