001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.revolt;
025:
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import javax.sql.DataSource;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.springframework.dao.DataAccessException;
037: import org.springframework.jdbc.core.JdbcTemplate;
038: import org.springframework.jdbc.core.SqlProvider;
039: import org.springframework.jdbc.core.StatementCallback;
040: import org.springframework.util.Assert;
041:
042: /**
043: * @author Felix Gnass [fgnass at neteye dot de]
044: *
045: */
046: public class Script {
047:
048: private List callbacks = new ArrayList();
049:
050: private StringBuffer buffer;
051:
052: private boolean nospace;
053:
054: private boolean manualExecutionOnly;
055:
056: public Script() {
057: }
058:
059: public Script(String sql) {
060: append(sql);
061: }
062:
063: public Script append(String sql) {
064: if (buffer == null) {
065: newStatement();
066: } else if (!nospace) {
067: buffer.append(' ');
068: }
069: nospace = false;
070: buffer.append(sql);
071: return this ;
072: }
073:
074: public Script append(char c) {
075: if (buffer == null) {
076: newStatement();
077: } else if (c == '(') {
078: buffer.append(' ');
079: nospace = true;
080: }
081: buffer.append(c);
082: return this ;
083: }
084:
085: public Script append(Script script) {
086: if (script != null) {
087: newStatement();
088: callbacks.addAll(script.getCallbacks());
089: manualExecutionOnly |= script.isManualExecutionOnly();
090: }
091: return this ;
092: }
093:
094: public void newStatement() {
095: if (buffer != null && buffer.length() > 0) {
096: callbacks.add(new SqlCallback(buffer.toString()));
097: }
098: buffer = new StringBuffer();
099: }
100:
101: public boolean isManualExecutionOnly() {
102: return manualExecutionOnly;
103: }
104:
105: public void forceManualExecution() {
106: manualExecutionOnly = true;
107: }
108:
109: public List getCallbacks() {
110: newStatement();
111: return callbacks;
112: }
113:
114: public void execute(DataSource dataSource) {
115: Assert.state(manualExecutionOnly == false,
116: "This script must be manually executed.");
117:
118: JdbcTemplate template = new JdbcTemplate(dataSource);
119: Iterator it = getCallbacks().iterator();
120: while (it.hasNext()) {
121: StatementCallback callback = (StatementCallback) it.next();
122: template.execute(callback);
123: }
124: }
125:
126: public String getSql() {
127: StringBuffer sql = new StringBuffer();
128: Iterator it = getCallbacks().iterator();
129: while (it.hasNext()) {
130: SqlProvider provider = (SqlProvider) it.next();
131: sql.append(provider.getSql()).append(";\n");
132: }
133: return sql.toString();
134: }
135:
136: public static class SqlCallback implements StatementCallback,
137: SqlProvider {
138:
139: private static Log log = LogFactory.getLog(SqlCallback.class);
140:
141: private String sql;
142:
143: public SqlCallback(String sql) {
144: this .sql = sql;
145: }
146:
147: public String getSql() {
148: return sql;
149: }
150:
151: public Object doInStatement(Statement statement)
152: throws SQLException, DataAccessException {
153:
154: log.info("Revolt: " + sql);
155: statement.execute(sql);
156: return null;
157: }
158: }
159: }
|