|
|
|
@ -30,56 +30,16 @@ public class Database {
@@ -30,56 +30,16 @@ public class Database {
|
|
|
|
|
private final String sql; |
|
|
|
|
private final HashMap<String, List<Object>> where = new HashMap<>(); |
|
|
|
|
private final HashMap<String,Object> values = new HashMap<>(); |
|
|
|
|
private final HashMap<String,Object> setValues = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
public Request(String sql) { |
|
|
|
|
this.sql = sql; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request where(String key, Object ... values) { |
|
|
|
|
for (var val : values) where(key,val); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request where(String key, Collection<? extends Object> values) { |
|
|
|
|
for (var val : values) where(key,val); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Request where(String key, Object value) { |
|
|
|
|
var list = where.get(key); |
|
|
|
|
if (list == null) where.put(key,list = new ArrayList<>()); |
|
|
|
|
list.add(value); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request values(Map<String,Object> newValues) { |
|
|
|
|
values.putAll(newValues); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request values(String key, Object value) { |
|
|
|
|
values.put(key,value); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void run() throws SQLException { |
|
|
|
|
public ResultSet exec() throws SQLException { |
|
|
|
|
var sb = new StringBuilder(sql); |
|
|
|
|
var args = new ArrayList<>(); |
|
|
|
|
if (!values.isEmpty()){ |
|
|
|
|
var keys = new ArrayList<String>(); |
|
|
|
|
for (var entry : values.entrySet()) { |
|
|
|
|
keys.add(entry.getKey()); |
|
|
|
|
args.add(entry.getValue()); |
|
|
|
|
} |
|
|
|
|
sb.append("(") |
|
|
|
|
.append(String.join(", ",keys)) |
|
|
|
|
.append(")") |
|
|
|
|
.append(" VALUES "); |
|
|
|
|
var arr = new String[args.size()]; |
|
|
|
|
Arrays.fill(arr,"?"); |
|
|
|
|
var marks = String.join(", ",arr); |
|
|
|
|
sb.append("(").append(marks).append(")"); |
|
|
|
|
} |
|
|
|
|
if (!where.isEmpty()){ |
|
|
|
|
var clauses = new ArrayList<String>(); |
|
|
|
|
sb.append(" WHERE "); |
|
|
|
@ -97,19 +57,46 @@ public class Database {
@@ -97,19 +57,46 @@ 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)); |
|
|
|
|
} |
|
|
|
|
stmt.execute(); |
|
|
|
|
return stmt.executeQuery(); |
|
|
|
|
} catch (SQLException sqle) { |
|
|
|
|
throw new SQLException(t("Query '{}' failed:",sql),sqle); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public ResultSet exec() throws SQLException { |
|
|
|
|
public void run() throws SQLException { |
|
|
|
|
var sb = new StringBuilder(sql); |
|
|
|
|
var args = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
if (!setValues.isEmpty()){ |
|
|
|
|
var keys = new ArrayList<String>(); |
|
|
|
|
var expressions = new ArrayList<String>(); |
|
|
|
|
for (var entry : setValues.entrySet()) { |
|
|
|
|
expressions.add(" SET "+entry.getKey()+" = ?"); |
|
|
|
|
args.add(entry.getValue()); |
|
|
|
|
} |
|
|
|
|
sb.append(String.join(", ",expressions)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!values.isEmpty()){ |
|
|
|
|
var keys = new ArrayList<String>(); |
|
|
|
|
for (var entry : values.entrySet()) { |
|
|
|
|
keys.add(entry.getKey()); |
|
|
|
|
args.add(entry.getValue()); |
|
|
|
|
} |
|
|
|
|
sb.append("(") |
|
|
|
|
.append(String.join(", ",keys)) |
|
|
|
|
.append(")") |
|
|
|
|
.append(" VALUES "); |
|
|
|
|
var arr = new String[args.size()]; |
|
|
|
|
Arrays.fill(arr,"?"); |
|
|
|
|
var marks = String.join(", ",arr); |
|
|
|
|
sb.append("(").append(marks).append(")"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!where.isEmpty()){ |
|
|
|
|
var clauses = new ArrayList<String>(); |
|
|
|
|
sb.append(" WHERE "); |
|
|
|
@ -127,21 +114,61 @@ public class Database {
@@ -127,21 +114,61 @@ public class Database {
|
|
|
|
|
var sql = sb.toString(); |
|
|
|
|
LOG.debug(sql); |
|
|
|
|
try { |
|
|
|
|
var stmt = Database.this.conn.prepareStatement(sql); |
|
|
|
|
var stmt = conn.prepareStatement(sql); |
|
|
|
|
if (!args.isEmpty()) { |
|
|
|
|
for (int i = 0; i < args.size(); i++) stmt.setObject(i+1, args.get(i)); |
|
|
|
|
} |
|
|
|
|
return stmt.executeQuery(); |
|
|
|
|
stmt.execute(); |
|
|
|
|
} catch (SQLException sqle) { |
|
|
|
|
throw new SQLException(t("Query '{}' failed:",sql),sqle); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request set(String key, Object value) { |
|
|
|
|
setValues.put(key,value); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Request where(String key, Object ... values) { |
|
|
|
|
for (var val : values) where(key,val); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request where(String key, Collection<? extends Object> values) { |
|
|
|
|
for (var val : values) where(key,val); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Request where(String key, Object value) { |
|
|
|
|
var list = where.get(key); |
|
|
|
|
if (list == null) where.put(key,list = new ArrayList<>()); |
|
|
|
|
list.add(value); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request values(Map<String,Object> newValues) { |
|
|
|
|
values.putAll(newValues); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request values(String key, Object value) { |
|
|
|
|
values.put(key,value); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Database(Connection connection) { |
|
|
|
|
this.conn = connection; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Database assertTables() throws SQLException { |
|
|
|
|
if (!tableExists(User.TABLE_NAME)) User.createTable(); |
|
|
|
|
if (!tableExists(MailingList.TABLE_NAME)) MailingList.createTable(); |
|
|
|
|
if (!tableExists(ListMember.TABLE_NAME)) ListMember.createTable(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Database open() { |
|
|
|
|
if (singleton == null){ |
|
|
|
|
Configuration config = Configuration.instance(); |
|
|
|
@ -159,15 +186,10 @@ public class Database {
@@ -159,15 +186,10 @@ public class Database {
|
|
|
|
|
return singleton; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Database assertTables() throws SQLException { |
|
|
|
|
if (!tableExists(User.TABLE_NAME)) User.createTable(); |
|
|
|
|
if (!tableExists(MailingList.TABLE_NAME)) MailingList.createTable(); |
|
|
|
|
if (!tableExists(ListMember.TABLE_NAME)) ListMember.createTable(); |
|
|
|
|
return this; |
|
|
|
|
public Request query(String sql) { |
|
|
|
|
return new Request(sql); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean tableExists(String tbName) throws SQLException { |
|
|
|
|
try { |
|
|
|
|
ResultSet rs = query("SELECT EXISTS (SELECT name FROM sqlite_schema WHERE type='table' AND name='" + tbName + "')").exec(); |
|
|
|
@ -179,8 +201,4 @@ public class Database {
@@ -179,8 +201,4 @@ public class Database {
|
|
|
|
|
throw new SQLException(t("Was not able to check existence of table {}!",tbName),e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Request query(String sql) { |
|
|
|
|
return new Request(sql); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|