EMMA Coverage Report (generated Sun Mar 01 22:06:14 CET 2015)
[all classes][org.h2.tools]

COVERAGE SUMMARY FOR SOURCE FILE [Script.java]

nameclass, %method, %block, %line, %
Script.java100% (1/1)100% (5/5)88%  (225/257)90%  (56.1/62)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Script100% (1/1)100% (5/5)88%  (225/257)90%  (56.1/62)
process (String, String, String, String, String, String): void 100% (1/1)78%  (18/23)97%  (6.8/7)
process (Connection, String, String, String): void 100% (1/1)86%  (30/35)98%  (6.8/7)
runTool (String []): void 100% (1/1)88%  (168/190)88%  (39.5/45)
Script (): void 100% (1/1)100% (3/3)100% (1/1)
main (String []): void 100% (1/1)100% (6/6)100% (2/2)

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

[all classes][org.h2.tools]
EMMA 2.0.5312 (C) Vladimir Roubtsov