| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.test; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.springframework.core.io.DefaultResourceLoader; |
| 21 | |
import org.springframework.jdbc.core.ConnectionCallback; |
| 22 | |
import org.springframework.jdbc.core.JdbcTemplate; |
| 23 | |
import org.springframework.transaction.TransactionStatus; |
| 24 | |
import org.springframework.transaction.support.TransactionCallback; |
| 25 | |
import org.springframework.transaction.support.TransactionTemplate; |
| 26 | |
|
| 27 | |
import java.io.BufferedReader; |
| 28 | |
import java.io.InputStreamReader; |
| 29 | |
import java.sql.Connection; |
| 30 | |
import java.sql.SQLException; |
| 31 | |
import java.sql.Statement; |
| 32 | |
|
| 33 | 0 | public class SQLDataLoader { |
| 34 | |
|
| 35 | 0 | private static final Logger LOG = Logger.getLogger(SQLDataLoader.class); |
| 36 | |
public static final String SQL_LINE_COMMENT_PREFIX = "--"; |
| 37 | |
|
| 38 | |
private String fileLoc; |
| 39 | |
private String seperatorChar; |
| 40 | |
private String statement; |
| 41 | |
|
| 42 | 0 | public SQLDataLoader(String statement) { |
| 43 | 0 | this.fileLoc = null; |
| 44 | 0 | this.seperatorChar = null; |
| 45 | 0 | this.statement = statement; |
| 46 | 0 | } |
| 47 | |
|
| 48 | 0 | public SQLDataLoader(String fileLoc, String seperatorChar) { |
| 49 | 0 | this.fileLoc = fileLoc; |
| 50 | 0 | this.seperatorChar = seperatorChar; |
| 51 | 0 | this.statement = null; |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
public void runSql() throws Exception { |
| 55 | 0 | String[] sqlStatements = null; |
| 56 | 0 | if (statement == null) { |
| 57 | 0 | String sqlStatementsContent = getContentsAsString(fileLoc); |
| 58 | |
|
| 59 | |
|
| 60 | 0 | sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$"); |
| 61 | 0 | } else { |
| 62 | 0 | sqlStatements = new String[]{statement}; |
| 63 | |
} |
| 64 | 0 | final String[] finalSqlStatements = sqlStatements; |
| 65 | 0 | new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() { |
| 66 | |
public Object doInTransaction(TransactionStatus status) { |
| 67 | 0 | return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() { |
| 68 | |
public Object doInConnection(Connection connection) throws SQLException { |
| 69 | 0 | Statement statement = connection.createStatement(); |
| 70 | 0 | LOG.info("################################"); |
| 71 | 0 | LOG.info(fileLoc != null ? "#" + fileLoc : "#"); |
| 72 | 0 | LOG.info("#"); |
| 73 | 0 | for (String sqlStatement : finalSqlStatements) { |
| 74 | 0 | if (StringUtils.isNotBlank(sqlStatement)) { |
| 75 | 0 | LOG.info("# Executing sql statement ->" + sqlStatement + "<-"); |
| 76 | 0 | statement.execute(sqlStatement); |
| 77 | |
} |
| 78 | |
} |
| 79 | 0 | LOG.info("#"); |
| 80 | 0 | LOG.info("#"); |
| 81 | 0 | LOG.info("################################"); |
| 82 | 0 | statement.close(); |
| 83 | 0 | return null; |
| 84 | |
} |
| 85 | |
}); |
| 86 | |
} |
| 87 | |
}); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
private String getContentsAsString(String fileLoc) throws Exception { |
| 91 | 0 | DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); |
| 92 | 0 | String data = ""; |
| 93 | 0 | BufferedReader reader = null; |
| 94 | |
try { |
| 95 | 0 | reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream())); |
| 96 | 0 | String line = ""; |
| 97 | 0 | while ((line = reader.readLine()) != null) { |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 0 | if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) { |
| 102 | 0 | data += line + "\r\n "; |
| 103 | |
} |
| 104 | |
} |
| 105 | |
} finally { |
| 106 | 0 | if (reader != null) { |
| 107 | |
try { |
| 108 | 0 | reader.close(); |
| 109 | 0 | } catch (Exception e) { |
| 110 | 0 | LOG.error(e); |
| 111 | 0 | } |
| 112 | |
} |
| 113 | |
|
| 114 | |
} |
| 115 | 0 | return data; |
| 116 | |
} |
| 117 | |
|
| 118 | |
public String getSeperatorChar() { |
| 119 | 0 | if (this.seperatorChar == null) { |
| 120 | 0 | return ";"; |
| 121 | |
} |
| 122 | 0 | return seperatorChar; |
| 123 | |
} |
| 124 | |
|
| 125 | |
} |