sql fixes

This commit is contained in:
nulli0n committed 2024-06-18 15:23:22 +05:00
1 parent 8823d0eecd
commit 6795c00576
13 files changed
+231 -14

No files matched your search

@@ -11,6 +11,7 @@ import su.nightexpress.nightcore.database.sql.SQLCondition;
import su.nightexpress.nightcore.database.sql.SQLQueries;
import su.nightexpress.nightcore.database.sql.SQLValue;
import su.nightexpress.nightcore.database.sql.executor.*;
import su.nightexpress.nightcore.database.sql.query.UpdateQuery;
import su.nightexpress.nightcore.manager.AbstractManager;
import java.sql.Connection;
@@ -135,10 +136,34 @@ public abstract class AbstractDataHandler<P extends NightCorePlugin> extends Abs
InsertQueryExecutor.builder(table).values(values).execute(this.getConnector());
}
@Deprecated
public void update(@NotNull String table, @NotNull List<SQLValue> values, @NotNull SQLCondition... conditions) {
UpdateQueryExecutor.builder(table).values(values).where(conditions).execute(this.getConnector());
}
@NotNull
public UpdateQuery updateQuery(@NotNull String table, @NotNull List<SQLValue> values, @NotNull List<SQLCondition> conditions) {
return UpdateQuery.create(table, values, conditions);
}
public void executeUpdate(@NotNull String table, @NotNull List<SQLValue> values, @NotNull List<SQLCondition> conditions) {
this.executeUpdate(this.updateQuery(table, values, conditions));
}
public void executeUpdate(@NotNull UpdateQuery query) {
SQLQueries.executeUpdate(this.connector, query);
}
public void executeUpdates(@NotNull List<UpdateQuery> queries) {
SQLQueries.executeUpdates(this.connector, queries);
}
public void delete(@NotNull String table, @NotNull SQLCondition... conditions) {
DeleteQueryExecutor.builder(table).where(conditions).execute(this.getConnector());
}
@@ -10,6 +10,8 @@ import su.nightexpress.nightcore.database.sql.SQLQueries;
import su.nightexpress.nightcore.database.sql.SQLValue;
import su.nightexpress.nightcore.database.sql.column.ColumnType;
import su.nightexpress.nightcore.core.CoreConfig;
import su.nightexpress.nightcore.database.sql.query.UpdateQuery;
import su.nightexpress.nightcore.util.Lists;
import su.nightexpress.nightcore.util.TimeUtil;
import java.sql.ResultSet;
@@ -55,9 +57,7 @@ public abstract class AbstractUserDataHandler<P extends NightDataPlugin<U>, U ex
@Override
public void onSave() {
for (U user : this.plugin.getUserManager().getLoaded()) {
this.saveUser(user);
}
this.executeUpdates(this.plugin.getUserManager().getLoaded().stream().map(this::saveQuery).toList());
}
@Override
@@ -151,13 +151,30 @@ public abstract class AbstractUserDataHandler<P extends NightDataPlugin<U>, U ex
}
public void saveUser(@NotNull U user) {
/*List<SQLValue> values = new ArrayList<>();
values.add(COLUMN_USER_NAME.toValue(user.getName()));
values.add(COLUMN_USER_DATE_CREATED.toValue(user.getDateCreated()));
values.add(COLUMN_USER_LAST_ONLINE.toValue(user.getLastOnline()));
values.addAll(this.getSaveColumns(user));
this.update(this.tableUsers, values, SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId())));*/
this.executeUpdate(this.saveQuery(user));
}
@NotNull
public UpdateQuery saveQuery(@NotNull U user) {
List<SQLValue> values = new ArrayList<>();
values.add(COLUMN_USER_NAME.toValue(user.getName()));
values.add(COLUMN_USER_DATE_CREATED.toValue(user.getDateCreated()));
values.add(COLUMN_USER_LAST_ONLINE.toValue(user.getLastOnline()));
values.addAll(this.getSaveColumns(user));
this.update(this.tableUsers, values, SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId())));
List<SQLCondition> conditions = Lists.newList(
SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId()))
);
return this.updateQuery(this.tableUsers, values, conditions);
}
public void addUser(@NotNull U user) {
@@ -4,6 +4,8 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.NightDataPlugin;
import su.nightexpress.nightcore.database.sql.SQLQueries;
import su.nightexpress.nightcore.database.sql.query.UpdateQuery;
import su.nightexpress.nightcore.manager.AbstractManager;
import su.nightexpress.nightcore.core.CoreConfig;
import su.nightexpress.nightcore.database.listener.UserListener;
@@ -42,6 +44,11 @@ public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extend
this.scheduledSaves.clear();
}
@NotNull
private AbstractUserDataHandler<?, U> getDataHandler() {
return this.plugin.getData();
}
@NotNull
public abstract U createUserData(@NotNull UUID uuid, @NotNull String name);
@@ -53,8 +60,13 @@ public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extend
}
public void saveScheduled() {
this.scheduledSaves.forEach(this::save);
List<UpdateQuery> queries = new ArrayList<>();
this.scheduledSaves.forEach(user -> queries.add(this.getDataHandler().saveQuery(user)));
this.scheduledSaves.clear();
SQLQueries.executeUpdates(this.getDataHandler().getConnector(), queries);
//this.scheduledSaves.forEach(this::save);
//this.scheduledSaves.clear();
}
@NotNull
@@ -2,6 +2,7 @@ package su.nightexpress.nightcore.database.sql;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.database.AbstractConnector;
import su.nightexpress.nightcore.database.sql.query.UpdateQuery;
import java.sql.*;
import java.util.*;
@@ -83,6 +84,35 @@ public class SQLQueries {
}
}
public static void executeUpdate(@NotNull AbstractConnector connector, @NotNull UpdateQuery query) {
executeUpdates(connector, List.of(query));
}
public static void executeUpdates(@NotNull AbstractConnector connector, @NotNull List<UpdateQuery> queries) {
try (Connection connection = connector.getConnection()) {
for (UpdateQuery query : queries) {
try (PreparedStatement statement = connection.prepareStatement(query.getSQL())) {
int count = 1;
for (String columnValue : query.getValues()) {
statement.setString(count++, columnValue);
}
for (String conditionValue : query.getWheres()) {
statement.setString(count++, conditionValue);
}
statement.executeUpdate();
}
catch (SQLException exception) {
exception.printStackTrace();
}
}
}
catch (SQLException exception) {
exception.printStackTrace();
}
}
@NotNull
public static <T> List<@NotNull T> executeQuery(@NotNull AbstractConnector connector,
@NotNull String sql,
@@ -0,0 +1,14 @@
package su.nightexpress.nightcore.database.sql;
import org.jetbrains.annotations.NotNull;
public class SQLUtils {
@NotNull
public static String escape(@NotNull String string) {
if (string.isBlank()) return string;
if (string.charAt(0) == '`') return string;
return "`" + string + "`";
}
}
@@ -1,17 +1,20 @@
package su.nightexpress.nightcore.database.sql.executor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.database.AbstractConnector;
import su.nightexpress.nightcore.database.sql.SQLCondition;
import su.nightexpress.nightcore.database.sql.SQLExecutor;
import su.nightexpress.nightcore.database.sql.SQLQueries;
import su.nightexpress.nightcore.database.sql.SQLValue;
import su.nightexpress.nightcore.database.sql.query.UpdateQuery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Deprecated
public final class UpdateQueryExecutor extends SQLExecutor<Void> {
private final List<SQLValue> values;
@@ -52,6 +55,24 @@ public final class UpdateQueryExecutor extends SQLExecutor<Void> {
return this;
}
/*@Nullable
public UpdateQuery createObject() {
if (this.values.isEmpty()) return null;
String values = this.values.stream().map(value -> value.getColumn().getNameEscaped() + " = ?")
.collect(Collectors.joining(","));
String wheres = this.wheres.stream().map(where -> where.getColumn().getNameEscaped() + " " + where.getType().getOperator() + " ?")
.collect(Collectors.joining(" AND "));
String sql = "UPDATE " + this.getTable() + " SET " + values + (wheres.isEmpty() ? "" : " WHERE " + wheres);
List<String> values2 = this.values.stream().map(SQLValue::getValue).toList();
List<String> whers2 = this.wheres.stream().map(SQLCondition::getValue).map(SQLValue::getValue).toList();
return new UpdateQuery(sql, values2, whers2);
}*/
@Override
@NotNull
public Void execute(@NotNull AbstractConnector connector) {
@@ -0,0 +1,17 @@
package su.nightexpress.nightcore.database.sql.query;
import org.jetbrains.annotations.NotNull;
public class AbstractQuery {
private final String sql;
protected AbstractQuery(@NotNull String sql) {
this.sql = sql;
}
@NotNull
public String getSQL() {
return sql;
}
}
@@ -0,0 +1,66 @@
package su.nightexpress.nightcore.database.sql.query;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.database.sql.SQLCondition;
import su.nightexpress.nightcore.database.sql.SQLUtils;
import su.nightexpress.nightcore.database.sql.SQLValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UpdateQuery extends AbstractQuery {
private final List<String> values;
private final List<String> wheres;
private UpdateQuery(@NotNull String sql, @NotNull List<String> values, @NotNull List<String> wheres) {
super(sql);
this.values = values;
this.wheres = wheres;
}
@NotNull
public static UpdateQuery create(@NotNull String table, @NotNull List<SQLValue> values) {
return create(table, values, Collections.emptyList());
}
@NotNull
public static UpdateQuery create(@NotNull String table, @NotNull List<SQLValue> values, @NotNull List<SQLCondition> conditions) {
List<String> columnNames = new ArrayList<>();
List<String> columnValues = new ArrayList<>();
List<String> whereColumns = new ArrayList<>();
List<String> whereValues = new ArrayList<>();
values.forEach(value -> {
columnNames.add(value.getColumn().getNameEscaped() + " = ?");
columnValues.add(value.getValue());
});
conditions.forEach(condition -> {
whereColumns.add(condition.getColumn().getNameEscaped() + " " + condition.getType().getOperator() + " ?");
whereValues.add(condition.getValue().getValue());
});
StringBuilder builder = new StringBuilder();
builder.append("UPDATE ").append(SQLUtils.escape(table));
builder.append(" SET ").append(String.join(",", columnNames));
if (!whereColumns.isEmpty()) {
builder.append(" WHERE ").append(String.join(" AND ", whereColumns));
}
String sql = builder.toString();
return new UpdateQuery(sql, columnValues, whereValues);
}
@NotNull
public List<String> getValues() {
return values;
}
@NotNull
public List<String> getWheres() {
return wheres;
}
}
@@ -95,7 +95,7 @@ public abstract class EditorMenu<P extends NightCorePlugin, T> extends AbstractM
@NotNull
protected MenuItem addExit(int slot) {
ItemStack item = ItemUtil.getSkinHead("27548362a24c0fa8453e4d93e68c5969ddbde57bf6666c0319c1ed1e84d89065");
return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slot, (viewer, event, obj) -> ItemHandler.forClose(this));
return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slot, (viewer, event, obj) -> this.runNextTick(() -> viewer.getPlayer().closeInventory()));
}
@NotNull
@@ -154,10 +154,22 @@ public class Colorizer {
lastIndex = index + 1;
//if (builder.length() < 7) break;
if (index > 0 && builder.charAt(index - 1) == Tag.OPEN_BRACKET) continue;
if (builder.length() < index + 7) break;
int lookup = index + 7;
if (builder.length() < lookup) break;
String sub = builder.substring(index, index + 7);
Character prefix = index > 0 ? builder.charAt(index - 1) : null;
Character postfix = builder.length() > lookup ? builder.charAt(lookup) : null;
if (prefix != null && prefix == Tag.OPEN_BRACKET) continue;
if (postfix != null && postfix == Tag.CLOSE_BRACKET) continue;
if (prefix != null && postfix != null && prefix == ':' && postfix == ':') continue;
//if (index > 0 && builder.charAt(index - 1) == Tag.OPEN_BRACKET) continue;
//if (builder.length() < index + 7) break;
//if (builder.length() > index + 7 && builder.charAt(index + 7) == Tag.CLOSE_BRACKET) continue;
String sub = builder.substring(index, lookup);
try {
Integer.decode(sub);
}
@@ -54,9 +54,9 @@ public class Placeholders {
@NotNull
public static UnaryOperator<String> forLocation(@NotNull Location location) {
return new PlaceholderMap()
.add(LOCATION_X, () -> NumberUtil.format(location.getX()))
.add(LOCATION_Y, () -> NumberUtil.format(location.getY()))
.add(LOCATION_Z, () -> NumberUtil.format(location.getZ()))
.add(LOCATION_X, () -> String.valueOf(location.getBlockX()))
.add(LOCATION_Y, () -> String.valueOf(location.getBlockY()))
.add(LOCATION_Z, () -> String.valueOf(location.getBlockZ()))
.add(LOCATION_WORLD, () -> LocationUtil.getWorldName(location))
.replacer();
}
@@ -6,6 +6,7 @@ import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.core.CoreConfig;
import su.nightexpress.nightcore.util.Colorizer;
import su.nightexpress.nightcore.util.text.tag.TagPool;
import su.nightexpress.nightcore.util.text.tag.Tags;
@@ -38,7 +39,9 @@ public class TextRoot {
}
public void setString(@NotNull String string) {
string = Colorizer.tagPlainHex(Colorizer.plain(string));
if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) {
string = Colorizer.tagPlainHex(Colorizer.plain(string));
}
/*if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) {
TimedMatcher timedMatcher = TimedMatcher.create(Colorizer.PATTERN_HEX_LEGACY, string);
@@ -13,7 +13,7 @@ public class HexColorTag extends Tag implements ContentTag {
public static final String NAME = "color";
public HexColorTag() {
super(NAME);
super(NAME, new String[]{"c"});
}
@Override