package org.hwo.pulscounter.db; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import java.util.UUID; import org.hwo.pulscounter.SnapShot; import static org.hwo.logging.Logging.*; import static org.hwo.logging.LogLevel.*; public class PulsCounterDatabase { private Connection dbConnection; public PulsCounterDatabase(){ try { getClass().getClassLoader().loadClass("org.hsqldb.jdbcDriver"); dbConnection = DriverManager.getConnection("jdbc:hsqldb:file:synololog-hsql", "SA", ""); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { log(ERROR,"HyperSQL Driver could not be loaded. [%s]",e.toString()); } InputStream is = getClass().getResourceAsStream("/org/hwo/pulscounter/db/schema/schema.sql"); if (is == null){ log(ERROR,"Database schema file not found"); } else { try { BufferedReader br = new BufferedReader(new InputStreamReader(is)); do { String sql = br.readLine(); if (sql == null){ break; } sql = sql.trim(); if (!sql.equals("") && !sql.startsWith("//")){ log(sql); executeSimpleSQL(sql); } }while (true); } catch (IOException e) { log(e); } } } public void close(){ try { dbConnection.close(); } catch (SQLException e) { log(ERROR,"Exception while closing database: %s",e.toString()); } } private ResultSet executeSimpleSQL(String query,Object... args){ try { PreparedStatement stmt = dbConnection.prepareStatement(query); for (int i=0;i snapshots = new LinkedList<>(); ResultSet result = executeSimpleSQL("SELECT id,device,snap_id,timestamp,counters,analogs,inputs,outputs,pullups,inverts,field0 FROM snapshots WHERE timestamp >= ?", fromTimestamp); try { while (result.next()){ SnapShot ss = new SnapShot(result); snapshots.add(ss); } } catch (SQLException e) { log(e); } return snapshots.toArray(new SnapShot[0]); } }