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.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
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 public class SQLDataLoader {
34
35 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 public SQLDataLoader(String statement) {
43 this.fileLoc = null;
44 this.seperatorChar = null;
45 this.statement = statement;
46 }
47
48 public SQLDataLoader(String fileLoc, String seperatorChar) {
49 this.fileLoc = fileLoc;
50 this.seperatorChar = seperatorChar;
51 this.statement = null;
52 }
53
54 public void runSql() throws Exception {
55 String[] sqlStatements = null;
56 if (statement == null) {
57 String sqlStatementsContent = getContentsAsString(fileLoc);
58
59
60 sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$");
61 } else {
62 sqlStatements = new String[]{statement};
63 }
64 final String[] finalSqlStatements = sqlStatements;
65 new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() {
66 public Object doInTransaction(TransactionStatus status) {
67 return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() {
68 public Object doInConnection(Connection connection) throws SQLException {
69 Statement statement = connection.createStatement();
70 LOG.info("################################");
71 LOG.info(fileLoc != null ? "#" + fileLoc : "#");
72 LOG.info("#");
73 for (String sqlStatement : finalSqlStatements) {
74 if (StringUtils.isNotBlank(sqlStatement)) {
75 LOG.info("# Executing sql statement ->" + sqlStatement + "<-");
76 statement.execute(sqlStatement);
77 }
78 }
79 LOG.info("#");
80 LOG.info("#");
81 LOG.info("################################");
82 statement.close();
83 return null;
84 }
85 });
86 }
87 });
88 }
89
90 private String getContentsAsString(String fileLoc) throws Exception {
91 ClasspathOrFileResourceLoader resourceLoader = new ClasspathOrFileResourceLoader();
92 String data = "";
93 BufferedReader reader = null;
94 try {
95 reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream()));
96 String line = "";
97 while ((line = reader.readLine()) != null) {
98
99
100
101 if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) {
102 data += line + "\r\n ";
103 }
104 }
105 } finally {
106 if (reader != null) {
107 try {
108 reader.close();
109 } catch (Exception e) {
110 LOG.error(e);
111 }
112 }
113
114 }
115 return data;
116 }
117
118 public String getSeperatorChar() {
119 if (this.seperatorChar == null) {
120 return ";";
121 }
122 return seperatorChar;
123 }
124
125 }