fixed database table creation

This commit is contained in:
2022-04-15 15:40:08 +02:00
parent d9a04ea788
commit abf9b814fe
5 changed files with 73 additions and 14 deletions

View File

@@ -18,7 +18,6 @@ public class Database {
public static final String SALT = "salt";
private static final Logger LOG = LoggerFactory.getLogger(Database.class);
private static final String VARCHAR = "VARCHAR(255)";
private static Database singleton = null;
private final Connection conn;
@@ -29,7 +28,7 @@ public class Database {
public class Request{
private final String sql;
private final HashMap<String,List<Object>> where = new HashMap<>();
private final HashMap<String, List<Object>> where = new HashMap<>();
private final HashMap<String,Object> values = new HashMap<>();
public Request(String sql) {
@@ -132,7 +131,8 @@ public class Database {
LOG.debug("Opening {}",url);
dbFile.getParentFile().mkdirs();
try {
singleton = new Database(DriverManager.getConnection(url)).assertTables();
singleton = new Database(DriverManager.getConnection(url));
singleton.assertTables(); // must not be concatenated to exception above (assertTables accesses singleton)!
} catch (SQLException sqle) {
sqle.printStackTrace();
}
@@ -141,13 +141,12 @@ public class Database {
}
private Database assertTables() throws SQLException {
if (!tableExists("Users")) createUsersTable();
if (!tableExists(User.TABLE_NAME)) User.createTable();
if (!tableExists(MailingList.TABLE_NAME)) MailingList.createTable();
return this;
}
private void createUsersTable() throws SQLException {
query("CREATE TABLE Users ("+EMAIL+" "+ VARCHAR +", "+SALT+" "+VARCHAR+", "+HASHED_PASS+" "+VARCHAR+", "+NAME+" "+VARCHAR+");").run();
}
private boolean tableExists(String tbName) throws SQLException {
try {