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