KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > Script


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt;
25
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.sql.DataSource JavaDoc;
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 /**
43  * @author Felix Gnass [fgnass at neteye dot de]
44  *
45  */

46 public class Script {
47
48     private List JavaDoc callbacks = new ArrayList JavaDoc();
49
50     private StringBuffer JavaDoc buffer;
51
52     private boolean nospace;
53     
54     private boolean manualExecutionOnly;
55     
56     public Script() {
57     }
58
59     public Script(String JavaDoc sql) {
60         append(sql);
61     }
62
63     public Script append(String JavaDoc 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 JavaDoc();
101     }
102
103     public boolean isManualExecutionOnly() {
104         return manualExecutionOnly;
105     }
106
107     public void forceManualExecution() {
108         manualExecutionOnly = true;
109     }
110
111     public List JavaDoc getCallbacks() {
112         newStatement();
113         return callbacks;
114     }
115
116     public void execute(DataSource JavaDoc dataSource) {
117         Assert.state(manualExecutionOnly == false,
118                 "This script must be manually executed.");
119         
120         JdbcTemplate template = new JdbcTemplate(dataSource);
121         Iterator JavaDoc it = getCallbacks().iterator();
122         while (it.hasNext()) {
123             StatementCallback callback = (StatementCallback) it.next();
124             template.execute(callback);
125         }
126     }
127     
128     public String JavaDoc getSql() {
129         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
130         Iterator JavaDoc 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 JavaDoc sql;
143
144         public SqlCallback(String JavaDoc sql) {
145             this.sql = sql;
146         }
147
148         public String JavaDoc getSql() {
149             return sql;
150         }
151
152         public Object JavaDoc doInStatement(Statement JavaDoc statement)
153                 throws SQLException JavaDoc, DataAccessException {
154             
155             log.info("Revolt: " + sql);
156             statement.execute(sql);
157             return null;
158         }
159     }
160 }
161
Popular Tags