122 lines
3.2 KiB
Java
122 lines
3.2 KiB
Java
package org.hwo.pulscounter.db;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
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());
|
|
}
|
|
|
|
}
|
|
|
|
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<args.length;i++){
|
|
stmt.setObject(i+1, args[i]);
|
|
}
|
|
|
|
stmt.execute();
|
|
|
|
ResultSet result = stmt.getResultSet();
|
|
|
|
stmt.closeOnCompletion();
|
|
|
|
return result;
|
|
|
|
} catch (SQLException e) {
|
|
log(ERROR,"SQL Statement failed: %s",query);
|
|
log(ERROR,"Exception: %s", e.toString());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private String executeVerySimpleQuery(String query,Object... args){
|
|
ResultSet result = executeSimpleSQL(query,args);
|
|
if (result != null){
|
|
try {
|
|
if (result.next()){
|
|
return result.getString(1);
|
|
}
|
|
} catch (SQLException e) {
|
|
log(ERROR,"SQL Statement failed: %s",query);
|
|
log(ERROR,"Exception: %s", e.toString());
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getProperty(String name){
|
|
return executeVerySimpleQuery("SELECT value FROM properties WHERE name=?", name);
|
|
}
|
|
public void setProperty(String name,String value){
|
|
executeSimpleSQL("MERGE INTO properties USING (VALUES(?,?)) as p(name,value) ON properties.name = p.name WHEN MATCHED THEN UPDATE SET properties.value = p.value WHEN NOT MATCHED THEN INSERT VALUES uuid(), p.name, p.value", name,value);
|
|
}
|
|
public void removeProperty(String name){
|
|
executeSimpleSQL("DELETE FROM properties WHERE properties.name = ?", name);
|
|
}
|
|
|
|
public String getSchemaVersion(){
|
|
return getProperty("db.schema.version");
|
|
}
|
|
|
|
public void storeSnapshots(SnapShot[] snapShots){
|
|
for (SnapShot snapShot: snapShots){
|
|
storeSnapshot(snapShot);
|
|
}
|
|
}
|
|
|
|
private void storeSnapshot(SnapShot snapShot){
|
|
|
|
executeVerySimpleQuery("INSERT INTO snapshots (id,device,timestamp,counters,analogs,inputs,outputs,pullups,inverts) VALUES(uuid(),?,?,?,?,?,?,?,?)",
|
|
snapShot.getDeviceSerial(),
|
|
snapShot.getTimestamp(),
|
|
snapShot.getValues(),
|
|
snapShot.getAnalog(),
|
|
snapShot.getInputmask(),
|
|
snapShot.getOutputmask(),
|
|
snapShot.getPullupmask(),
|
|
snapShot.getInvertmask()
|
|
);
|
|
|
|
// (id uuid,device integer,timestamp integer,counters integer array[32],analogs integer array[8],inputs integer,outputs integer,pullups integer,inverts integer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|