Implemented the automatic verfication of the db scheme and creation of a

new sqlite db.
This commit is contained in:
socrates
2014-10-02 13:58:33 +02:00
parent 566a8ca49f
commit 1b07cbdf14
2 changed files with 93 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map.Entry;
/** /**
* @class sqliteConnector * @class sqliteConnector
@@ -28,7 +29,7 @@ public class SqliteConnector implements DbConnector {
public SqliteConnector(String pHandle) { public SqliteConnector(String pHandle) {
this.dbHandle = pHandle; this.dbHandle = pHandle;
this.loadScheme(); this.sqlScheme = this.loadScheme();
try { try {
File dbFile = new File(this.dbHandle); File dbFile = new File(this.dbHandle);
@@ -38,17 +39,23 @@ public class SqliteConnector implements DbConnector {
if(dbFile.isFile()){ if(dbFile.isFile()){
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
//Pr<50>fe Schema if(!this.isValidDB(this.sqlScheme, this.connection)){
throw new SQLException("The given db file doesn't match the required sql schema.");
}
else {
System.out.println("Db Scheme looks fine to me.");
}
} }
else { else {
throw new IOException("Provided db handle may not be a file but a directory or a symlink!"); throw new IOException("Provided db handle may not be a file but a directory or a symlink!");
} }
} }
else { else {
System.out.println("There is no db file yet... creating a new db.");
dbFile.createNewFile(); dbFile.createNewFile();
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
//INitialisiere this.initNewDB(this.sqlScheme, this.connection);
} }
} }
catch (ClassNotFoundException e){ catch (ClassNotFoundException e){
@@ -62,15 +69,32 @@ public class SqliteConnector implements DbConnector {
} }
} }
private boolean isConfigured(){ private boolean isValidDB(Hashtable<String, String> pScheme, Connection pConn){
try { try {
Statement st = this.connection.createStatement(); Statement st = pConn.createStatement();
String query = "SELECT value FROM kborrow WHERE setting_name='is_configured' LIMIT 1"; String query = "SELECT name, sql FROM sqlite_master WHERE type = 'table'";
Hashtable<String, String> dbScheme = new Hashtable<String, String>();
ResultSet rs = st.executeQuery(query); ResultSet rs = st.executeQuery(query);
return rs.getBoolean("value"); while(rs.next()){
dbScheme.put(rs.getString("name"), this.removeLineBreaks(rs.getString("sql")));
}
for (Entry<String, String> pEntry : pScheme.entrySet()){
String pSql = pEntry.getValue();
boolean match = false;
for (Entry<String, String> dbEntry : dbScheme.entrySet()){
if(pSql.equalsIgnoreCase(dbEntry.getValue())){
match = true;
break;
}
}
if(!match) return false;
}
return true;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -78,23 +102,38 @@ public class SqliteConnector implements DbConnector {
} }
} }
private void loadScheme(){ private boolean initNewDB(Hashtable<String, String> pScheme, Connection pConn){
this.sqlScheme = new Hashtable<String, String>(); try {
Statement st = pConn.createStatement();
this.sqlScheme.put("kborrow", for (Entry<String, String> pEntry : pScheme.entrySet()){
st.executeUpdate(pEntry.getValue());
}
return true;
}
catch (SQLException e){
e.printStackTrace();
return false;
}
}
private Hashtable<String, String> loadScheme(){
Hashtable<String, String> tScheme= new Hashtable<String, String>();
tScheme.put("kborrow",
"CREATE TABLE kborrow (" "CREATE TABLE kborrow ("
+ "setting_name TEXT," + "setting_name TEXT,"
+ "value INT" + "value INT"
+ ")"); + ")");
this.sqlScheme.put("article", tScheme.put("article",
"CREATE TABLE article (" "CREATE TABLE article ("
+ "id INT PRIMARY KEY," + "id INT PRIMARY KEY,"
+ "name TEXT NOT NULL," + "name TEXT NOT NULL,"
+ "description TEXT" + "description TEXT"
+ ")"); + ")");
this.sqlScheme.put("lender", tScheme.put("lender",
"CREATE TABLE lender (" "CREATE TABLE lender ("
+ "id INT PRIMARY KEY," + "id INT PRIMARY KEY,"
+ "name TEXT," + "name TEXT,"
@@ -103,14 +142,14 @@ public class SqliteConnector implements DbConnector {
+ "comment TEXT" + "comment TEXT"
+ ")"); + ")");
this.sqlScheme.put("user", tScheme.put("user",
"CREATE TABLE user (" "CREATE TABLE user ("
+ "id INT PRIMARY KEY," + "id INT PRIMARY KEY,"
+ "name TEXT," + "name TEXT,"
+ "surname TEXT" + "surname TEXT"
+ ")"); + ")");
this.sqlScheme.put("lending", tScheme.put("lending",
"CREATE TABLE lending (" "CREATE TABLE lending ("
+ "id INT PRIMARY KEY," + "id INT PRIMARY KEY,"
+ "article_id INT," + "article_id INT,"
@@ -121,6 +160,40 @@ public class SqliteConnector implements DbConnector {
+ "end_date DATE," + "end_date DATE,"
+ "comment TEXT" + "comment TEXT"
+ ")"); + ")");
return tScheme;
}
private String removeLineBreaks(String pString){
StringBuffer text = new StringBuffer(pString);
int i = 0;
boolean addI = true;
while (i < text.length()) {
if (text.charAt(i) == '\n') {
text.deleteCharAt(i);
addI = false;
}
if (text.charAt(i) == '\r') {
text.deleteCharAt(i);
addI = false;
}
if (text.charAt(i) == '\t') {
text.deleteCharAt(i);
addI = false;
}
if (addI) {
i++;
}
addI = true;
}
return text.toString();
} }
} }

View File

@@ -1,16 +1,16 @@
CREATE TABLE kborrow ( CREATE TABLE kborrow (
setting_name TEXT, setting_name TEXT,
value INTEGER value INT
); );
CREATE TABLE article ( CREATE TABLE article (
id INTEGER PRIMARY KEY, id INT PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT description TEXT
); );
CREATE TABLE lender ( CREATE TABLE lender (
id INTEGER PRIMARY KEY, id INT PRIMARY KEY,
name TEXT, name TEXT,
surname TEXT, surname TEXT,
student_number INT, student_number INT,
@@ -18,13 +18,13 @@ CREATE TABLE lender (
); );
CREATE TABLE user ( CREATE TABLE user (
id INTEGER PRIMARY KEY, id INT PRIMARY KEY,
name TEXT, name TEXT,
surname TEXT surname TEXT
); );
CREATE TABLE lending ( CREATE TABLE lending (
id INTEGER PRIMARY KEY, id INT PRIMARY KEY,
article_id INT, article_id INT,
user_id INT, user_id INT,
lender_id INT, lender_id INT,