Skip to content

Commit

Permalink
change to NewSqliteStorageSupplier
Browse files Browse the repository at this point in the history
since early MC versions don't support upsert by default
  • Loading branch information
HSGamer committed Nov 9, 2024
1 parent 3973c35 commit ee457cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public DataStorageBuilder(
this.defaultSupplier = () -> new FlatStorageSupplier<>(holderBaseFolder, flatEntryConverter);
register(defaultSupplier, "flat", "properties", "");
register(v -> new SqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "sqlite", "sqlite3");
register(v -> new OldSqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "old-sqlite");
register(v -> new NewSqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "new-sqlite", "new-sqlite3");
register(v -> new MySqlStorageSupplier<>(databaseSettingSupplier.get(), sqlEntryConverter), "mysql", "mysql-connector-java", "mysql-connector");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import java.io.File;

public class OldSqliteStorageSupplier<K, V> extends SqliteStorageSupplier<K, V> {
public OldSqliteStorageSupplier(DatabaseSetting databaseSetting, File holderBaseFolder, SqlEntryConverter<K, V> converter) {
public class NewSqliteStorageSupplier<K, V> extends SqliteStorageSupplier<K, V> {
public NewSqliteStorageSupplier(DatabaseSetting databaseSetting, File holderBaseFolder, SqlEntryConverter<K, V> converter) {
super(databaseSetting, holderBaseFolder, converter);
}

@Override
protected String toSaveStatement(String name, String[] keyColumns, String[] valueColumns) {
StringBuilder statement = new StringBuilder("INSERT OR REPLACE INTO `")
StringBuilder statement = new StringBuilder("INSERT INTO `")
.append(name)
.append("` (");
for (int i = 0; i < keyColumns.length + valueColumns.length; i++) {
Expand All @@ -30,7 +30,28 @@ protected String toSaveStatement(String name, String[] keyColumns, String[] valu
statement.append(", ");
}
}
statement.append(");");
statement.append(")");
statement.append(" ON CONFLICT (");
for (int i = 0; i < keyColumns.length; i++) {
statement.append("`")
.append(keyColumns[i])
.append("`");
if (i != keyColumns.length - 1) {
statement.append(", ");
}
}
statement.append(") DO UPDATE SET ");
for (int i = 0; i < valueColumns.length; i++) {
statement.append("`")
.append(valueColumns[i])
.append("` = EXCLUDED.`")
.append(valueColumns[i])
.append("`");
if (i != valueColumns.length - 1) {
statement.append(", ");
}
}
statement.append(";");
return statement.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void flushConnection(Connection connection) {

@Override
protected String toSaveStatement(String name, String[] keyColumns, String[] valueColumns) {
StringBuilder statement = new StringBuilder("INSERT INTO `")
StringBuilder statement = new StringBuilder("INSERT OR REPLACE INTO `")
.append(name)
.append("` (");
for (int i = 0; i < keyColumns.length + valueColumns.length; i++) {
Expand All @@ -61,28 +61,7 @@ protected String toSaveStatement(String name, String[] keyColumns, String[] valu
statement.append(", ");
}
}
statement.append(")");
statement.append(" ON CONFLICT (");
for (int i = 0; i < keyColumns.length; i++) {
statement.append("`")
.append(keyColumns[i])
.append("`");
if (i != keyColumns.length - 1) {
statement.append(", ");
}
}
statement.append(") DO UPDATE SET ");
for (int i = 0; i < valueColumns.length; i++) {
statement.append("`")
.append(valueColumns[i])
.append("` = EXCLUDED.`")
.append(valueColumns[i])
.append("`");
if (i != valueColumns.length - 1) {
statement.append(", ");
}
}
statement.append(";");
statement.append(");");
return statement.toString();
}

Expand Down

0 comments on commit ee457cc

Please sign in to comment.