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.IOException;
009: import java.io.LineNumberReader;
010: import java.io.PrintWriter;
011: import java.sql.SQLException;
012:
013: import org.h2.message.Message;
014: import org.h2.util.FileUtils;
015: import org.h2.util.IOUtils;
016:
017: /**
018: * Convert a trace file to a java class.
019: * This is required because the find command truncates lines.
020: */
021: public class ConvertTraceFile {
022:
023: private void showUsage() {
024: System.out
025: .println("java "
026: + getClass().getName()
027: + " [-traceFile <trace file name>] [-javaClass <java class name>] [-script <sql script file>]");
028: System.out
029: .println("See also http://h2database.com/javadoc/org/h2/tools/ConvertTraceFile.html");
030: }
031:
032: /**
033: * The command line interface for this tool. The options must be split into
034: * strings like this: "-traceFile", "test.trace.db",... Options are case
035: * sensitive. The following options are supported:
036: * <ul>
037: * <li>-help or -? (print the list of options) </li>
038: * <li>-traceFile filename (the default is test.trace.db) </li>
039: * <li>-script filename (the default is test.sql) </li>
040: * <li>-javaClass className (the default is Test) </li>
041: * </ul>
042: *
043: * @param args the command line arguments
044: * @throws Exception
045: */
046: public static void main(String[] args) throws SQLException {
047: new ConvertTraceFile().run(args);
048: }
049:
050: private void run(String[] args) throws SQLException {
051: String traceFile = "test.trace.db";
052: String javaClass = "Test";
053: String script = "test.sql";
054: for (int i = 0; args != null && i < args.length; i++) {
055: if (args[i].equals("-traceFile")) {
056: traceFile = args[++i];
057: } else if (args[i].equals("-javaClass")) {
058: javaClass = args[++i];
059: } else if (args[i].equals("-script")) {
060: script = args[++i];
061: } else {
062: showUsage();
063: return;
064: }
065: }
066: try {
067: convertFile(traceFile, javaClass, script);
068: } catch (IOException e) {
069: throw Message.convertIOException(e, traceFile);
070: }
071: }
072:
073: /**
074: * Converts a trace file to a Java class file and a script file.
075: *
076: * @param traceFileName
077: * @param javaClassName
078: * @throws IOException
079: */
080: private void convertFile(String traceFileName,
081: String javaClassName, String script) throws IOException,
082: SQLException {
083: LineNumberReader reader = new LineNumberReader(
084: IOUtils.getReader(FileUtils
085: .openFileInputStream(traceFileName)));
086: PrintWriter javaWriter = new PrintWriter(FileUtils
087: .openFileWriter(javaClassName + ".java", false));
088: PrintWriter scriptWriter = new PrintWriter(FileUtils
089: .openFileWriter(script, false));
090: javaWriter.println("import java.io.*;");
091: javaWriter.println("import java.sql.*;");
092: javaWriter.println("import java.math.*;");
093: javaWriter.println("import java.util.Calendar;");
094: javaWriter.println("public class " + javaClassName + " {");
095: javaWriter
096: .println(" public static void main(String[] args) throws Exception {");
097: javaWriter.println(" Class.forName(\"org.h2.Driver\");");
098: while (true) {
099: String line = reader.readLine();
100: if (line == null) {
101: break;
102: }
103: if (line.startsWith("/**/")) {
104: line = " " + line.substring(4);
105: javaWriter.println(line);
106: } else if (line.startsWith("/*SQL*/")) {
107: line = line.substring("/*SQL*/".length());
108: scriptWriter.println(line);
109: }
110: }
111: javaWriter.println(" }");
112: javaWriter.println("}");
113: reader.close();
114: javaWriter.close();
115: scriptWriter.close();
116: }
117:
118: }
|