Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package es.cbgp.upm.logic;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Iterator;
- import java.util.Properties;
- import java.util.Set;
- import java.util.TreeSet;
- import es.cbgp.upm.objects.Input;
- import es.cbgp.upm.objects.Output;
- import es.cbgp.upm.objects.Result;
- public class DerbyDBManager {
- private Connection conn;
- private int api;
- private int reasoner;
- private int mode;
- private MyLogger logger;
- public DerbyDBManager(int a, int r, int m, MyLogger l) throws Exception {
- this.api = a;
- this.reasoner = r;
- this.mode = m;
- this.logger = l;
- load();
- }
- public void load() throws Exception {
- String driver = ConfigManager.getConfig(Constants.DERBY_DB_DRIVER);
- String protocol = ConfigManager.getConfig(Constants.DERBY_DB_PROTOCOL);
- String dbName = ConfigManager.getConfig(Constants.DERBY_DB_NAME);
- Class.forName(driver).newInstance();
- Properties props = new Properties();
- props.put(Constants.USER,
- ConfigManager.getConfig(Constants.DERBY_DB_USER));
- props.put(Constants.PASSWORD,
- ConfigManager.getConfig(Constants.DERBY_DB_PASSWORD));
- conn = DriverManager.getConnection(protocol + "db/" + dbName
- + ";create=true", props);
- conn.setAutoCommit(false);
- }
- public boolean existsSchema() throws Exception {
- Statement s = conn.createStatement();
- try {
- return s.execute("SELECT * from inputs");
- } catch (Exception e) {
- return false;
- }
- }
- public Result createSchema() {
- try {
- Statement s = conn.createStatement();
- s.execute("create table inputs(id int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), input_class varchar(512), error varchar(1000), api varchar(128))");
- s.execute("create table outputs(id int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), output_class varchar(512), error varchar(1000), api varchar(128))");
- s.execute("create table pairs(id int, input_class varchar(512), output_class varchar(512), reasoned smallint, error varchar(1000), results varchar(1000), api varchar(128), reasoner varchar(128), mode varchar(128))");
- } catch (Exception e) {
- return new Result(false, e.getMessage());
- }
- return new Result(true);
- }
- public Set<Input> getInputsFromDB() {
- Set<Input> inputs = new TreeSet<Input>();
- try {
- ResultSet rs = conn.createStatement(
- ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_UPDATABLE,
- ResultSet.HOLD_CURSORS_OVER_COMMIT).executeQuery(
- "SELECT * from inputs");
- int rowcount = 0;
- if (rs.last()) {
- rowcount = rs.getRow();
- rs.beforeFirst();
- }
- if (rowcount <= 0) {
- return inputs;
- } else {
- while (!rs.next()) {
- System.out.println(rs.getString("input_class"));
- // String inClass = rs.getString(1);
- // inputs.add(new Input(inClass));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return inputs;
- }
- public Set<Output> getOutputsFromDB() throws Exception {
- Set<Output> outputs = new TreeSet<Output>();
- ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT)
- .executeQuery("SELECT output_class from outputs");
- while (!rs.next()) {
- String ouClass = rs.getString(1);
- outputs.add(new Output(ouClass));
- }
- return outputs;
- }
- /**
- * Method to insert the inputs.
- * @param ri Receives the set with the inputs.
- * @throws Exception It can throw an exception.
- */
- public void insertInputs(Set<Input> ri) throws Exception {
- PreparedStatement psInsert = null;
- Iterator<Input> it = ri.iterator();
- while (it.hasNext()) {
- Input in = it.next();
- psInsert = conn
- .prepareStatement("insert into inputs(input_class,api) values (?, ?)");
- psInsert.setString(1, in.getObject());
- psInsert.setString(2, Constants.APIS[this.api]);
- this.logger.log("Inserting input in db: " + in.getObject() + " - API[" + Constants.APIS[this.api] + "]");
- psInsert.executeUpdate();
- psInsert.close();
- }
- }
- public void close() throws Exception {
- this.conn.commit();
- this.conn.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement