groovy.sql.DataSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all Show documentation
Show all versions of groovy-all Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.sql;
import groovy.lang.Closure;
import groovy.lang.GroovyRuntimeException;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.CodeVisitorSupport;
import org.codehaus.groovy.ast.stmt.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
* Represents an extent of objects
*
* @author Chris Stevenson
* @author Paul King
* @author James Strachan
* @version $Revision: 18226 $
*/
public class DataSet extends Sql {
private Closure where;
private Closure sort;
private boolean reversed = false;
private DataSet parent;
private String table;
private SqlWhereVisitor visitor;
private SqlOrderByVisitor sortVisitor;
private String sql;
private List params;
private Sql delegate;
public DataSet(Sql sql, Class type) {
super(sql);
delegate = sql;
String table = type.getName();
int idx = table.lastIndexOf('.');
if (idx > 0) {
table = table.substring(idx + 1);
}
this.table = table.toLowerCase();
}
public DataSet(Sql sql, String table) {
super(sql);
delegate = sql;
this.table = table;
}
private DataSet(DataSet parent, Closure where) {
super(parent);
this.delegate = parent.delegate;
this.table = parent.table;
this.parent = parent;
this.where = where;
}
private DataSet(DataSet parent, Closure where, Closure sort) {
super(parent);
this.delegate = parent.delegate;
this.table = parent.table;
this.parent = parent;
this.where = where;
this.sort = sort;
}
private DataSet(DataSet parent) {
super(parent);
this.delegate = parent.delegate;
this.table = parent.table;
this.parent = parent;
this.reversed = true;
}
@Override
protected Connection createConnection() throws SQLException {
return delegate.createConnection();
}
@Override
protected void closeResources(Connection connection, java.sql.Statement statement, ResultSet results) {
delegate.closeResources(connection, statement, results);
}
@Override
protected void closeResources(Connection connection, java.sql.Statement statement) {
delegate.closeResources(connection, statement);
}
@Override
public void cacheConnection(Closure closure) throws SQLException {
delegate.cacheConnection(closure);
}
@Override
public void withTransaction(Closure closure) throws SQLException {
delegate.withTransaction(closure);
}
@Override
public void commit() throws SQLException {
delegate.commit();
}
@Override
public void rollback() throws SQLException {
delegate.rollback();
}
public void add(Map map) throws SQLException {
StringBuffer buffer = new StringBuffer("insert into ");
buffer.append(table);
buffer.append(" (");
StringBuffer paramBuffer = new StringBuffer();
boolean first = true;
for (String column : map.keySet()) {
if (first) {
first = false;
paramBuffer.append("?");
} else {
buffer.append(", ");
paramBuffer.append(", ?");
}
buffer.append(column);
}
buffer.append(") values (");
buffer.append(paramBuffer.toString());
buffer.append(")");
int answer = executeUpdate(buffer.toString(), new ArrayList