|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DataSet.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: DataSet.java,v 1.8 2004/05/15 04:53:01 bran Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
44 |
|
|
45 |
*/
|
|
46 |
package groovy.sql;
|
|
47 |
|
|
48 |
import groovy.lang.Closure;
|
|
49 |
import groovy.lang.GroovyRuntimeException;
|
|
50 |
|
|
51 |
import java.sql.Connection;
|
|
52 |
import java.sql.PreparedStatement;
|
|
53 |
import java.sql.SQLException;
|
|
54 |
import java.util.ArrayList;
|
|
55 |
import java.util.Iterator;
|
|
56 |
import java.util.List;
|
|
57 |
import java.util.Map;
|
|
58 |
import java.util.logging.Level;
|
|
59 |
|
|
60 |
import org.codehaus.groovy.ast.ClassNode;
|
|
61 |
import org.codehaus.groovy.ast.MethodNode;
|
|
62 |
import org.codehaus.groovy.ast.stmt.Statement;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Represents an extent of objects
|
|
66 |
*
|
|
67 |
* @author Chris Stevenson
|
|
68 |
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
|
|
69 |
* @version $Revision: 1.8 $
|
|
70 |
*/
|
|
71 |
public class DataSet extends Sql { |
|
72 |
|
|
73 |
private Closure where;
|
|
74 |
private DataSet parent;
|
|
75 |
private String table;
|
|
76 |
private SqlWhereVisitor visitor;
|
|
77 |
private String sql;
|
|
78 |
private List params;
|
|
79 |
|
|
80 | 0 |
public DataSet(Sql sql, Class type) {
|
81 | 0 |
super(sql);
|
82 | 0 |
String table = type.getName(); |
83 | 0 |
int idx = table.lastIndexOf('.');
|
84 | 0 |
if (idx > 0) {
|
85 | 0 |
table = table.substring(idx + 1); |
86 |
} |
|
87 | 0 |
this.table = table.toLowerCase();
|
88 |
} |
|
89 |
|
|
90 | 0 |
public DataSet(Sql sql, String table) {
|
91 | 0 |
super(sql);
|
92 | 0 |
this.table = table;
|
93 |
} |
|
94 |
|
|
95 | 0 |
public DataSet(DataSet parent, Closure where) {
|
96 | 0 |
super(parent);
|
97 | 0 |
this.table = parent.table;
|
98 | 0 |
this.parent = parent;
|
99 | 0 |
this.where = where;
|
100 |
} |
|
101 |
|
|
102 | 0 |
public void add(Map values) throws SQLException { |
103 | 0 |
StringBuffer buffer = new StringBuffer("insert into "); |
104 | 0 |
buffer.append(table); |
105 | 0 |
buffer.append(" (");
|
106 | 0 |
StringBuffer paramBuffer = new StringBuffer();
|
107 | 0 |
boolean first = true; |
108 | 0 |
for (Iterator iter = values.entrySet().iterator(); iter.hasNext();) {
|
109 | 0 |
Map.Entry entry = (Map.Entry) iter.next(); |
110 | 0 |
String column = entry.getKey().toString(); |
111 | 0 |
if (first) {
|
112 | 0 |
first = false;
|
113 | 0 |
paramBuffer.append("?");
|
114 |
} |
|
115 |
else {
|
|
116 | 0 |
buffer.append(", ");
|
117 | 0 |
paramBuffer.append(", ?");
|
118 |
} |
|
119 | 0 |
buffer.append(column); |
120 |
} |
|
121 | 0 |
buffer.append(") values (");
|
122 | 0 |
buffer.append(paramBuffer.toString()); |
123 | 0 |
buffer.append(")");
|
124 |
|
|
125 | 0 |
Connection connection = createConnection(); |
126 | 0 |
PreparedStatement statement = null;
|
127 | 0 |
try {
|
128 | 0 |
statement = connection.prepareStatement(buffer.toString()); |
129 | 0 |
int i = 1;
|
130 | 0 |
for (Iterator iter = values.entrySet().iterator(); iter.hasNext();) {
|
131 | 0 |
Map.Entry entry = (Map.Entry) iter.next(); |
132 | 0 |
setObject(statement, i++, entry.getValue()); |
133 |
} |
|
134 | 0 |
int answer = statement.executeUpdate();
|
135 | 0 |
if (answer != 1) {
|
136 | 0 |
log.log(Level.WARNING, "Should have updated 1 row not " + answer + " when trying to add: " + values); |
137 |
} |
|
138 |
} |
|
139 |
catch (SQLException e) {
|
|
140 | 0 |
log.log(Level.WARNING, "Failed to add row for: " + values, e);
|
141 | 0 |
throw e;
|
142 |
} |
|
143 |
finally {
|
|
144 | 0 |
closeResources(connection, statement); |
145 |
} |
|
146 |
} |
|
147 |
|
|
148 | 0 |
public DataSet findAll(Closure where) {
|
149 | 0 |
return new DataSet(this, where); |
150 |
} |
|
151 |
|
|
152 | 0 |
public void each(Closure closure) throws SQLException { |
153 | 0 |
eachRow(getSql(), getParameters(), closure); |
154 |
} |
|
155 |
|
|
156 | 0 |
public String getSql() {
|
157 | 0 |
if (sql == null) { |
158 | 0 |
sql = "select * from " + table;
|
159 | 0 |
if (where != null) { |
160 | 0 |
String clause = "";
|
161 | 0 |
if (parent != null && parent.where != null) { |
162 | 0 |
clause += parent.getSqlVisitor().getWhere() + " and ";
|
163 |
} |
|
164 | 0 |
clause += getSqlVisitor().getWhere(); |
165 | 0 |
if (clause.length() > 0) {
|
166 | 0 |
sql += " where " + clause;
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 | 0 |
return sql;
|
171 |
} |
|
172 |
|
|
173 | 0 |
public List getParameters() {
|
174 | 0 |
if (params == null) { |
175 | 0 |
params = new ArrayList();
|
176 | 0 |
if (parent != null && parent.where != null) { |
177 | 0 |
params.addAll(parent.getParameters()); |
178 |
} |
|
179 | 0 |
params.addAll(getSqlVisitor().getParameters()); |
180 |
} |
|
181 | 0 |
return params;
|
182 |
} |
|
183 |
|
|
184 | 0 |
protected SqlWhereVisitor getSqlVisitor() {
|
185 | 0 |
if (visitor == null) { |
186 | 0 |
visitor = new SqlWhereVisitor();
|
187 | 0 |
if (where != null) { |
188 | 0 |
ClassNode classNode = where.getMetaClass().getClassNode(); |
189 | 0 |
if (classNode == null) { |
190 | 0 |
throw new GroovyRuntimeException( |
191 |
"Could not find the ClassNode for MetaClass: " + where.getMetaClass());
|
|
192 |
} |
|
193 | 0 |
List methods = classNode.getDeclaredMethods("doCall");
|
194 | 0 |
if (!methods.isEmpty()) {
|
195 | 0 |
MethodNode method = (MethodNode) methods.get(0); |
196 | 0 |
if (method != null) { |
197 | 0 |
Statement statement = method.getCode(); |
198 | 0 |
if (statement != null) { |
199 | 0 |
statement.visit(visitor); |
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 | 0 |
return visitor;
|
206 |
} |
|
207 |
/*
|
|
208 |
* create a subset of the original dataset
|
|
209 |
*/
|
|
210 | 0 |
public DataSet createView(Closure criteria) {
|
211 | 0 |
return new DataSet(this, criteria); |
212 |
} |
|
213 |
} |
|
214 |
|
|