From 03dff358461b7eed6f5a41f8693e5efdd5d36347 Mon Sep 17 00:00:00 2001 From: slowcheet4h <47327665+slowcheet4h@users.noreply.github.com> Date: Tue, 18 Oct 2022 22:30:48 +0300 Subject: [PATCH] ystring update --- pisi/unitedmeows/yystal/YYStal.java | 3 +- .../impl/sql/YexMySQLDriverError.java | 17 ++ .../exception/impl/sql/YexSqlError.java | 17 ++ pisi/unitedmeows/yystal/hook/YString.java | 76 ++++++++- .../advanced/server/YSocketChannel.java | 6 - .../yystal/sql/YDatabaseClient.java | 158 ------------------ .../yystal/sql/YDatabaseWorker.java | 46 +++++ pisi/unitedmeows/yystal/sql/YQueryResult.java | 83 +++++++++ pisi/unitedmeows/yystal/sql/YSQLClient.java | 62 +++++++ test/yystal/User.java | 12 ++ test/yystal/YTestStart.java | 44 ++--- 11 files changed, 319 insertions(+), 205 deletions(-) create mode 100644 pisi/unitedmeows/yystal/exception/impl/sql/YexMySQLDriverError.java create mode 100644 pisi/unitedmeows/yystal/exception/impl/sql/YexSqlError.java delete mode 100644 pisi/unitedmeows/yystal/sql/YDatabaseClient.java create mode 100644 pisi/unitedmeows/yystal/sql/YDatabaseWorker.java create mode 100644 pisi/unitedmeows/yystal/sql/YQueryResult.java create mode 100644 pisi/unitedmeows/yystal/sql/YSQLClient.java create mode 100644 test/yystal/User.java diff --git a/pisi/unitedmeows/yystal/YYStal.java b/pisi/unitedmeows/yystal/YYStal.java index 5c9e2ba..5e86753 100644 --- a/pisi/unitedmeows/yystal/YYStal.java +++ b/pisi/unitedmeows/yystal/YYStal.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.util.*; import java.util.function.Consumer; -import java.util.function.Supplier; public class YYStal { @@ -191,7 +190,7 @@ public static X setting(YSettings setting) { return (X) settings.get(setting); } - public static YString yString(String str) { return new YString(str); } + public static YString string(String str) { return new YString(str); } public static Thread mainThread() { return mainThread; diff --git a/pisi/unitedmeows/yystal/exception/impl/sql/YexMySQLDriverError.java b/pisi/unitedmeows/yystal/exception/impl/sql/YexMySQLDriverError.java new file mode 100644 index 0000000..3634b85 --- /dev/null +++ b/pisi/unitedmeows/yystal/exception/impl/sql/YexMySQLDriverError.java @@ -0,0 +1,17 @@ +package pisi.unitedmeows.yystal.exception.impl.sql; + +import pisi.unitedmeows.yystal.exception.YEx; + +public class YexMySQLDriverError extends YEx { + + private String message; + + public YexMySQLDriverError(String error) { + message = error; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/pisi/unitedmeows/yystal/exception/impl/sql/YexSqlError.java b/pisi/unitedmeows/yystal/exception/impl/sql/YexSqlError.java new file mode 100644 index 0000000..6744f35 --- /dev/null +++ b/pisi/unitedmeows/yystal/exception/impl/sql/YexSqlError.java @@ -0,0 +1,17 @@ +package pisi.unitedmeows.yystal.exception.impl.sql; + +import pisi.unitedmeows.yystal.exception.YEx; + +public class YexSqlError extends YEx { + + private String message; + + public YexSqlError(final String _message) { + message = _message; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/pisi/unitedmeows/yystal/hook/YString.java b/pisi/unitedmeows/yystal/hook/YString.java index 5c262dd..eae37e3 100644 --- a/pisi/unitedmeows/yystal/hook/YString.java +++ b/pisi/unitedmeows/yystal/hook/YString.java @@ -2,19 +2,24 @@ import pisi.unitedmeows.yystal.clazz.HookClass; import java.lang.StringBuilder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class YString extends HookClass { public static final String EMPTY_R = ""; public static final YString EMPTY = new YString(""); + private static final Pattern INLINE_VAR_PATTERN + = Pattern.compile("\\#\\[([a-zA-Z_][a-zA-Z0-9_]*).([a-zA-Z_][a-zA-Z0-9_]*)(\\(\\))?\\]"); + private boolean changed; private String currentValue; + private List holders = new LinkedList(); + private Map entities = new LinkedHashMap<>(); + public YString(StringBuilder stringBuilder) { hooked = stringBuilder; changed = true; @@ -103,10 +108,71 @@ public List lines() { @Override public String toString() { - return currentValue(); + String value = currentValue(); + if (changed) { + + while (true) { + Matcher matcher = INLINE_VAR_PATTERN.matcher(value); + if (!matcher.find()) + break; + String entityName = matcher.group(1); + String variable = matcher.group(2); + final Object entity = entities.getOrDefault(entityName, null); + if (entity != null) { + try { + Object data; + if (matcher.group().contains("()")) { + data = entity.getClass().getDeclaredMethod(variable).invoke(entity); + } else { + data = entity.getClass().getDeclaredField(variable).get(entity); + } + value = matcher.replaceFirst(data.toString()); + } catch (Exception e) { + e.printStackTrace(); + value = matcher.replaceFirst("?"); + } + + } + } + + StringBuilder builder = new StringBuilder(); + int holderIndex = 0; + boolean escapeNext = false; + for (char character : value.toCharArray()) { + + if (character == '\\') + escapeNext = true; + else { + if (character == '^') { + if (escapeNext) + builder.deleteCharAt(builder.length() - 1); + else { + builder.append(holders.get(holderIndex++)); + continue; + } + } + + escapeNext = false; + } + + builder.append(character); + } + value = builder.toString(); + } + return value; } protected String currentValue() { return changed ? (currentValue = hooked.toString()) : currentValue; } + + public YString entity(String name, Object entity) { + entities.put(name, entity); + return this; + } + + public YString put(Object data) { + holders.add(data); + return this; + } } diff --git a/pisi/unitedmeows/yystal/networking/advanced/server/YSocketChannel.java b/pisi/unitedmeows/yystal/networking/advanced/server/YSocketChannel.java index d260e17..cda5bc2 100644 --- a/pisi/unitedmeows/yystal/networking/advanced/server/YSocketChannel.java +++ b/pisi/unitedmeows/yystal/networking/advanced/server/YSocketChannel.java @@ -3,9 +3,7 @@ import pisi.unitedmeows.yystal.clazz.HookClass; import pisi.unitedmeows.yystal.exception.YExManager; import pisi.unitedmeows.yystal.exception.impl.YexIO; -import pisi.unitedmeows.yystal.networking.advanced.T; import pisi.unitedmeows.yystal.networking.advanced.server.pipeline.YServerChannelDataIn; -import pisi.unitedmeows.yystal.networking.simple.server.YSocketClient; import pisi.unitedmeows.yystal.utils.MemoryReader; import pisi.unitedmeows.yystal.utils.MemoryWriter; import pisi.unitedmeows.yystal.utils.kThread; @@ -16,10 +14,6 @@ import java.io.IOException; import java.net.Socket; import java.net.SocketException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; public class YSocketChannel extends HookClass { diff --git a/pisi/unitedmeows/yystal/sql/YDatabaseClient.java b/pisi/unitedmeows/yystal/sql/YDatabaseClient.java deleted file mode 100644 index 0234796..0000000 --- a/pisi/unitedmeows/yystal/sql/YDatabaseClient.java +++ /dev/null @@ -1,158 +0,0 @@ -package pisi.unitedmeows.yystal.sql; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import pisi.unitedmeows.yystal.utils.IDisposable; - -public class YDatabaseClient implements IDisposable { - private boolean connected; - private Connection connection; - private final java.lang.Object actionLock = new java.lang.Object(); - private HashMap> tableColumnsCache; - - public YDatabaseClient(String username, String password, String database, String host, int port) { - try { - synchronized (this) { - if (connection != null && !connection.isClosed()) return; - connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?characterEncoding=latin1&useConfigs=maxPerformance", username, password); - connected = true; - } - tableColumnsCache = new HashMap<>(); - } - catch (Exception ex) { - connected = false; - ex.printStackTrace(); - } - } - - public YDatabaseClient(String username, String password, String database, String host) { - this(username, password, database, host, 3306); - } - - public YDatabaseClient(String username, String password, String database) { - this(username, password, database, "localhost", 3306); - } - - public boolean execute(String sql) { - PreparedStatement command = null; - try { - command = connection.prepareStatement(sql); - return command.execute(); - } - catch (SQLException e) { - e.printStackTrace(); - return false; - } - } - - public boolean execute(YSQLCommand sql) { - return execute(sql.getHooked()); - } - - - - public List dbColumnsNoCache(String table) { - synchronized (actionLock) { - List> select = select(new YSQLCommand("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=^").putString(table), "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME", - "COLUMN_NAME"); - List columns = new ArrayList<>(); - for (int i = 0; i < select.size(); i++) { - columns.add((String) select.get(i).get("COLUMN_NAME")); - } - return columns; - } - } - - public List dbColumns(String table) { - List columns = tableColumnsCache.getOrDefault(table, null); - if (columns == null) { - tableColumnsCache.put(table, dbColumnsNoCache(table)); - return columns; - } - return tableColumnsCache.get(table); - } - - - public List> select(YSQLCommand sql) { - return select(sql.getHooked()); - } - - public List> select(String sql) { - synchronized (actionLock) { - try { - PreparedStatement command = connection.prepareStatement(sql); - ResultSet resultSet = command.executeQuery(); - List> list = new ArrayList<>(); - final int columnCount = command.getMetaData().getColumnCount(); - while (resultSet.next()) { - List dataList = new ArrayList<>(); - for (int i = 0; i < columnCount; i++) { - dataList.add(i, resultSet.getObject(i)); - } - list.add(dataList); - } - return list; - } - catch (SQLException e) { - e.printStackTrace(); - return new ArrayList<>(); - } - } - } - - public List> select(YSQLCommand sqlCommand, String... columnNames) { - return select(sqlCommand.getHooked(), columnNames); - } - - public List> select(String sql, String... columnNames) { - synchronized (actionLock) { - try { - PreparedStatement command = connection.prepareStatement(sql); - ResultSet resultSet = command.executeQuery(); - List> list = new ArrayList<>(); - while (resultSet.next()) { - Map dataMap = new HashMap<>(); - for (String column : columnNames) { - dataMap.put(column, resultSet.getObject(column)); - } - list.add(dataMap); - } - return list; - } - catch (SQLException e) { - e.printStackTrace(); - return new ArrayList<>(); - } - } - } - - public boolean connected() { - return connected; - } - - public Connection connection() { - return connection; - } - - @Override - public void close() { - try { - connection.close(); - } - catch (Exception ex) {} - connected = false; - tableColumnsCache.clear(); - } - /* public boolean insertMulti(String tableName, List> dataList, String... columns) { - String sql = "INSERT INTO " + tableName + "(" + String.join(",", columns) + " VALUES "; - - }*/ -} diff --git a/pisi/unitedmeows/yystal/sql/YDatabaseWorker.java b/pisi/unitedmeows/yystal/sql/YDatabaseWorker.java new file mode 100644 index 0000000..7e19486 --- /dev/null +++ b/pisi/unitedmeows/yystal/sql/YDatabaseWorker.java @@ -0,0 +1,46 @@ +package pisi.unitedmeows.yystal.sql; + +import pisi.unitedmeows.yystal.clazz.HookClass; +import pisi.unitedmeows.yystal.clazz.prop; +import pisi.unitedmeows.yystal.exception.YExManager; +import pisi.unitedmeows.yystal.exception.impl.sql.YexSqlError; +import pisi.unitedmeows.yystal.hook.YString; + +import java.sql.*; + +public class YDatabaseWorker extends HookClass { + + // connection = DriverManager.getConnection("jdbc:mysql://" + _host + ":" + _port + "/" + database + "?characterEncoding=latin1&useConfigs=maxPerformance", _username, _password); + + public final prop database; + private Connection connection; + private boolean connected; + + protected YDatabaseWorker(final YSQLClient owner, final String _database) { + hooked = owner; + database = new prop(_database); + + try { + /* not required */ synchronized (this) { + if (connection != null && !connection.isClosed()) return; + + /* change this to ystring */ + connection = DriverManager.getConnection("jdbc:mysql://" + owner.host + ":" + owner.port + "/" + database.get() + "?characterEncoding=latin1&useConfigs=maxPerformance", owner.username, owner.password); + connected = true; + } + } catch (Exception e) { + connected = false; + YExManager.pop(new YexSqlError("YDatabaseWorker couldn't connect to database (" + database.get() + ")")); + } + } + + public YQueryResult createQuery(String query) { + final String newQuery = query.startsWith("@") ? query : new YString(query).toString(); + try { + return new YQueryResult(connection.prepareStatement(newQuery)); + } catch (SQLException e) { + return null; + } + } + +} diff --git a/pisi/unitedmeows/yystal/sql/YQueryResult.java b/pisi/unitedmeows/yystal/sql/YQueryResult.java new file mode 100644 index 0000000..ab2dd75 --- /dev/null +++ b/pisi/unitedmeows/yystal/sql/YQueryResult.java @@ -0,0 +1,83 @@ +package pisi.unitedmeows.yystal.sql; + +import pisi.unitedmeows.yystal.clazz.HookClass; +import pisi.unitedmeows.yystal.exception.YExManager; +import pisi.unitedmeows.yystal.exception.impl.sql.YexSqlError; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class YQueryResult extends HookClass { + + private PreparedStatement preparedStatement; + + public YQueryResult(PreparedStatement statement) { + preparedStatement = statement; + } + + public YQueryResult execute() { + try { + hooked = preparedStatement.executeQuery(); + } catch (SQLException e) { + YExManager.pop(new YexSqlError("Couldn't execute query")); + return null; + } + + return this; + } + + public boolean hasNext() { + try { + return hooked.next(); + } catch (SQLException e) { + YExManager.pop(new YexSqlError("Couldn't go to next row")); + return false; + } + } + + public X get(String column) { + try { + return (X) hooked.getObject(column); + } catch (SQLException e) { + YExManager.pop(new YexSqlError("Couldn't get the column object")); + return null; + } + } + + public X get(int index) { + try { + return (X) hooked.getObject(index); + } catch (SQLException e) { + YExManager.pop(new YexSqlError("Couldn't get the column object")); + return null; + } + } + + public List> asList() { + try { + final int columnLength = hooked.getMetaData().getColumnCount(); + List> array = new ArrayList<>(); + while (hasNext()) { + List list = new ArrayList<>(columnLength); + for (int i = 0; i < columnLength; i++) { + list.add(get(i)); + } + array.add(list); + } + + return array; + } catch (SQLException ex) { + YExManager.pop(new YexSqlError("Couldn't get the table metadata")); + return new ArrayList<>(0); + } + } + + @Override + protected ResultSet getHooked() { + return super.getHooked(); + } +} diff --git a/pisi/unitedmeows/yystal/sql/YSQLClient.java b/pisi/unitedmeows/yystal/sql/YSQLClient.java new file mode 100644 index 0000000..b9ba685 --- /dev/null +++ b/pisi/unitedmeows/yystal/sql/YSQLClient.java @@ -0,0 +1,62 @@ +package pisi.unitedmeows.yystal.sql; + +import pisi.unitedmeows.yystal.exception.YExManager; +import pisi.unitedmeows.yystal.exception.impl.YexIO; +import pisi.unitedmeows.yystal.exception.impl.sql.YexMySQLDriverError; +import pisi.unitedmeows.yystal.networking.IPAddress; +import pisi.unitedmeows.yystal.utils.IDisposable; + +import java.util.HashMap; +import java.util.Map; + +public class YSQLClient implements IDisposable { + + // connection = DriverManager.getConnection("jdbc:mysql://" + _host + ":" + _port + "/" + database + "?characterEncoding=latin1&useConfigs=maxPerformance", _username, _password); + + // hide password and username + protected String username, password, host; // make prop + protected int port; + + private Map workers; + + public YSQLClient(final String _username, final String _password, final String _host, final int _port) { + username = _username; + password = _password; + host = _host; + port = _port; + + try { + // Trying to create an instance + Class.forName("com.mysql.jdbc.Driver").newInstance(); + } catch (Exception ex) { + YExManager.pop(new YexMySQLDriverError("MySQL Driver is not installed (error in YSQLClient constructor)")); + } + + workers = new HashMap<>(); + } + + public YSQLClient(final String _username, final String _password, final IPAddress _host, final int _port) { + this(_username, _password, _host.getAddress(), _port); + } + + public YSQLClient(final String _username, final String _password, final IPAddress _host) { + this(_username, _password, _host.getAddress()); + } + + public YSQLClient(final String _username, final String _password, final String _host) { + this(_username, _password, _host, 3306); + } + + public YSQLClient(final String _username, final String _password) { + this(_username, _password, "localhost"); + } + + public YDatabaseWorker worker(String database) { + return workers.computeIfAbsent(database, (name) -> new YDatabaseWorker(this, name)); + } + + @Override + public void close() { + + } +} \ No newline at end of file diff --git a/test/yystal/User.java b/test/yystal/User.java new file mode 100644 index 0000000..6a33cf0 --- /dev/null +++ b/test/yystal/User.java @@ -0,0 +1,12 @@ +package test.yystal; + +public class User { + + public String name = "George"; + public int a = 5; + + public String test() { + return "yystal"; + } + +} diff --git a/test/yystal/YTestStart.java b/test/yystal/YTestStart.java index a84579e..13c8daf 100644 --- a/test/yystal/YTestStart.java +++ b/test/yystal/YTestStart.java @@ -1,33 +1,9 @@ package test.yystal; import pisi.unitedmeows.yystal.YYStal; -import pisi.unitedmeows.yystal.exception.impl.YexIO; -import pisi.unitedmeows.yystal.file.YFile; +import pisi.unitedmeows.yystal.hook.YString; import pisi.unitedmeows.yystal.logger.impl.YLogger; -import pisi.unitedmeows.yystal.networking.IPAddress; -import pisi.unitedmeows.yystal.networking.advanced.server.YServerChannel; -import pisi.unitedmeows.yystal.networking.advanced.server.YServerChannelBuilder; -import pisi.unitedmeows.yystal.networking.simple.client.YTcpClient; -import pisi.unitedmeows.yystal.networking.simple.client.extension.impl.CTcpLineRead; -import pisi.unitedmeows.yystal.networking.simple.events.CDataReceivedEvent; -import pisi.unitedmeows.yystal.ui.YWindow; -import pisi.unitedmeows.yystal.ui.element.impl.YButton; -import pisi.unitedmeows.yystal.ui.element.simple.YBackgroundColor; -import pisi.unitedmeows.yystal.ui.utils.Vertex2f; -import pisi.unitedmeows.yystal.utils.MemoryWriter; -import pisi.unitedmeows.yystal.utils.kThread; -import pisi.unitedmeows.yystal.web.client.YWebClient; -import pisi.unitedmeows.yystal.web.client.YWebClientBuilder; -import pisi.unitedmeows.yystal.web.client.YWebResponse; -import test.yystal.stuff.YTestHandler; - -import java.awt.*; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; +import pisi.unitedmeows.yystal.sql.YSQLClient; public enum YTestStart { gaming; /* :D */ @@ -37,15 +13,15 @@ public enum YTestStart { .colored(true); public static void main(final String[] args) throws Exception { - YServerChannel channel = YServerChannelBuilder.create() - .build() - .handler(new YTestHandler()) - .bind(7772); + User user = new User(); + user.name = "gayhost"; - while (true) { - kThread.sleep(1000); - } + String string = YYStal.string( + "Hello my name is #[user.name] and i like #[user.test()] I'm ^ years old") + .entity("user", user) + .put("12").toString(); - } + + } }