From abf9b814fed64749c1d5d0901093960991618abb Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 15 Apr 2022 15:40:08 +0200 Subject: [PATCH] fixed database table creation --- .../de/srsoftware/widerhall/Application.java | 1 + .../de/srsoftware/widerhall/Constants.java | 4 +- .../srsoftware/widerhall/data/Database.java | 13 +++--- .../widerhall/data/MailingList.java | 46 +++++++++++++++++++ .../de/srsoftware/widerhall/data/User.java | 23 +++++++--- 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/srsoftware/widerhall/data/MailingList.java diff --git a/src/main/java/de/srsoftware/widerhall/Application.java b/src/main/java/de/srsoftware/widerhall/Application.java index b85bda8..cbd6686 100644 --- a/src/main/java/de/srsoftware/widerhall/Application.java +++ b/src/main/java/de/srsoftware/widerhall/Application.java @@ -21,6 +21,7 @@ public class Application { var config = Configuration.instance(); // the following construct allows the initial config file to point to another config file, which is then loaded: while (!config.configFile().equals(config.file())) config.load(config.configFile()); + if (!config.configFile().exists()) config.save(); //startMailSystem(json); startWebserver(); diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index c78c05d..a687e5e 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -11,10 +11,12 @@ public class Constants { public static final String NAME = "name"; public static final String NOTES = "notes"; public static final String PASSWORD = "password"; + public static final Object PORT = "port"; public static final String PROTOCOL = "mail.store.protocol"; public static final String USER = "user"; + public static final String VARCHAR = "VARCHAR(255)"; + - public static final Object PORT = "port"; public static final String BASE_URL = "base_url"; public static final String DB = "database"; public static final String BASE = "base"; diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 5bc4251..e2307df 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -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> where = new HashMap<>(); + private final HashMap> where = new HashMap<>(); private final HashMap 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 { diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java new file mode 100644 index 0000000..f91856d --- /dev/null +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -0,0 +1,46 @@ +package de.srsoftware.widerhall.data; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.validation.constraints.Email; + +import static de.srsoftware.widerhall.Constants.*; +import static de.srsoftware.widerhall.data.Database.HASHED_PASS; +import static de.srsoftware.widerhall.data.Database.SALT; + +import java.sql.SQLException; +import java.util.Map; + +public class MailingList { + private static final Logger LOG = LoggerFactory.getLogger(MailingList.class); + private final String name; + private final String email; + public static final String TABLE_NAME = "Lists"; + + public MailingList(String email, String name){ + this.email = email; + this.name = name; + } + + public static MailingList create(String email, String name) throws SQLException { + return new MailingList(email,name).save(); + } + + public static void createTable() throws SQLException { + var sql = new StringBuilder() + .append("CREATE TABLE ").append(TABLE_NAME) + .append(" (") + .append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") + .append(NAME).append(" ").append(VARCHAR) + .append(");"); + Database.open().query(sql.toString()).run(); + } + + private MailingList save() throws SQLException { + Database.open().insertInto(TABLE_NAME) + .values(Map.of(EMAIL,email,NAME,name)) + .run(); + return this; + } +} diff --git a/src/main/java/de/srsoftware/widerhall/data/User.java b/src/main/java/de/srsoftware/widerhall/data/User.java index 9421a16..d37f0e1 100644 --- a/src/main/java/de/srsoftware/widerhall/data/User.java +++ b/src/main/java/de/srsoftware/widerhall/data/User.java @@ -13,10 +13,11 @@ import java.util.List; import java.util.Map; import static de.srsoftware.widerhall.data.Database.*; +import static de.srsoftware.widerhall.Constants.*; public class User { + public static final String TABLE_NAME = "Users"; private static final Logger LOG = LoggerFactory.getLogger(User.class); - private static Database database = Database.open(); private String email, salt, hashedPass, name; public User(String email, String name, String salt, String hashedPass) { @@ -32,7 +33,17 @@ public class User { return new User(email,name,salt,hashedPass).save(); } - + public static void createTable() throws SQLException { + var sql = new StringBuilder() + .append("CREATE TABLE ").append(TABLE_NAME) + .append(" (") + .append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") + .append(SALT).append(" ").append(VARCHAR).append(", ") + .append(HASHED_PASS).append(" ").append(VARCHAR).append(", ") + .append(NAME).append(" ").append(VARCHAR) + .append(");"); + Database.open().query(sql.toString()).run(); + } public String email() { return email; @@ -51,7 +62,7 @@ public class User { public static List list() { var userList = new ArrayList(); try { - var rs = database.query("SELECT * FROM Users").exec(); + var rs = Database.open().query("SELECT * FROM Users").exec(); while (rs.next()){ var email = rs.getString(EMAIL); var name = rs.getString(NAME); @@ -66,7 +77,7 @@ public class User { } public static User load(String email, String password) throws InvalidKeyException, SQLException { - ResultSet rs = database + ResultSet rs = Database.open() .query("SELECT * FROM Users") .where(EMAIL,email) .exec(); @@ -102,7 +113,7 @@ public class User { } public static boolean noUsers() throws SQLException { - var rs = database.query("SELECT count(*) FROM users").exec(); + var rs = Database.open().query("SELECT count(*) FROM users").exec(); try { if (rs.next()) { return rs.getInt(1) < 1; @@ -114,7 +125,7 @@ public class User { } private User save() throws SQLException { - database.insertInto("Users") + Database.open().insertInto(TABLE_NAME) .values(Map.of(EMAIL,email,NAME,name,SALT,salt,HASHED_PASS,hashedPass)) .run(); return this;