Started implementing a way to verify the db scheme prior to accessing
the db.
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
package de.katho.kBorrow.db;
|
package de.katho.kBorrow.db;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class sqliteConnector
|
* @class sqliteConnector
|
||||||
@@ -17,26 +19,46 @@ import java.sql.Statement;
|
|||||||
public class SqliteConnector implements DbConnector {
|
public class SqliteConnector implements DbConnector {
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private String dbHandle;
|
private String dbHandle;
|
||||||
|
private Hashtable<String, String> sqlScheme;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param pHandle This string contains the path to database file the connector has to use
|
* @param pHandle This string contains the path to database file the connector has to use
|
||||||
* @throws FileNotFoundException, SQLException
|
* @throws FileNotFoundException, SQLException
|
||||||
*/
|
*/
|
||||||
public SqliteConnector(String pHandle) {
|
public SqliteConnector(String pHandle) {
|
||||||
|
|
||||||
this.dbHandle = pHandle;
|
this.dbHandle = pHandle;
|
||||||
|
this.loadScheme();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
File dbFile = new File(this.dbHandle);
|
||||||
Class.forName("org.sqlite.JDBC");
|
Class.forName("org.sqlite.JDBC");
|
||||||
System.out.println(this.dbHandle);
|
|
||||||
|
if(dbFile.exists()){
|
||||||
|
if(dbFile.isFile()){
|
||||||
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
|
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
|
||||||
|
|
||||||
System.out.println(this.isConfigured());
|
//Pr<50>fe Schema
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new IOException("Provided db handle may not be a file but a directory or a symlink!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
dbFile.createNewFile();
|
||||||
|
|
||||||
|
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
|
||||||
|
//INitialisiere
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException e){
|
catch (ClassNotFoundException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,4 +78,49 @@ public class SqliteConnector implements DbConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadScheme(){
|
||||||
|
this.sqlScheme = new Hashtable<String, String>();
|
||||||
|
|
||||||
|
this.sqlScheme.put("kborrow",
|
||||||
|
"CREATE TABLE kborrow ("
|
||||||
|
+ "setting_name TEXT,"
|
||||||
|
+ "value INT"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
this.sqlScheme.put("article",
|
||||||
|
"CREATE TABLE article ("
|
||||||
|
+ "id INT PRIMARY KEY,"
|
||||||
|
+ "name TEXT NOT NULL,"
|
||||||
|
+ "description TEXT"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
this.sqlScheme.put("lender",
|
||||||
|
"CREATE TABLE lender ("
|
||||||
|
+ "id INT PRIMARY KEY,"
|
||||||
|
+ "name TEXT,"
|
||||||
|
+ "surname TEXT,"
|
||||||
|
+ "student_number INT,"
|
||||||
|
+ "comment TEXT"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
this.sqlScheme.put("user",
|
||||||
|
"CREATE TABLE user ("
|
||||||
|
+ "id INT PRIMARY KEY,"
|
||||||
|
+ "name TEXT,"
|
||||||
|
+ "surname TEXT"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
this.sqlScheme.put("lending",
|
||||||
|
"CREATE TABLE lending ("
|
||||||
|
+ "id INT PRIMARY KEY,"
|
||||||
|
+ "article_id INT,"
|
||||||
|
+ "user_id INT,"
|
||||||
|
+ "lender_id INT,"
|
||||||
|
+ "start_date DATE DEFAULT CURRENT_DATE,"
|
||||||
|
+ "expected_end_date DATE,"
|
||||||
|
+ "end_date DATE,"
|
||||||
|
+ "comment TEXT"
|
||||||
|
+ ")");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,5 @@ CREATE TABLE lending (
|
|||||||
start_date DATE DEFAULT current_date,
|
start_date DATE DEFAULT current_date,
|
||||||
expected_end_date DATE,
|
expected_end_date DATE,
|
||||||
end_date DATE,
|
end_date DATE,
|
||||||
comment
|
comment TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into kborrow
|
|
||||||
(setting_name, value)
|
|
||||||
VALUES
|
|
||||||
('is_configured', 1)
|
|
||||||
;
|
|
||||||
Reference in New Issue
Block a user