PageRenderTime 22ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/hsqldb/src/org/hsqldb/util/ScriptTool.java

https://bitbucket.org/pedpresn/gpr
Java | 293 lines | 172 code | 56 blank | 65 comment | 26 complexity | e318d66af4e40a58ebfe02253d550d76 MD5 | raw file
  1. /* Copyright (c) 2001-2005, The HSQL Development Group
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the HSQL Development Group nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package org.hsqldb.util;
  31. import java.io.BufferedReader;
  32. import java.io.FileReader;
  33. import java.sql.Connection;
  34. import java.sql.DriverManager;
  35. import java.sql.ResultSet;
  36. import java.sql.ResultSetMetaData;
  37. import java.sql.SQLException;
  38. import java.sql.Statement;
  39. import java.util.Properties;
  40. import org.hsqldb.lib.java.JavaSystem;
  41. // fredt@users 20011220 - patch 481239 by yfl@users - new class
  42. // jrmaher@users 20020710 - support for batch mode
  43. /**
  44. * Script tool - command line tool to read in sql script and execute it.
  45. *
  46. *
  47. * @version 1.7.0
  48. */
  49. public class ScriptTool {
  50. private static Properties pProperties = new Properties();
  51. private Connection cConn;
  52. private Statement sStatement;
  53. private boolean BATCH = true;
  54. private String EKW = new String("go");
  55. private boolean EOF = false;
  56. private int ln = 0;
  57. /**
  58. * Main method
  59. *
  60. *
  61. * @param arg
  62. */
  63. public static void main(String[] arg) {
  64. for (int i = 0; i < arg.length; i++) {
  65. String p = arg[i];
  66. if (p.equals("-?")) {
  67. printHelp();
  68. System.exit(0);
  69. }
  70. }
  71. ScriptTool tool = new ScriptTool();
  72. tool.execute(arg);
  73. System.exit(0);
  74. } // end main
  75. public void execute(String[] arg) {
  76. for (int i = 0; i < arg.length; i++) {
  77. String p = arg[i];
  78. if (p.charAt(0) == '-') {
  79. pProperties.put(p.substring(1), arg[i + 1]);
  80. i++;
  81. }
  82. }
  83. ln = 0;
  84. EOF = false;
  85. BufferedReader in = null;
  86. Properties p = pProperties;
  87. String driver = p.getProperty("driver", "org.hsqldb.jdbcDriver");
  88. String url = p.getProperty("url", "jdbc:hsqldb:");
  89. String database = p.getProperty("database", "test");
  90. String user = p.getProperty("user", "sa");
  91. String password = p.getProperty("password", "");
  92. String script = p.getProperty("script", "st.sql");
  93. boolean log = p.getProperty("log", "false").equalsIgnoreCase("true");
  94. BATCH = p.getProperty("batch", "true").equalsIgnoreCase("true");
  95. try {
  96. if (log) {
  97. trace("driver = " + driver);
  98. trace("url = " + url);
  99. trace("database = " + database);
  100. trace("user = " + user);
  101. trace("password = " + password);
  102. trace("script = " + script);
  103. trace("log = " + log);
  104. trace("batch = " + BATCH);
  105. JavaSystem.setLogToSystem(true);
  106. }
  107. // As described in the JDBC FAQ:
  108. // http://java.sun.com/products/jdbc/jdbc-frequent.html;
  109. // Why doesn't calling class.forName() load my JDBC driver?
  110. // There is a bug in the JDK 1.1.x that can cause Class.forName() to fail.
  111. // new org.hsqldb.jdbcDriver();
  112. Class.forName(driver).newInstance();
  113. cConn = DriverManager.getConnection(url + database, user,
  114. password);
  115. in = new BufferedReader(new FileReader(script));
  116. } catch (Exception e) {
  117. System.out.println("ScriptTool.init error: " + e.getMessage());
  118. e.printStackTrace();
  119. }
  120. try {
  121. sStatement = cConn.createStatement();
  122. String sql;
  123. while ((sql = fileToString(in)) != null) {
  124. if (sql.length() == 1) {
  125. continue;
  126. }
  127. if (log) {
  128. trace("SQL (" + ln + ") : "
  129. + sql.substring(0, sql.length() - 2));
  130. }
  131. sStatement.execute(sql);
  132. ResultSet results = sStatement.getResultSet();
  133. int updateCount = sStatement.getUpdateCount();
  134. if (updateCount == -1) {
  135. trace(toString(results));
  136. } else {
  137. trace("update count " + updateCount);
  138. }
  139. }
  140. } catch (SQLException e) {
  141. System.out.println("SQL Error at line " + ln + ": " + e);
  142. }
  143. try {
  144. cConn.close();
  145. in.close();
  146. } catch (Exception ce) {}
  147. }
  148. /**
  149. * Translate ResultSet to String representation
  150. * @param r
  151. */
  152. private String toString(ResultSet r) {
  153. try {
  154. if (r == null) {
  155. return "No Result";
  156. }
  157. ResultSetMetaData m = r.getMetaData();
  158. int col = m.getColumnCount();
  159. StringBuffer strbuf = new StringBuffer();
  160. for (int i = 1; i <= col; i++) {
  161. strbuf = strbuf.append(m.getColumnLabel(i) + "\t");
  162. }
  163. strbuf = strbuf.append("\n");
  164. while (r.next()) {
  165. for (int i = 1; i <= col; i++) {
  166. strbuf = strbuf.append(r.getString(i) + "\t");
  167. if (r.wasNull()) {
  168. strbuf = strbuf.append("(null)\t");
  169. }
  170. }
  171. strbuf = strbuf.append("\n");
  172. }
  173. return strbuf.toString();
  174. } catch (SQLException e) {
  175. return null;
  176. }
  177. }
  178. /**
  179. * Read file and convert it to string.
  180. */
  181. private String fileToString(BufferedReader in) {
  182. if (EOF) {
  183. return null;
  184. }
  185. EOF = true;
  186. StringBuffer a = new StringBuffer();
  187. try {
  188. String line;
  189. while ((line = in.readLine()) != null) {
  190. ln = ln + 1;
  191. if (BATCH) {
  192. if (line.startsWith("print ")) {
  193. trace("\n" + line.substring(5));
  194. continue;
  195. }
  196. if (line.equalsIgnoreCase(EKW)) {
  197. EOF = false;
  198. break;
  199. }
  200. }
  201. a.append(line);
  202. a.append('\n');
  203. }
  204. a.append('\n');
  205. return a.toString();
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. throw new RuntimeException(e.getMessage());
  209. }
  210. }
  211. /**
  212. * Method declaration
  213. *
  214. *
  215. * @param s
  216. */
  217. private void trace(String s) {
  218. System.out.println(s);
  219. }
  220. /**
  221. * Method declaration
  222. *
  223. */
  224. private static void printHelp() {
  225. System.out.println(
  226. "Usage: java ScriptTool [-options]\n"
  227. + "where options include:\n"
  228. + " -driver <classname> name of the driver class\n"
  229. + " -url <name> first part of the jdbc url\n"
  230. + " -database <name> second part of the jdbc url\n"
  231. + " -user <name> username used for connection\n"
  232. + " -password <name> password for this user\n"
  233. + " -log <true/false> write log to system out\n"
  234. + " -batch <true/false> allow go/print pseudo statements\n"
  235. + " -script <script file> reads from script file\n");
  236. }
  237. }