1 24 package org.riotfamily.revolt; 25 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.sql.DataSource ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.springframework.dao.DataAccessException; 37 import org.springframework.jdbc.core.JdbcTemplate; 38 import org.springframework.jdbc.core.SqlProvider; 39 import org.springframework.jdbc.core.StatementCallback; 40 import org.springframework.util.Assert; 41 42 46 public class Script { 47 48 private List callbacks = new ArrayList (); 49 50 private StringBuffer buffer; 51 52 private boolean nospace; 53 54 private boolean manualExecutionOnly; 55 56 public Script() { 57 } 58 59 public Script(String sql) { 60 append(sql); 61 } 62 63 public Script append(String sql) { 64 if (buffer == null) { 65 newStatement(); 66 } 67 else if (!nospace) { 68 buffer.append(' '); 69 } 70 nospace = false; 71 buffer.append(sql); 72 return this; 73 } 74 75 public Script append(char c) { 76 if (buffer == null) { 77 newStatement(); 78 } 79 else if (c == '(') { 80 buffer.append(' '); 81 nospace = true; 82 } 83 buffer.append(c); 84 return this; 85 } 86 87 public Script append(Script script) { 88 if (script != null) { 89 newStatement(); 90 callbacks.addAll(script.getCallbacks()); 91 manualExecutionOnly |= script.isManualExecutionOnly(); 92 } 93 return this; 94 } 95 96 public void newStatement() { 97 if (buffer != null && buffer.length() > 0) { 98 callbacks.add(new SqlCallback(buffer.toString())); 99 } 100 buffer = new StringBuffer (); 101 } 102 103 public boolean isManualExecutionOnly() { 104 return manualExecutionOnly; 105 } 106 107 public void forceManualExecution() { 108 manualExecutionOnly = true; 109 } 110 111 public List getCallbacks() { 112 newStatement(); 113 return callbacks; 114 } 115 116 public void execute(DataSource dataSource) { 117 Assert.state(manualExecutionOnly == false, 118 "This script must be manually executed."); 119 120 JdbcTemplate template = new JdbcTemplate(dataSource); 121 Iterator it = getCallbacks().iterator(); 122 while (it.hasNext()) { 123 StatementCallback callback = (StatementCallback) it.next(); 124 template.execute(callback); 125 } 126 } 127 128 public String getSql() { 129 StringBuffer sql = new StringBuffer (); 130 Iterator it = getCallbacks().iterator(); 131 while (it.hasNext()) { 132 SqlProvider provider = (SqlProvider) it.next(); 133 sql.append(provider.getSql()).append(";\n"); 134 } 135 return sql.toString(); 136 } 137 138 public static class SqlCallback implements StatementCallback, SqlProvider { 139 140 private static Log log = LogFactory.getLog(SqlCallback.class); 141 142 private String sql; 143 144 public SqlCallback(String sql) { 145 this.sql = sql; 146 } 147 148 public String getSql() { 149 return sql; 150 } 151 152 public Object doInStatement(Statement statement) 153 throws SQLException , DataAccessException { 154 155 log.info("Revolt: " + sql); 156 statement.execute(sql); 157 return null; 158 } 159 } 160 } 161 | Popular Tags |