001 /* 002 $Id: DataSet.java,v 1.8 2004/05/15 04:53:01 bran Exp $ 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: 1.8 $ 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.hasNext();) { 109 Map.Entry entry = (Map.Entry) iter.next(); 110 String column = entry.getKey().toString(); 111 if (first) { 112 first = false; 113 paramBuffer.append("?"); 114 } 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.hasNext();) { 131 Map.Entry entry = (Map.Entry) iter.next(); 132 setObject(statement, i++, entry.getValue()); 133 } 134 int answer = statement.executeUpdate(); 135 if (answer != 1) { 136 log.log(Level.WARNING, "Should have updated 1 row not " + answer + " when trying to add: " + values); 137 } 138 } 139 catch (SQLException e) { 140 log.log(Level.WARNING, "Failed to add row for: " + values, e); 141 throw e; 142 } 143 finally { 144 closeResources(connection, statement); 145 } 146 } 147 148 public DataSet findAll(Closure where) { 149 return new DataSet(this, where); 150 } 151 152 public void each(Closure closure) throws SQLException { 153 eachRow(getSql(), getParameters(), closure); 154 } 155 156 public String getSql() { 157 if (sql == null) { 158 sql = "select * from " + table; 159 if (where != null) { 160 String clause = ""; 161 if (parent != null && parent.where != null) { 162 clause += parent.getSqlVisitor().getWhere() + " and "; 163 } 164 clause += getSqlVisitor().getWhere(); 165 if (clause.length() > 0) { 166 sql += " where " + clause; 167 } 168 } 169 } 170 return sql; 171 } 172 173 public List getParameters() { 174 if (params == null) { 175 params = new ArrayList(); 176 if (parent != null && parent.where != null) { 177 params.addAll(parent.getParameters()); 178 } 179 params.addAll(getSqlVisitor().getParameters()); 180 } 181 return params; 182 } 183 184 protected SqlWhereVisitor getSqlVisitor() { 185 if (visitor == null) { 186 visitor = new SqlWhereVisitor(); 187 if (where != null) { 188 ClassNode classNode = where.getMetaClass().getClassNode(); 189 if (classNode == null) { 190 throw new GroovyRuntimeException( 191 "Could not find the ClassNode for MetaClass: " + where.getMetaClass()); 192 } 193 List methods = classNode.getDeclaredMethods("doCall"); 194 if (!methods.isEmpty()) { 195 MethodNode method = (MethodNode) methods.get(0); 196 if (method != null) { 197 Statement statement = method.getCode(); 198 if (statement != null) { 199 statement.visit(visitor); 200 } 201 } 202 } 203 } 204 } 205 return visitor; 206 } 207 /* 208 * create a subset of the original dataset 209 */ 210 public DataSet createView(Closure criteria) { 211 return new DataSet(this, criteria); 212 } 213 }