You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQLExec.java 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import java.io.*;
  57. import java.util.Enumeration;
  58. import java.util.StringTokenizer;
  59. import java.util.Vector;
  60. import java.util.zip.*;
  61. import java.sql.*;
  62. /**
  63. * Reads in a text file containing SQL statements seperated with semicolons
  64. * and executes it in a given db.
  65. * Both -- and // maybe used as comments.
  66. *
  67. * @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a>
  68. */
  69. public class SQLExec extends Task {
  70. /**
  71. * Database connection
  72. */
  73. private Connection conn = null;
  74. /**
  75. * Autocommit flag. Default value is false
  76. */
  77. private boolean autocommit=false;
  78. /**
  79. * SQL statement
  80. */
  81. private Statement statement = null;
  82. /**
  83. * DB driver.
  84. */
  85. private String driver = null;
  86. /**
  87. * DB url.
  88. */
  89. private String url = null;
  90. /**
  91. * User name.
  92. */
  93. private String userId = null;
  94. /**
  95. * Password
  96. */
  97. private String password = null;
  98. /**
  99. * SQL input file
  100. */
  101. private File srcFile = null;
  102. /**
  103. * SQL input command
  104. */
  105. private String sqlCommand = "";
  106. /**
  107. * Set the name of the sql file to be run.
  108. */
  109. public void setSrc(File srcFile) {
  110. this.srcFile = srcFile;
  111. }
  112. /**
  113. * Set the name of the sql file to be run.
  114. */
  115. public void addText(String sql) {
  116. this.sqlCommand += sql;
  117. }
  118. /**
  119. * Set the JDBC driver to be used.
  120. */
  121. public void setDriver(String driver) {
  122. this.driver = driver;
  123. }
  124. /**
  125. * Set the DB connection url.
  126. */
  127. public void setUrl(String url) {
  128. this.url = url;
  129. }
  130. /**
  131. * Set the user name for the DB connection.
  132. */
  133. public void setUserid(String userId) {
  134. this.userId = userId;
  135. }
  136. /**
  137. * Set the password for the DB connection.
  138. */
  139. public void setPassword(String password) {
  140. this.password = password;
  141. }
  142. /**
  143. * Set the autocommit flag for the DB connection.
  144. */
  145. public void setAutocommit(boolean autocommit) {
  146. this.autocommit = autocommit;
  147. }
  148. /**
  149. * Load the sql file and then execute it
  150. */
  151. public void execute() throws BuildException {
  152. Connection conn = null;
  153. sqlCommand = sqlCommand.trim();
  154. if (srcFile == null && sqlCommand.length() == 0) {
  155. throw new BuildException("Source file or sql statement must be set!", location);
  156. }
  157. if (driver == null) {
  158. throw new BuildException("Driver attribute must be set!", location);
  159. }
  160. if (userId == null) {
  161. throw new BuildException("User Id attribute must be set!", location);
  162. }
  163. if (password == null) {
  164. throw new BuildException("Password attribute must be set!", location);
  165. }
  166. if (url == null) {
  167. throw new BuildException("Url attribute must be set!", location);
  168. }
  169. if (srcFile != null && !srcFile.exists()) {
  170. throw new BuildException("Source file does not exist!", location);
  171. }
  172. try{
  173. Class.forName(driver);
  174. }catch(ClassNotFoundException e){
  175. throw new BuildException("JDBC driver " + driver + " could not be loaded", location);
  176. }
  177. try{
  178. conn.setAutoCommit(autocommit);
  179. log("connecting to " + url, Project.MSG_VERBOSE );
  180. conn = DriverManager.getConnection(url, userId, password);
  181. statement = conn.createStatement();
  182. if (sqlCommand.length() != 0) {
  183. runStatements(new StringReader(sqlCommand));
  184. }
  185. if (srcFile != null) {
  186. runStatements(new FileReader(srcFile));
  187. }
  188. if (!autocommit) {
  189. conn.commit();
  190. }
  191. } catch(IOException e){
  192. if (!autocommit) {
  193. try {
  194. conn.rollback();
  195. } catch (SQLException ex) {}
  196. }
  197. throw new BuildException(e, location);
  198. } catch(SQLException e){
  199. if (!autocommit) {
  200. try {
  201. conn.rollback();
  202. } catch (SQLException ex) {}
  203. }
  204. throw new BuildException(e, location);
  205. }
  206. finally {
  207. try {
  208. if (statement != null) {
  209. statement.close();
  210. }
  211. if (conn != null) {
  212. conn.close();
  213. }
  214. }
  215. catch (SQLException e) {}
  216. }
  217. log("SQL statements executed successfully", Project.MSG_VERBOSE);
  218. }
  219. private void runStatements(Reader reader) throws SQLException, IOException {
  220. String sql = "";
  221. String line = "";
  222. BufferedReader in = new BufferedReader(reader);
  223. try{
  224. while ((line=in.readLine()) != null){
  225. if (line.trim().startsWith("//")) continue;
  226. if (line.trim().startsWith("--")) continue;
  227. sql += " " + line;
  228. if (sql.trim().endsWith(";")){
  229. log("SQL: " + sql, Project.MSG_VERBOSE);
  230. execSQL(sql.substring(0, sql.length()-1));
  231. sql = "";
  232. }
  233. }
  234. // Catch any statements not followed by ;
  235. if(!sql.trim().equals("")){
  236. execSQL(sql);
  237. }
  238. }catch(SQLException e){
  239. log("Failed to execute: " + sql, Project.MSG_ERR);
  240. throw e;
  241. }
  242. }
  243. /**
  244. * Exec the sql statement.
  245. */
  246. private void execSQL(String sql) throws SQLException{
  247. if (!statement.execute(sql)) {
  248. log(statement.getUpdateCount()+" rows affected",
  249. Project.MSG_VERBOSE);
  250. }
  251. SQLWarning warning = conn.getWarnings();
  252. while(warning!=null){
  253. log(warning + " sql warnging", Project.MSG_VERBOSE);
  254. warning=warning.getNextWarning();
  255. }
  256. conn.clearWarnings();
  257. }
  258. }