GenerateValue.java
/***************************************************************************
Copyright 2015 Emily Estes
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 net.metanotion.sqltest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.metanotion.scripting.Struct;
import net.metanotion.sql.RowMapParser;
import net.metanotion.sql.RSIterator;
import net.metanotion.sqlc.SQLObjectServer;
interface GenerateValue {
public Object eval(Connection conn, SQLObjectServer os, Map<String,Value> env) throws Exception;
}
final class ApplyMethod implements GenerateValue {
private final String klazz;
private final String method;
private final Iterable<Value> params;
public ApplyMethod(final String klazz, final String method, final Iterable<Value> params) {
this.klazz = klazz;
this.method = method;
this.params = params;
}
private static Map<String, Object> structToMap(final Struct struct) {
final HashMap<String,Object> map = new HashMap<>();
for(final String key: struct.listProperties()) {
map.put(key, struct.get(key));
}
return map;
}
@Override public Object eval(final Connection conn, final SQLObjectServer os, final Map<String,Value> env) throws Exception {
final ArrayList<Object> pList = new ArrayList<>();
pList.add(method);
pList.add(conn);
for(final Value param: params) {
pList.add(param.eval(env));
}
final Object result = os.get(klazz).eval(pList);
if (result instanceof Struct) {
return structToMap((Struct) result);
} else if (result instanceof Iterable) {
final ArrayList<Object> list = new ArrayList<>();
for(final Object o: (Iterable) result) {
if(o instanceof Struct) {
list.add(structToMap((Struct) o));
} else {
list.add(o);
}
}
return list;
} else {
return result;
}
}
}
final class ApplySql implements GenerateValue {
private final String sql;
public ApplySql(final String sql) { this.sql = sql; }
@Override public Object eval(final Connection conn,
final SQLObjectServer os,
final Map<String,Value> env) throws SQLException {
try (final PreparedStatement stmt = conn.prepareStatement(sql)) {
if(stmt.execute()) {
try (final ResultSet rs = stmt.getResultSet()) {
final ArrayList<Object> list = new ArrayList<>();
final RSIterator<Map> it = new RSIterator<>(rs, RowMapParser.INSTANCE);
while(it.hasNext()) {
list.add(it.next());
}
return list;
}
} else {
return stmt.getUpdateCount();
}
}
}
}