001: package org.julp.util.db;
002:
003: import java.util.*;
004: import java.io.*;
005: import java.sql.*;
006: import javax.sql.*;
007: import org.julp.*;
008:
009: public class SQLScriptExecutor {
010:
011: protected String delimiter = ";";
012:
013: /** Executes SQL script(s) from file */
014: public SQLScriptExecutor() {
015: }
016:
017: public void execute(DataSource dataSource, String filePath)
018: throws SQLException {
019: execute(dataSource.getConnection(), filePath, this .delimiter);
020: }
021:
022: public void execute(DataSource dataSource, String filePath,
023: String delimiter) throws SQLException {
024: execute(dataSource.getConnection(), filePath, delimiter);
025: }
026:
027: public void execute(Connection conn, String filePath)
028: throws SQLException {
029: execute(conn, filePath, this .delimiter);
030: }
031:
032: public void execute(Connection conn, String filePath,
033: String delimiter) throws SQLException {
034:
035: if (delimiter != null && delimiter.trim().length() > 0) {
036: this .delimiter = delimiter;
037: }
038:
039: File sqlScript = new File(filePath);
040: if (!sqlScript.exists()) {
041: throw new IllegalArgumentException("File " + filePath
042: + " does not exist");
043: }
044:
045: org.julp.DBServices dbServices = new org.julp.DBServices();
046: dbServices.setConnection(conn);
047:
048: BufferedReader in = null;
049: StringBuffer sb = new StringBuffer();
050: try {
051: in = new BufferedReader(new FileReader(filePath));
052: String str;
053: while ((str = in.readLine()) != null) {
054: sb.append(str);
055: }
056: } catch (IOException e) {
057: throw new RuntimeException(e);
058: } finally {
059: try {
060: in.close();
061: } catch (IOException e) {
062: throw new RuntimeException(e);
063: }
064: }
065:
066: try {
067: StringTokenizer st = new StringTokenizer(sb.toString(),
068: this .delimiter, false);
069: if (conn.getMetaData().supportsBatchUpdates()) {
070: Collection batch = new ArrayList();
071: while (st.hasMoreTokens()) {
072: String sql = st.nextToken();
073: batch.add(sql);
074: }
075: dbServices.beginTran();
076: dbServices.executeBatch(batch);
077: } else {
078: dbServices.setCacheStatements(true);
079: dbServices.beginTran();
080: while (st.hasMoreTokens()) {
081: String sql = st.nextToken();
082: dbServices.execute(sql);
083: }
084: }
085: dbServices.commitTran();
086: } catch (Exception e) {
087: e.printStackTrace();
088: try {
089: dbServices.rollbackTran();
090: } catch (SQLException sqle) {
091: throw new RuntimeException(sqle);
092: }
093: throw new RuntimeException(e);
094: } finally {
095: try {
096: dbServices.release(true);
097: } catch (Exception e) {
098: e.printStackTrace();
099: throw new RuntimeException(e);
100: }
101: }
102: }
103:
104: public void execute(String driver, String dbURL,
105: Properties connectionProperties, String filePath,
106: String delimiter) throws SQLException {
107: BasicDataSourceImpl dataSource = new BasicDataSourceImpl();
108: dataSource.setDbURL(dbURL);
109: dataSource.setDriverName(driver);
110: dataSource.setConnectionProperties(connectionProperties);
111: execute(dataSource, filePath, delimiter);
112: }
113:
114: public void execute(String driver, String dbURL, String user,
115: String password, String optionalConnProps, String filePath)
116: throws SQLException {
117: execute(driver, dbURL, user, password, optionalConnProps,
118: filePath, this .delimiter);
119: }
120:
121: public void execute(String driver, String dbURL, String user,
122: String password, String optionalConnProps, String filePath,
123: String delimiter) throws SQLException {
124:
125: if (driver == null || driver.trim().equals("")) {
126: throw new IllegalArgumentException("Driver name is missing");
127: }
128: if (user == null || user.trim().equals("")) {
129: throw new IllegalArgumentException("User name is missing");
130: }
131: if (dbURL == null || dbURL.trim().equals("")) {
132: throw new IllegalArgumentException("DbURL is missing");
133: }
134: Properties prop = new Properties();
135: prop.setProperty("user", user);
136: prop.setProperty("password", password);
137: if (optionalConnProps != null
138: && optionalConnProps.trim().length() > 0) {
139: StringTokenizer st = new StringTokenizer(optionalConnProps,
140: ",", false);
141: while (st.hasMoreTokens()) {
142: String token = st.nextToken();
143: int idx = token.indexOf("=");
144: if (idx == -1) {
145: throw new IllegalArgumentException(
146: "Invalid optional connection properties format. \nIt must have format: name1=value1, name2=value2, ...");
147: }
148: String name = token.substring(0, idx);
149: String value = token.substring(idx + 1);
150: prop.setProperty(name, value);
151: }
152: }
153: execute(driver, dbURL, prop, filePath, delimiter);
154: }
155: }
|