|
@@ -23,6 +23,7 @@ public class Database {
|
|
|
|
|
|
static {
|
|
|
ini();
|
|
|
+ unset();
|
|
|
setup();
|
|
|
}
|
|
|
|
|
@@ -80,7 +81,7 @@ public class Database {
|
|
|
|
|
|
public static boolean query(String sql) {
|
|
|
try(Statement st = db.createStatement()) {
|
|
|
- st.executeQuery(sql);
|
|
|
+ st.executeQuery(prepare(sql));
|
|
|
return true;
|
|
|
} catch (SQLException | IllegalStateException e) {
|
|
|
LOGGER.warning(e.getMessage());
|
|
@@ -90,7 +91,7 @@ public class Database {
|
|
|
|
|
|
public static <T> Optional<T> query(String sql, ResultSetMapper<T> mapper) {
|
|
|
try(Statement st = db.createStatement()) {
|
|
|
- ResultSet result = st.executeQuery(sql);
|
|
|
+ ResultSet result = st.executeQuery(prepare(sql));
|
|
|
T obj = mapper.map(result);
|
|
|
return (obj == null) ? Optional.empty() : Optional.of(obj);
|
|
|
} catch (SQLException | IllegalStateException e) {
|
|
@@ -100,7 +101,7 @@ public class Database {
|
|
|
}
|
|
|
|
|
|
public static <T> Optional<T> query(String sql, List<Object> params, ResultSetMapper<T> mapper) {
|
|
|
- try(PreparedStatement st = db.prepareStatement(sql)) {
|
|
|
+ try(PreparedStatement st = db.prepareStatement(prepare(sql))) {
|
|
|
int i = 1;
|
|
|
for(Object param : params) {
|
|
|
st.setObject(i++, param);
|
|
@@ -116,7 +117,7 @@ public class Database {
|
|
|
|
|
|
public static boolean execute(String sql) {
|
|
|
try(Statement st = db.createStatement()) {
|
|
|
- int result = st.executeUpdate(sql);
|
|
|
+ int result = st.executeUpdate(prepare(sql));
|
|
|
return result == 1;
|
|
|
} catch (SQLException e) {
|
|
|
LOGGER.warning(e.getMessage());
|
|
@@ -125,7 +126,7 @@ public class Database {
|
|
|
}
|
|
|
|
|
|
public static boolean execute(String sql, List<Object> params) {
|
|
|
- try(PreparedStatement st = db.prepareStatement(sql)) {
|
|
|
+ try(PreparedStatement st = db.prepareStatement(prepare(sql))) {
|
|
|
int i = 1;
|
|
|
for (Object param : params) {
|
|
|
st.setObject(i++, param);
|
|
@@ -140,7 +141,7 @@ public class Database {
|
|
|
|
|
|
public static Map<String, Object> insert(String sql, List<Object> params, String[] idField) {
|
|
|
Map<String, Object> ids = new HashMap<>();
|
|
|
- try(PreparedStatement st = db.prepareStatement(sql, idField)) {
|
|
|
+ try(PreparedStatement st = db.prepareStatement(prepare(sql), idField)) {
|
|
|
|
|
|
int i = 1;
|
|
|
for(Object param : params) {
|
|
@@ -186,5 +187,11 @@ public class Database {
|
|
|
throw new IllegalStateException("Database Manager Class is not a child of ManagaDatabase Class or no getInstance method found", e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static String prepare(String sqlQuery) {
|
|
|
+ String result = sqlQuery.trim();
|
|
|
+ result = result.charAt(result.length() - 1) == ';' ? result.substring(0, result.length() - 1) : result;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
}
|