001: /*
002: $Id: DataSet.java 1197 2004-05-15 04:53:01Z bran $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package groovy.sql;
047:
048: import groovy.lang.Closure;
049: import groovy.lang.GroovyRuntimeException;
050:
051: import java.sql.Connection;
052: import java.sql.PreparedStatement;
053: import java.sql.SQLException;
054: import java.util.ArrayList;
055: import java.util.Iterator;
056: import java.util.List;
057: import java.util.Map;
058: import java.util.logging.Level;
059:
060: import org.codehaus.groovy.ast.ClassNode;
061: import org.codehaus.groovy.ast.MethodNode;
062: import org.codehaus.groovy.ast.stmt.Statement;
063:
064: /**
065: * Represents an extent of objects
066: *
067: * @author Chris Stevenson
068: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
069: * @version $Revision: 1197 $
070: */
071: public class DataSet extends Sql {
072:
073: private Closure where;
074: private DataSet parent;
075: private String table;
076: private SqlWhereVisitor visitor;
077: private String sql;
078: private List params;
079:
080: public DataSet(Sql sql, Class type) {
081: super (sql);
082: String table = type.getName();
083: int idx = table.lastIndexOf('.');
084: if (idx > 0) {
085: table = table.substring(idx + 1);
086: }
087: this .table = table.toLowerCase();
088: }
089:
090: public DataSet(Sql sql, String table) {
091: super (sql);
092: this .table = table;
093: }
094:
095: public DataSet(DataSet parent, Closure where) {
096: super (parent);
097: this .table = parent.table;
098: this .parent = parent;
099: this .where = where;
100: }
101:
102: public void add(Map values) throws SQLException {
103: StringBuffer buffer = new StringBuffer("insert into ");
104: buffer.append(table);
105: buffer.append(" (");
106: StringBuffer paramBuffer = new StringBuffer();
107: boolean first = true;
108: for (Iterator iter = values.entrySet().iterator(); iter
109: .hasNext();) {
110: Map.Entry entry = (Map.Entry) iter.next();
111: String column = entry.getKey().toString();
112: if (first) {
113: first = false;
114: paramBuffer.append("?");
115: } else {
116: buffer.append(", ");
117: paramBuffer.append(", ?");
118: }
119: buffer.append(column);
120: }
121: buffer.append(") values (");
122: buffer.append(paramBuffer.toString());
123: buffer.append(")");
124:
125: Connection connection = createConnection();
126: PreparedStatement statement = null;
127: try {
128: statement = connection.prepareStatement(buffer.toString());
129: int i = 1;
130: for (Iterator iter = values.entrySet().iterator(); iter
131: .hasNext();) {
132: Map.Entry entry = (Map.Entry) iter.next();
133: setObject(statement, i++, entry.getValue());
134: }
135: int answer = statement.executeUpdate();
136: if (answer != 1) {
137: log.log(Level.WARNING, "Should have updated 1 row not "
138: + answer + " when trying to add: " + values);
139: }
140: } catch (SQLException e) {
141: log.log(Level.WARNING, "Failed to add row for: " + values,
142: e);
143: throw e;
144: } finally {
145: closeResources(connection, statement);
146: }
147: }
148:
149: public DataSet findAll(Closure where) {
150: return new DataSet(this , where);
151: }
152:
153: public void each(Closure closure) throws SQLException {
154: eachRow(getSql(), getParameters(), closure);
155: }
156:
157: public String getSql() {
158: if (sql == null) {
159: sql = "select * from " + table;
160: if (where != null) {
161: String clause = "";
162: if (parent != null && parent.where != null) {
163: clause += parent.getSqlVisitor().getWhere()
164: + " and ";
165: }
166: clause += getSqlVisitor().getWhere();
167: if (clause.length() > 0) {
168: sql += " where " + clause;
169: }
170: }
171: }
172: return sql;
173: }
174:
175: public List getParameters() {
176: if (params == null) {
177: params = new ArrayList();
178: if (parent != null && parent.where != null) {
179: params.addAll(parent.getParameters());
180: }
181: params.addAll(getSqlVisitor().getParameters());
182: }
183: return params;
184: }
185:
186: protected SqlWhereVisitor getSqlVisitor() {
187: if (visitor == null) {
188: visitor = new SqlWhereVisitor();
189: if (where != null) {
190: ClassNode classNode = where.getMetaClass()
191: .getClassNode();
192: if (classNode == null) {
193: throw new GroovyRuntimeException(
194: "Could not find the ClassNode for MetaClass: "
195: + where.getMetaClass());
196: }
197: List methods = classNode.getDeclaredMethods("doCall");
198: if (!methods.isEmpty()) {
199: MethodNode method = (MethodNode) methods.get(0);
200: if (method != null) {
201: Statement statement = method.getCode();
202: if (statement != null) {
203: statement.visit(visitor);
204: }
205: }
206: }
207: }
208: }
209: return visitor;
210: }
211:
212: /*
213: * create a subset of the original dataset
214: */
215: public DataSet createView(Closure criteria) {
216: return new DataSet(this, criteria);
217: }
218: }
|