From 1b07cbdf14fc94eb45982c210ea5dd3939488252 Mon Sep 17 00:00:00 2001 From: socrates Date: Thu, 2 Oct 2014 13:58:33 +0200 Subject: [PATCH] Implemented the automatic verfication of the db scheme and creation of a new sqlite db. --- src/de/katho/kBorrow/db/SqliteConnector.java | 103 ++++++++++++++++--- src/de/katho/kBorrow/db/sqlite_init.sql | 10 +- 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/src/de/katho/kBorrow/db/SqliteConnector.java b/src/de/katho/kBorrow/db/SqliteConnector.java index c980cc7..52bc595 100644 --- a/src/de/katho/kBorrow/db/SqliteConnector.java +++ b/src/de/katho/kBorrow/db/SqliteConnector.java @@ -9,6 +9,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Hashtable; +import java.util.Map.Entry; /** * @class sqliteConnector @@ -28,7 +29,7 @@ public class SqliteConnector implements DbConnector { public SqliteConnector(String pHandle) { this.dbHandle = pHandle; - this.loadScheme(); + this.sqlScheme = this.loadScheme(); try { File dbFile = new File(this.dbHandle); @@ -37,18 +38,24 @@ public class SqliteConnector implements DbConnector { if(dbFile.exists()){ if(dbFile.isFile()){ this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); - - //Prü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 { throw new IOException("Provided db handle may not be a file but a directory or a symlink!"); } } else { + System.out.println("There is no db file yet... creating a new db."); dbFile.createNewFile(); this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); - //INitialisiere + this.initNewDB(this.sqlScheme, this.connection); } } catch (ClassNotFoundException e){ @@ -62,39 +69,71 @@ public class SqliteConnector implements DbConnector { } } - private boolean isConfigured(){ + private boolean isValidDB(Hashtable pScheme, Connection pConn){ try { - Statement st = this.connection.createStatement(); - String query = "SELECT value FROM kborrow WHERE setting_name='is_configured' LIMIT 1"; + Statement st = pConn.createStatement(); + String query = "SELECT name, sql FROM sqlite_master WHERE type = 'table'"; + Hashtable dbScheme = new Hashtable(); ResultSet rs = st.executeQuery(query); - return rs.getBoolean("value"); + while(rs.next()){ + dbScheme.put(rs.getString("name"), this.removeLineBreaks(rs.getString("sql"))); + } + for (Entry pEntry : pScheme.entrySet()){ + String pSql = pEntry.getValue(); + boolean match = false; + + for (Entry dbEntry : dbScheme.entrySet()){ + if(pSql.equalsIgnoreCase(dbEntry.getValue())){ + match = true; + break; + } + } + if(!match) return false; + } + + return true; } catch (SQLException e) { e.printStackTrace(); return false; + } + } + + private boolean initNewDB(Hashtable pScheme, Connection pConn){ + try { + Statement st = pConn.createStatement(); + + for (Entry pEntry : pScheme.entrySet()){ + st.executeUpdate(pEntry.getValue()); + } + return true; + } + catch (SQLException e){ + e.printStackTrace(); + return false; } } - private void loadScheme(){ - this.sqlScheme = new Hashtable(); + private Hashtable loadScheme(){ + Hashtable tScheme= new Hashtable(); - this.sqlScheme.put("kborrow", + tScheme.put("kborrow", "CREATE TABLE kborrow (" + "setting_name TEXT," + "value INT" + ")"); - this.sqlScheme.put("article", + tScheme.put("article", "CREATE TABLE article (" + "id INT PRIMARY KEY," + "name TEXT NOT NULL," + "description TEXT" + ")"); - this.sqlScheme.put("lender", + tScheme.put("lender", "CREATE TABLE lender (" + "id INT PRIMARY KEY," + "name TEXT," @@ -103,14 +142,14 @@ public class SqliteConnector implements DbConnector { + "comment TEXT" + ")"); - this.sqlScheme.put("user", + tScheme.put("user", "CREATE TABLE user (" + "id INT PRIMARY KEY," + "name TEXT," + "surname TEXT" + ")"); - this.sqlScheme.put("lending", + tScheme.put("lending", "CREATE TABLE lending (" + "id INT PRIMARY KEY," + "article_id INT," @@ -121,6 +160,40 @@ public class SqliteConnector implements DbConnector { + "end_date DATE," + "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(); } } diff --git a/src/de/katho/kBorrow/db/sqlite_init.sql b/src/de/katho/kBorrow/db/sqlite_init.sql index bf1532e..bb64847 100644 --- a/src/de/katho/kBorrow/db/sqlite_init.sql +++ b/src/de/katho/kBorrow/db/sqlite_init.sql @@ -1,16 +1,16 @@ CREATE TABLE kborrow ( setting_name TEXT, - value INTEGER + value INT ); CREATE TABLE article ( - id INTEGER PRIMARY KEY, + id INT PRIMARY KEY, name TEXT NOT NULL, description TEXT ); CREATE TABLE lender ( - id INTEGER PRIMARY KEY, + id INT PRIMARY KEY, name TEXT, surname TEXT, student_number INT, @@ -18,13 +18,13 @@ CREATE TABLE lender ( ); CREATE TABLE user ( - id INTEGER PRIMARY KEY, + id INT PRIMARY KEY, name TEXT, surname TEXT ); CREATE TABLE lending ( - id INTEGER PRIMARY KEY, + id INT PRIMARY KEY, article_id INT, user_id INT, lender_id INT,