HSQLDB Store...
parent
e903133b95
commit
ce2d831fa2
|
@ -30,6 +30,7 @@ import org.hwo.io.NewSerialPort.NewSerialPort;
|
||||||
import org.hwo.platform.Platform;
|
import org.hwo.platform.Platform;
|
||||||
import org.hwo.servicelink.ServiceLink;
|
import org.hwo.servicelink.ServiceLink;
|
||||||
import org.hwo.servicelink.ServiceLinkListener;
|
import org.hwo.servicelink.ServiceLinkListener;
|
||||||
|
import org.hwo.pulscounter.db.PulsCounterDatabase;
|
||||||
import org.hwo.pulscounter.device.IDeviceConnector;
|
import org.hwo.pulscounter.device.IDeviceConnector;
|
||||||
import org.hwo.pulscounter.device.ServiceLinkDeviceConnector;
|
import org.hwo.pulscounter.device.ServiceLinkDeviceConnector;
|
||||||
import org.hwo.pulscounter.device.SimulatedCounter;
|
import org.hwo.pulscounter.device.SimulatedCounter;
|
||||||
|
@ -64,6 +65,7 @@ public class PulsCounterApplication implements ServiceLinkListener{
|
||||||
interfaceClasses;
|
interfaceClasses;
|
||||||
private List<IDeviceConnector> interfaces;
|
private List<IDeviceConnector> interfaces;
|
||||||
|
|
||||||
|
private PulsCounterDatabase database;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -248,6 +250,10 @@ public class PulsCounterApplication implements ServiceLinkListener{
|
||||||
Class<IDeviceConnector> clazz = getInterfaceClass(applicationConfiguration.getProperty(String.format("interfaces.%d.class",n)));
|
Class<IDeviceConnector> clazz = getInterfaceClass(applicationConfiguration.getProperty(String.format("interfaces.%d.class",n)));
|
||||||
addInterface(clazz, applicationConfiguration.getProperty(String.format("interfaces.%d.settings",n)));
|
addInterface(clazz, applicationConfiguration.getProperty(String.format("interfaces.%d.settings",n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
database = new PulsCounterDatabase();
|
||||||
|
log(INFO,"Database Schema Version: %s", database.getSchemaVersion());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,6 +267,9 @@ public class PulsCounterApplication implements ServiceLinkListener{
|
||||||
frame.dispose();
|
frame.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
database.close();
|
||||||
|
|
||||||
|
|
||||||
if (shouldSaveConfiguration){
|
if (shouldSaveConfiguration){
|
||||||
|
|
||||||
applicationConfiguration.setProperty("interfaces.n", Integer.toString(interfaces.size()));
|
applicationConfiguration.setProperty("interfaces.n", Integer.toString(interfaces.size()));
|
||||||
|
@ -370,7 +379,9 @@ public class PulsCounterApplication implements ServiceLinkListener{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PulsCounterDatabase getDatabase() {
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ToDO: Upgrade the old stuff ... */
|
/* ToDO: Upgrade the old stuff ... */
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
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){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
/*
|
||||||
|
synololog sql database schema
|
||||||
|
|
||||||
|
Version: 0
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
create table properties (id uuid primary key,name varchar(255) unique,value varchar(255));
|
||||||
|
create table devices (id uuid,serial varchar(12));
|
||||||
|
create table snapshots (id uuid,device uuid,timestamp integer,counters int array[32],analogs int array[8],inputs int,outputs int,pullups int,inverts int);
|
||||||
|
|
||||||
|
insert into properties (id,name,value) values(uuid(),'db.schema.version','0');
|
||||||
|
|
Loading…
Reference in New Issue