
net.sf.aguacate.context.spi.sql.impl.AbstractSentenceSqlStatic Maven / Gradle / Ivy
package net.sf.aguacate.context.spi.sql.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.sf.aguacate.context.spi.sql.SentenceExecutionResult;
import net.sf.aguacate.context.spi.sql.SentenceSql;
public abstract class AbstractSentenceSqlStatic implements SentenceSql {
private static final Logger LOGGER = LogManager.getLogger(AbstractSentenceSqlStatic.class);
private final String name;
private final List methods;
private final String outputName;
private final String[] required;
private final String sql;
public AbstractSentenceSqlStatic(String name, List methods, String outputName, String[] required,
String sql) {
this.name = name;
this.methods = methods;
this.outputName = outputName;
this.required = required;
this.sql = sql;
}
@Override
public String getName() {
return name;
}
@Override
public boolean validFor(String method) {
return methods.contains(method);
}
@Override
public String outputName() {
return outputName;
}
@Override
public SentenceExecutionResult execute(Connection connection, Map context) throws SQLException {
PreparedStatement statement = connection.prepareStatement(sql);
try {
for (int i = 0; i < required.length; i++) {
String parameter = required[i];
statement.setObject(i + 1, context.get(parameter));
}
return execute(statement);
} finally {
try {
statement.close();
} catch (SQLException e) {
LOGGER.warn("", e);
}
}
}
abstract SentenceExecutionResult execute(PreparedStatement statement) throws SQLException;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy