Fixes getNextId method for empty databases. Adds some new saving methods. TODO: clean up this class

This commit is contained in:
ceze88 2023-07-24 12:23:10 +02:00
parent 9d94232ab5
commit 54ef2c0b29

View File

@ -51,8 +51,6 @@ public class DataManager {
@Deprecated @Deprecated
private static final Map<String, LinkedList<Runnable>> queues = new HashMap<>(); private static final Map<String, LinkedList<Runnable>> queues = new HashMap<>();
DataManager() { DataManager() {
this.databaseConfig = null; this.databaseConfig = null;
this.plugin = null; this.plugin = null;
@ -233,26 +231,49 @@ public class DataManager {
// }, () -> this.autoIncrementCache.put(prefixedTable, new AtomicInteger(1))); // }, () -> this.autoIncrementCache.put(prefixedTable, new AtomicInteger(1)));
// //
//recreate upper method using java 8 syntax //recreate upper method using java 8 syntax
Optional<Integer> max = context.select(DSL.max(DSL.field("id"))).from(prefixedTable).fetchOptional().map(record -> record.get(0, Integer.class)); try {
this.autoIncrementCache.put(prefixedTable, new AtomicInteger(max.orElse(1))); Optional<Integer> max = context.select(DSL.max(DSL.field("id"))).from(prefixedTable).fetchOptional().map(record -> record.get(0, Integer.class));
this.autoIncrementCache.put(prefixedTable, new AtomicInteger(max.orElse(1)));
} catch (Exception e) {
//Table is empty
this.autoIncrementCache.put(prefixedTable, new AtomicInteger(1));
}
}); });
} }
return this.autoIncrementCache.get(prefixedTable).incrementAndGet(); return this.autoIncrementCache.get(prefixedTable).incrementAndGet();
} }
//TODO: Fix/create javadocs for all methods
/** /**
* Saves the data to the database * Saves the data to the database
*/ */
public void save(Data data) { public void save(Data data) {
asyncPool.execute(() -> { asyncPool.execute(() -> {
databaseConnector.connectDSL(context -> { saveSync(data);
context.insertInto(DSL.table(getTablePrefix() + data.getTableName())) });
.set(data.serialize()) }
.onConflict(data.getId() != -1 ? DSL.field("id") : DSL.field("uuid")).doUpdate()
.set(data.serialize()) /**
.where(data.getId() != -1 ? DSL.field("id").eq(data.getId()) : DSL.field("uuid").eq(data.getUniqueId().toString())) * Saves the data to the database
.execute(); */
}); public void save(Data data, String idField, Object idValue) {
asyncPool.execute(() -> {
saveSync(data, idField, idValue);
});
}
/**
* Saves the data to the database
*/
public void saveSync(Data data, String idField, Object idValue) {
databaseConnector.connectDSL(context -> {
context.insertInto(DSL.table(getTablePrefix() + data.getTableName()))
.set(data.serialize())
.onConflict(DSL.field(idField)).doUpdate()
.set(data.serialize())
.where(DSL.field(idField).eq(idValue))
.execute();
}); });
} }
@ -275,18 +296,7 @@ public class DataManager {
*/ */
public void saveBatch(Collection<Data> dataBatch) { public void saveBatch(Collection<Data> dataBatch) {
asyncPool.execute(() -> { asyncPool.execute(() -> {
databaseConnector.connectDSL(context -> { saveBatchSync(dataBatch);
List<Query> queries = new ArrayList<>();
for (Data data : dataBatch) {
queries.add(context.insertInto(DSL.table(getTablePrefix() + data.getTableName()))
.set(data.serialize())
.onConflict(data.getId() != -1 ? DSL.field("id") : DSL.field("uuid")).doUpdate()
.set(data.serialize())
.where(data.getId() != -1 ? DSL.field("id").eq(data.getId()) : DSL.field("uuid").eq(data.getUniqueId().toString())));
}
context.batch(queries).execute();
});
}); });
} }
@ -313,11 +323,18 @@ public class DataManager {
*/ */
public void delete(Data data) { public void delete(Data data) {
asyncPool.execute(() -> { asyncPool.execute(() -> {
databaseConnector.connectDSL(context -> { deleteSync(data);
context.delete(DSL.table(getTablePrefix() + data.getTableName())) });
.where(data.getId() != -1 ? DSL.field("id").eq(data.getId()) : DSL.field("uuid").eq(data.getUniqueId().toString())) }
.execute();
}); /**
* Deletes the data from the database
*/
public void deleteSync(Data data) {
databaseConnector.connectDSL(context -> {
context.delete(DSL.table(getTablePrefix() + data.getTableName()))
.where(data.getId() != -1 ? DSL.field("id").eq(data.getId()) : DSL.field("uuid").eq(data.getUniqueId().toString()))
.execute();
}); });
} }