fixed database table creation
This commit is contained in:
@@ -21,6 +21,7 @@ public class Application {
|
|||||||
var config = Configuration.instance();
|
var config = Configuration.instance();
|
||||||
// the following construct allows the initial config file to point to another config file, which is then loaded:
|
// 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());
|
while (!config.configFile().equals(config.file())) config.load(config.configFile());
|
||||||
|
if (!config.configFile().exists()) config.save();
|
||||||
|
|
||||||
//startMailSystem(json);
|
//startMailSystem(json);
|
||||||
startWebserver();
|
startWebserver();
|
||||||
|
|||||||
@@ -11,10 +11,12 @@ public class Constants {
|
|||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
public static final String NOTES = "notes";
|
public static final String NOTES = "notes";
|
||||||
public static final String PASSWORD = "password";
|
public static final String PASSWORD = "password";
|
||||||
|
public static final Object PORT = "port";
|
||||||
public static final String PROTOCOL = "mail.store.protocol";
|
public static final String PROTOCOL = "mail.store.protocol";
|
||||||
public static final String USER = "user";
|
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 BASE_URL = "base_url";
|
||||||
public static final String DB = "database";
|
public static final String DB = "database";
|
||||||
public static final String BASE = "base";
|
public static final String BASE = "base";
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public class Database {
|
|||||||
public static final String SALT = "salt";
|
public static final String SALT = "salt";
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Database.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Database.class);
|
||||||
private static final String VARCHAR = "VARCHAR(255)";
|
|
||||||
private static Database singleton = null;
|
private static Database singleton = null;
|
||||||
private final Connection conn;
|
private final Connection conn;
|
||||||
|
|
||||||
@@ -132,7 +131,8 @@ public class Database {
|
|||||||
LOG.debug("Opening {}",url);
|
LOG.debug("Opening {}",url);
|
||||||
dbFile.getParentFile().mkdirs();
|
dbFile.getParentFile().mkdirs();
|
||||||
try {
|
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) {
|
} catch (SQLException sqle) {
|
||||||
sqle.printStackTrace();
|
sqle.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -141,13 +141,12 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Database assertTables() throws SQLException {
|
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;
|
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 {
|
private boolean tableExists(String tbName) throws SQLException {
|
||||||
try {
|
try {
|
||||||
|
|||||||
46
src/main/java/de/srsoftware/widerhall/data/MailingList.java
Normal file
46
src/main/java/de/srsoftware/widerhall/data/MailingList.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,10 +13,11 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static de.srsoftware.widerhall.data.Database.*;
|
import static de.srsoftware.widerhall.data.Database.*;
|
||||||
|
import static de.srsoftware.widerhall.Constants.*;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
|
public static final String TABLE_NAME = "Users";
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(User.class);
|
private static final Logger LOG = LoggerFactory.getLogger(User.class);
|
||||||
private static Database database = Database.open();
|
|
||||||
private String email, salt, hashedPass, name;
|
private String email, salt, hashedPass, name;
|
||||||
|
|
||||||
public User(String email, String name, String salt, String hashedPass) {
|
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();
|
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() {
|
public String email() {
|
||||||
return email;
|
return email;
|
||||||
@@ -51,7 +62,7 @@ public class User {
|
|||||||
public static List<User> list() {
|
public static List<User> list() {
|
||||||
var userList = new ArrayList<User>();
|
var userList = new ArrayList<User>();
|
||||||
try {
|
try {
|
||||||
var rs = database.query("SELECT * FROM Users").exec();
|
var rs = Database.open().query("SELECT * FROM Users").exec();
|
||||||
while (rs.next()){
|
while (rs.next()){
|
||||||
var email = rs.getString(EMAIL);
|
var email = rs.getString(EMAIL);
|
||||||
var name = rs.getString(NAME);
|
var name = rs.getString(NAME);
|
||||||
@@ -66,7 +77,7 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static User load(String email, String password) throws InvalidKeyException, SQLException {
|
public static User load(String email, String password) throws InvalidKeyException, SQLException {
|
||||||
ResultSet rs = database
|
ResultSet rs = Database.open()
|
||||||
.query("SELECT * FROM Users")
|
.query("SELECT * FROM Users")
|
||||||
.where(EMAIL,email)
|
.where(EMAIL,email)
|
||||||
.exec();
|
.exec();
|
||||||
@@ -102,7 +113,7 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean noUsers() throws SQLException {
|
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 {
|
try {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
return rs.getInt(1) < 1;
|
return rs.getInt(1) < 1;
|
||||||
@@ -114,7 +125,7 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private User save() throws SQLException {
|
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))
|
.values(Map.of(EMAIL,email,NAME,name,SALT,salt,HASHED_PASS,hashedPass))
|
||||||
.run();
|
.run();
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user