diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 66e1d27..5bc4251 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -4,7 +4,6 @@ import de.srsoftware.widerhall.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; @@ -12,17 +11,16 @@ import java.sql.SQLException; import java.util.*; import static de.srsoftware.widerhall.Util.t; +import static de.srsoftware.widerhall.Constants.*; public class Database { - public static final String EMAIL = "email"; - public static final String NAME = "name"; public static final String HASHED_PASS = "hashedPassword"; 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 static Connection conn; + private final Connection conn; public Request insertInto(String tbName) { return query("INSERT INTO "+tbName); @@ -31,8 +29,8 @@ public class Database { public class Request{ private final String sql; - private HashMap> where = new HashMap<>(); - private HashMap values = new HashMap<>(); + private final HashMap> where = new HashMap<>(); + private final HashMap values = new HashMap<>(); public Request(String sql) { this.sql = sql; @@ -45,7 +43,7 @@ public class Database { public Request where(String key, Object value) { var list = where.get(key); - if (list == null) where.put(key,list = new ArrayList()); + if (list == null) where.put(key,list = new ArrayList<>()); list.add(value); return this; } @@ -62,15 +60,17 @@ public class Database { public void run() throws SQLException { var sb = new StringBuilder(sql); - var args = new ArrayList(); + var args = new ArrayList<>(); if (!values.isEmpty()){ var keys = new ArrayList(); for (var entry : values.entrySet()) { keys.add(entry.getKey()); args.add(entry.getValue()); } - sb.append("("+String.join(", ",keys)+")"); - sb.append(" VALUES "); + sb.append("(") + .append(String.join(", ",keys)) + .append(")") + .append(" VALUES "); var arr = new String[args.size()]; Arrays.fill(arr,"?"); var marks = String.join(", ",arr); @@ -91,7 +91,7 @@ public class Database { public ResultSet exec() throws SQLException { var sb = new StringBuilder(sql); - var args = new ArrayList(); + var args = new ArrayList<>(); if (!where.isEmpty()){ var clauses = new ArrayList(); sb.append(" WHERE "); @@ -109,7 +109,7 @@ public class Database { var sql = sb.toString(); LOG.debug(sql); try { - var stmt = conn.prepareStatement(sql); + var stmt = Database.this.conn.prepareStatement(sql); if (!args.isEmpty()) { for (int i = 0; i < args.size(); i++) stmt.setObject(i+1, args.get(i)); }