From 11d0a2aff30f01b539d23cc9a8f528f1b17cf483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Thu, 4 Apr 2019 17:28:52 +0800 Subject: [PATCH] + fix sql --- build.gradle | 2 +- .../common/serialize/TSerializable.java | 11 +++++++ .../common/serialize/TSerializer.java | 2 +- .../common/serialize/TSerializerExample.java | 29 +++++++++---------- .../container/TSerializeCollection.java | 24 +++++++++++++++ .../serialize/container/TSerializeMap.java | 20 +++++++++++++ .../taboolib/mysql/builder/SQLColumn.java | 8 +++-- .../taboolib/mysql/builder/SQLTable.java | 4 +-- .../mysql/builder/query/RunnableQuery.java | 12 ++++++++ .../taboolib/mysql/sqlite/SQLiteColumn.java | 14 +++------ 10 files changed, 94 insertions(+), 32 deletions(-) create mode 100644 src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeCollection.java create mode 100644 src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeMap.java diff --git a/build.gradle b/build.gradle index fb7afbe..66ba5bb 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '4.75' +version = '4.76' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializable.java b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializable.java index 63a6fe1..f57ae2a 100644 --- a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializable.java +++ b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializable.java @@ -1,5 +1,8 @@ package me.skymc.taboolib.common.serialize; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + /** * @Author 坏黑 * @Since 2019-03-08 17:28 @@ -20,4 +23,12 @@ public interface TSerializable { default String write() { return TSerializer.write(this); } + + default Object readBase64(String value) { + return TSerializer.read(this, new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8)); + } + + default String writeBase64() { + return Base64.getEncoder().encodeToString(TSerializer.write(this).getBytes(StandardCharsets.UTF_8)); + } } \ No newline at end of file diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java index bbcf023..95b385d 100644 --- a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java +++ b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java @@ -29,10 +29,10 @@ public class TSerializer { for (Map.Entry jsonElementEntry : serializeObject.entrySet()) { try { Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey()); + declaredField.setAccessible(true); if (declaredField.isAnnotationPresent(DoNotSerialize.class)) { continue; } - declaredField.setAccessible(true); Optional serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); if (serializer.isPresent()) { declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString())); diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java index f77d673..d80333b 100644 --- a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java +++ b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java @@ -1,9 +1,11 @@ package me.skymc.taboolib.common.serialize; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import java.util.Collection; import java.util.List; +import java.util.Map; /** * @Author 坏黑 @@ -17,13 +19,13 @@ public class TSerializerExample { // 修改参数 date.number = 100; // 序列化 - String value = date.write(); + String value = date.writeBase64(); // 打印 System.out.println(value); // 创建新的对象 SimpleData dataCopy = new SimpleData(); // 反序列化 - dataCopy.read(value); + dataCopy.readBase64(value); // 打印 System.out.println(dataCopy); } @@ -43,10 +45,14 @@ public class TSerializerExample { /** * 特殊类型需要进行手动序列化 - * 本工具提供了基本容器的序列化方法 + * 标注 @TSerializeCollection 或 @TSerializeMap 来进行自动序列化(未完成) */ +// @TSerializeCollection private List list = Lists.newArrayList(1.0, 2.0, 3.0); +// @TSerializeMap + private Map map = ImmutableMap.of("abc", "def"); + /** * 跳过序列化 */ @@ -59,23 +65,16 @@ public class TSerializerExample { */ @Override public void read(String fieldName, String value) { - switch (fieldName) { - case "list": { - // List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化 - TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE); - break; - } - default: + if (fieldName.equals("list")) { + // List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化 + TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE); } } @Override public String write(String fieldName, Object value) { - switch (fieldName) { - case "list": { - // 序列化同理 - return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE); - } + if (fieldName.equals("list")) { + return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE); } return null; } diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeCollection.java b/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeCollection.java new file mode 100644 index 0000000..8b3a3f2 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeCollection.java @@ -0,0 +1,24 @@ +package me.skymc.taboolib.common.serialize.container; + +import me.skymc.taboolib.common.serialize.TSerializerElement; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.ArrayList; +import java.util.Collection; + +/** + * @Author sky + * @Since 2018-10-05 12:11 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface TSerializeCollection { + + Class type() default ArrayList.class; + + Class element() default TSerializerElement.class; + +} \ No newline at end of file diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeMap.java b/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeMap.java new file mode 100644 index 0000000..520d33f --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/common/serialize/container/TSerializeMap.java @@ -0,0 +1,20 @@ +package me.skymc.taboolib.common.serialize.container; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.HashMap; +import java.util.Map; + +/** + * @Author sky + * @Since 2018-10-05 12:11 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface TSerializeMap { + + Class type() default HashMap.class; + +} \ No newline at end of file diff --git a/src/main/scala/me/skymc/taboolib/mysql/builder/SQLColumn.java b/src/main/scala/me/skymc/taboolib/mysql/builder/SQLColumn.java index d8debe1..d154939 100644 --- a/src/main/scala/me/skymc/taboolib/mysql/builder/SQLColumn.java +++ b/src/main/scala/me/skymc/taboolib/mysql/builder/SQLColumn.java @@ -32,9 +32,7 @@ public class SQLColumn extends IColumn { /** * CHAR 类型常用构造器 - * - * @param columnType - * @param columnName + * new SQLColumn(SQLColumnType.CHAR, 1, "data"); */ public SQLColumn(SQLColumnType columnType, int m, String columnName) { this(columnType, m, 0, columnName, null); @@ -56,6 +54,10 @@ public class SQLColumn extends IColumn { this(columnType, 0, 0, columnName, defaultValue); } + public SQLColumn(SQLColumnType columnType, String columnName, Object defaultValue, SQLColumnOption... columnOptions) { + this(columnType, 0, 0, columnName, defaultValue, columnOptions); + } + /** * 完整构造器 * diff --git a/src/main/scala/me/skymc/taboolib/mysql/builder/SQLTable.java b/src/main/scala/me/skymc/taboolib/mysql/builder/SQLTable.java index 4e3412d..159e046 100644 --- a/src/main/scala/me/skymc/taboolib/mysql/builder/SQLTable.java +++ b/src/main/scala/me/skymc/taboolib/mysql/builder/SQLTable.java @@ -49,11 +49,11 @@ public class SQLTable { } public String deleteQuery() { - return Strings.replaceWithOrder("drop table if exists `{0}`" + tableName); + return Strings.replaceWithOrder("drop table if exists `{0}`", tableName); } public String cleanQuery() { - return Strings.replaceWithOrder("delete from `{0}`" + tableName); + return Strings.replaceWithOrder("delete from `{0}`", tableName); } public String truncateQuery() { diff --git a/src/main/scala/me/skymc/taboolib/mysql/builder/query/RunnableQuery.java b/src/main/scala/me/skymc/taboolib/mysql/builder/query/RunnableQuery.java index e76479f..c3f016d 100644 --- a/src/main/scala/me/skymc/taboolib/mysql/builder/query/RunnableQuery.java +++ b/src/main/scala/me/skymc/taboolib/mysql/builder/query/RunnableQuery.java @@ -19,6 +19,7 @@ public class RunnableQuery { private TaskStatement statement; private TaskResult result; private TaskResult resultNext; + private TaskResult resultAutoNext; private Connection connection; private boolean autoClose; private String query; @@ -47,6 +48,11 @@ public class RunnableQuery { return this; } + public RunnableQuery resultAutoNext(TaskResult result) { + this.resultAutoNext = result; + return this; + } + public RunnableQuery connection(Connection connection) { return connection(connection, false); } @@ -113,6 +119,12 @@ public class RunnableQuery { return resultNext.execute(resultSet); } else if (result != null) { return result.execute(resultSet); + } else if (resultAutoNext != null) { + Object result = null; + while (resultSet.next()) { + result = resultAutoNext.execute(resultSet); + } + return result; } else { return null; } diff --git a/src/main/scala/me/skymc/taboolib/mysql/sqlite/SQLiteColumn.java b/src/main/scala/me/skymc/taboolib/mysql/sqlite/SQLiteColumn.java index 4b7098b..c28cf07 100644 --- a/src/main/scala/me/skymc/taboolib/mysql/sqlite/SQLiteColumn.java +++ b/src/main/scala/me/skymc/taboolib/mysql/sqlite/SQLiteColumn.java @@ -30,16 +30,6 @@ public class SQLiteColumn extends IColumn { this(columnType, 0, 0, columnName, null); } - /** - * CHAR 类型常用构造器 - * - * @param columnType - * @param columnName - */ - public SQLiteColumn(SQLiteColumnType columnType, int m, String columnName) { - this(columnType, m, 0, columnName, null); - } - /** * 主键 类型常用构造器 * new SQLColumn(SQLiteColumnType.TEXT, "username", SQLiteColumnOption.PRIMARY_KEY, SQLiteColumnOption.AUTO_INCREMENT); @@ -56,6 +46,10 @@ public class SQLiteColumn extends IColumn { this(columnType, 0, 0, columnName, defaultValue); } + public SQLiteColumn(SQLiteColumnType columnType, String columnName, Object defaultValue, SQLiteColumnOption... columnOptions) { + this(columnType, 0, 0, columnName, defaultValue, columnOptions); + } + /** * 完整构造器 *