Utilize null safe equals

Dieser Commit ist enthalten in:
Lixfel 2023-01-15 20:07:01 +01:00
Ursprung c94ee5c55f
Commit 9276f79d93
3 geänderte Dateien mit 6 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -31,7 +31,7 @@ public class SelectStatement<T> extends Statement {
private final Table<T> table; private final Table<T> table;
SelectStatement(Table<T> table, String... kfields) { SelectStatement(Table<T> table, String... kfields) {
this(table, "SELECT " + Arrays.stream(table.fields).map(f -> f.identifier).collect(Collectors.joining(", ")) + " FROM " + table.name + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(" AND "))); this(table, "SELECT " + Arrays.stream(table.fields).map(f -> f.identifier).collect(Collectors.joining(", ")) + " FROM " + table.name + " WHERE " + Arrays.stream(kfields).map(f -> f + Statement.NULL_SAFE_EQUALS + "?").collect(Collectors.joining(" AND ")));
} }
public SelectStatement(Table<T> table, String sql) { public SelectStatement(Table<T> table, String sql) {

Datei anzeigen

@ -41,6 +41,7 @@ public class Statement implements AutoCloseable {
static final Consumer<Table<?>> schemaCreator; static final Consumer<Table<?>> schemaCreator;
static final String ON_DUPLICATE_KEY; static final String ON_DUPLICATE_KEY;
static final UnaryOperator<String> upsertWrapper; static final UnaryOperator<String> upsertWrapper;
public static final String NULL_SAFE_EQUALS;
private static final boolean MYSQL_MODE; private static final boolean MYSQL_MODE;
private static final boolean PRODUCTION_DATABASE; private static final boolean PRODUCTION_DATABASE;
@ -73,6 +74,7 @@ public class Statement implements AutoCloseable {
schemaCreator = table -> {}; schemaCreator = table -> {};
ON_DUPLICATE_KEY = " ON DUPLICATE KEY UPDATE "; ON_DUPLICATE_KEY = " ON DUPLICATE KEY UPDATE ";
upsertWrapper = f -> f + " = VALUES(" + f + ")"; upsertWrapper = f -> f + " = VALUES(" + f + ")";
NULL_SAFE_EQUALS = " <=> ";
} else { } else {
Connection connection; Connection connection;
@ -89,6 +91,7 @@ public class Statement implements AutoCloseable {
schemaCreator = Table::ensureExistanceInSqlite; schemaCreator = Table::ensureExistanceInSqlite;
ON_DUPLICATE_KEY = " ON CONFLICT DO UPDATE SET "; ON_DUPLICATE_KEY = " ON CONFLICT DO UPDATE SET ";
upsertWrapper = f -> f + " = " + f; upsertWrapper = f -> f + " = " + f;
NULL_SAFE_EQUALS = " IS ";
} }
} }

Datei anzeigen

@ -75,7 +75,7 @@ public class Table<T> {
} }
public Statement updateFields(String[] fields, String... kfields) { public Statement updateFields(String[] fields, String... kfields) {
return new Statement("UPDATE " + name + " SET " + Arrays.stream(fields).map(f -> f + " = ?").collect(Collectors.joining(", ")) + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(" AND "))); return new Statement("UPDATE " + name + " SET " + Arrays.stream(fields).map(f -> f + " = ?").collect(Collectors.joining(", ")) + " WHERE " + Arrays.stream(kfields).map(f -> f + Statement.NULL_SAFE_EQUALS + "?").collect(Collectors.joining(" AND ")));
} }
public Statement insert(String name) { public Statement insert(String name) {
@ -100,7 +100,7 @@ public class Table<T> {
} }
public Statement deleteFields(String... kfields) { public Statement deleteFields(String... kfields) {
return new Statement("DELETE FROM " + name + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(" AND "))); return new Statement("DELETE FROM " + name + " WHERE " + Arrays.stream(kfields).map(f -> f + Statement.NULL_SAFE_EQUALS + "?").collect(Collectors.joining(" AND ")));
} }
void ensureExistanceInSqlite() { void ensureExistanceInSqlite() {