001 /* 002 * Copyright 2007 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in 005 * compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.opensource.org/licenses/ecl2.php 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS 010 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 011 * language governing permissions and limitations under the License. 012 */ 013 package org.kuali.rice.test; 014 015 import java.io.BufferedReader; 016 import java.io.InputStreamReader; 017 import java.sql.Connection; 018 import java.sql.SQLException; 019 import java.sql.Statement; 020 021 import org.apache.commons.lang.StringUtils; 022 import org.apache.log4j.Logger; 023 import org.springframework.core.io.DefaultResourceLoader; 024 import org.springframework.jdbc.core.ConnectionCallback; 025 import org.springframework.jdbc.core.JdbcTemplate; 026 import org.springframework.transaction.TransactionStatus; 027 import org.springframework.transaction.support.TransactionCallback; 028 import org.springframework.transaction.support.TransactionTemplate; 029 030 public class SQLDataLoader { 031 032 private static final Logger LOG = Logger.getLogger(SQLDataLoader.class); 033 public static final String SQL_LINE_COMMENT_PREFIX = "--"; 034 035 private String fileLoc; 036 private String seperatorChar; 037 private String statement; 038 039 public SQLDataLoader(String statement) { 040 this.fileLoc = null; 041 this.seperatorChar = null; 042 this.statement = statement; 043 } 044 045 public SQLDataLoader(String fileLoc, String seperatorChar) { 046 this.fileLoc = fileLoc; 047 this.seperatorChar = seperatorChar; 048 this.statement = null; 049 } 050 051 public void runSql() throws Exception { 052 String[] sqlStatements = null; 053 if (statement == null) { 054 String sqlStatementsContent = getContentsAsString(fileLoc); 055 // separator char must be the last non-whitespace char on the line 056 // to avoid splitting in the middle of data that might contain the separator char 057 sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$"); 058 } else { 059 sqlStatements = new String[]{statement}; 060 } 061 final String[] finalSqlStatements = sqlStatements; 062 new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() { 063 public Object doInTransaction(TransactionStatus status) { 064 return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() { 065 public Object doInConnection(Connection connection) throws SQLException { 066 Statement statement = connection.createStatement(); 067 LOG.info("################################"); 068 LOG.info(fileLoc != null ? "#" + fileLoc : "#"); 069 LOG.info("#"); 070 for (String sqlStatement : finalSqlStatements) { 071 if (StringUtils.isNotBlank(sqlStatement)) { 072 LOG.info("# Executing sql statement ->" + sqlStatement + "<-"); 073 statement.execute(sqlStatement); 074 } 075 } 076 LOG.info("#"); 077 LOG.info("#"); 078 LOG.info("################################"); 079 statement.close(); 080 return null; 081 } 082 }); 083 } 084 }); 085 } 086 087 private String getContentsAsString(String fileLoc) throws Exception { 088 DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); 089 String data = ""; 090 BufferedReader reader = null; 091 try { 092 reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream())); 093 String line = ""; 094 while ((line = reader.readLine()) != null) { 095 // discard comments...commented single line statements 096 // will result in errors when executed because there are no 097 // results 098 if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) { 099 data += line + "\r\n "; 100 } 101 } 102 } finally { 103 if (reader != null) { 104 try { 105 reader.close(); 106 } catch (Exception e) { 107 LOG.error(e); 108 } 109 } 110 111 } 112 return data; 113 } 114 115 public String getSeperatorChar() { 116 if (this.seperatorChar == null) { 117 return ";"; 118 } 119 return seperatorChar; 120 } 121 122 }