mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Add templating system to account for differences in PSQL syntax
This commit is contained in:
1 parent
e065d78f2e
commit
5eb10014fa
5 files changed
+72
-24
No files matched your search
@@ -258,7 +258,7 @@ public final class DatabaseUserManager extends CachingUserManager {
|
|||||||
return new DatabaseUserManager(
|
return new DatabaseUserManager(
|
||||||
jdbi,
|
jdbi,
|
||||||
dataSource,
|
dataSource,
|
||||||
new QueriesLocator(),
|
new QueriesLocator(this.configManager.primaryConfig().storageType()),
|
||||||
this.logger,
|
this.logger,
|
||||||
this.profileResolver,
|
this.profileResolver,
|
||||||
this.playerInjector,
|
this.playerInjector,
|
||||||
|
|||||||
@@ -21,35 +21,72 @@ package net.draycia.carbon.common.users.db;
|
|||||||
|
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
|
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
import org.jdbi.v3.core.locator.ClasspathSqlLocator;
|
import org.jdbi.v3.core.locator.ClasspathSqlLocator;
|
||||||
import org.jdbi.v3.core.locator.internal.ClasspathBuilder;
|
import org.jdbi.v3.core.locator.internal.ClasspathBuilder;
|
||||||
|
|
||||||
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class QueriesLocator {
|
public final class QueriesLocator {
|
||||||
|
|
||||||
private static final String PREFIX = "queries/";
|
private static final String PREFIX = "queries/";
|
||||||
private static final Splitter SPLITTER = Splitter.on(';');
|
private static final Splitter SPLITTER = Splitter.on(';');
|
||||||
private final ClasspathSqlLocator locator = ClasspathSqlLocator.create();
|
private final ClasspathSqlLocator locator = ClasspathSqlLocator.create();
|
||||||
|
private final PrimaryConfig.StorageType storageType;
|
||||||
|
private final Pattern templatePattern = Pattern.compile("\\{([^}]*?)}");
|
||||||
|
private final Map<String, String> cache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public QueriesLocator() {
|
public QueriesLocator(final PrimaryConfig.StorageType storageType) {
|
||||||
|
this.storageType = storageType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NonNull String query(final @NonNull String name) {
|
public List<String> queries(final String name) {
|
||||||
|
return SPLITTER.splitToList(this.query(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String query(final String name) {
|
||||||
return this.locate(PREFIX + name);
|
return this.locate(PREFIX + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NonNull List<@NonNull String> queries(final @NonNull String name) {
|
private String locate(final String name) {
|
||||||
return SPLITTER.splitToList(this.locator.locate(PREFIX + name));
|
return this.cache.computeIfAbsent(name, $ -> {
|
||||||
|
final String sql = this.locator.getResource(
|
||||||
|
CarbonChat.class.getClassLoader(),
|
||||||
|
new ClasspathBuilder()
|
||||||
|
.appendDotPath(name)
|
||||||
|
.setExtension("sql")
|
||||||
|
.build());
|
||||||
|
return this.processTemplates(sql);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private String locate(final String name) {
|
private String processTemplates(final String sql) {
|
||||||
return this.locator.getResource(
|
return this.templatePattern.matcher(sql).replaceAll(match -> {
|
||||||
CarbonChat.class.getClassLoader(),
|
final String insideBraces = match.group(1);
|
||||||
new ClasspathBuilder()
|
try {
|
||||||
.appendDotPath(name)
|
final int colonIndex = insideBraces.indexOf(':');
|
||||||
.setExtension("sql")
|
String prefix = insideBraces.substring(0, colonIndex);
|
||||||
.build());
|
final String content = insideBraces.substring(colonIndex + 1);
|
||||||
|
boolean not = false;
|
||||||
|
if (prefix.startsWith("!")) {
|
||||||
|
not = true;
|
||||||
|
prefix = prefix.substring(1);
|
||||||
|
}
|
||||||
|
final PrimaryConfig.StorageType storageType = PrimaryConfig.StorageType.valueOf(prefix);
|
||||||
|
if (not) {
|
||||||
|
return storageType != this.storageType ? content : "";
|
||||||
|
} else {
|
||||||
|
return storageType == this.storageType ? content : "";
|
||||||
|
}
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
return match.group(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,21 @@
|
|||||||
INSERT IGNORE INTO carbon_users SET
|
INSERT{!PSQL: IGNORE} INTO carbon_users(
|
||||||
id = :id,
|
id,
|
||||||
muted = :muted,
|
muted,
|
||||||
deafened = :deafened,
|
deafened,
|
||||||
selectedchannel = :selectedchannel,
|
selectedchannel,
|
||||||
displayname = :displayname,
|
displayname,
|
||||||
lastwhispertarget = :lastwhispertarget,
|
lastwhispertarget,
|
||||||
whisperreplytarget = :whisperreplytarget,
|
whisperreplytarget,
|
||||||
spying = :spying,
|
spying,
|
||||||
ignoringdms = :ignoringdms;
|
ignoringdms
|
||||||
|
) VALUES (
|
||||||
|
:id,
|
||||||
|
:muted,
|
||||||
|
:deafened,
|
||||||
|
:selectedchannel,
|
||||||
|
:displayname,
|
||||||
|
:lastwhispertarget,
|
||||||
|
:whisperreplytarget,
|
||||||
|
:spying,
|
||||||
|
:ignoringdms
|
||||||
|
){PSQL: ON CONFLICT DO NOTHING};
|
||||||
@@ -1 +1 @@
|
|||||||
INSERT IGNORE INTO carbon_ignores (id, ignoredplayer) VALUES(:id, :ignoredplayer)
|
INSERT{!PSQL: IGNORE} INTO carbon_ignores (id, ignoredplayer) VALUES(:id, :ignoredplayer){PSQL: ON CONFLICT DO NOTHING};
|
||||||
@@ -1 +1 @@
|
|||||||
INSERT IGNORE INTO carbon_leftchannels (id, channel) VALUES(:id, :channel)
|
INSERT{!PSQL: IGNORE} INTO carbon_leftchannels (id, channel) VALUES(:id, :channel){PSQL: ON CONFLICT DO NOTHING};
|
||||||
Reference in new issue
Block a user