Skip to content

Commit

Permalink
sql fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nulli0n committed Jun 18, 2024
1 parent 8823d0e commit 6795c00
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -135,10 +136,34 @@ public void insert(@NotNull String table, @NotNull List<SQLValue> values) {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -55,9 +57,7 @@ protected void onShutdown() {

@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
Expand Down Expand Up @@ -151,13 +151,30 @@ public boolean isUserExists(@NotNull UUID uuid) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand Down Expand Up @@ -42,6 +44,11 @@ protected void onShutdown() {
this.scheduledSaves.clear();
}

@NotNull
private AbstractUserDataHandler<?, U> getDataHandler() {
return this.plugin.getData();
}

@NotNull
public abstract U createUserData(@NotNull UUID uuid, @NotNull String name);

Expand All @@ -53,8 +60,13 @@ public void loadOnlineUsers() {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.*;
Expand Down Expand Up @@ -83,6 +84,35 @@ public static void executeStatement(@NotNull AbstractConnector connector,
}
}

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,
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/su/nightexpress/nightcore/database/sql/SQLUtils.java
Original file line number Diff line number Diff line change
@@ -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 + "`";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,6 +55,24 @@ public UpdateQueryExecutor where(@NotNull List<SQLCondition> wheres) {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected MenuItem addExit(int[] slots) {
@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
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/su/nightexpress/nightcore/util/Colorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,22 @@ public static String tagPlainHex(@NotNull String str) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -38,7 +39,9 @@ public TextRoot(@NotNull String string, @NotNull TagPool tagPool) {
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6795c00

Please sign in to comment.