+ fix sql

This commit is contained in:
坏黑 2019-04-04 17:28:52 +08:00
parent 3ad414df45
commit 11d0a2aff3
10 changed files with 94 additions and 32 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group = 'me.skymc' group = 'me.skymc'
version = '4.75' version = '4.76'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View File

@ -1,5 +1,8 @@
package me.skymc.taboolib.common.serialize; package me.skymc.taboolib.common.serialize;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/** /**
* @Author 坏黑 * @Author 坏黑
* @Since 2019-03-08 17:28 * @Since 2019-03-08 17:28
@ -20,4 +23,12 @@ public interface TSerializable {
default String write() { default String write() {
return TSerializer.write(this); 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));
}
} }

View File

@ -29,10 +29,10 @@ public class TSerializer {
for (Map.Entry<String, JsonElement> jsonElementEntry : serializeObject.entrySet()) { for (Map.Entry<String, JsonElement> jsonElementEntry : serializeObject.entrySet()) {
try { try {
Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey()); Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey());
declaredField.setAccessible(true);
if (declaredField.isAnnotationPresent(DoNotSerialize.class)) { if (declaredField.isAnnotationPresent(DoNotSerialize.class)) {
continue; continue;
} }
declaredField.setAccessible(true);
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
if (serializer.isPresent()) { if (serializer.isPresent()) {
declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString())); declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString()));

View File

@ -1,9 +1,11 @@
package me.skymc.taboolib.common.serialize; package me.skymc.taboolib.common.serialize;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author 坏黑 * @Author 坏黑
@ -17,13 +19,13 @@ public class TSerializerExample {
// 修改参数 // 修改参数
date.number = 100; date.number = 100;
// 序列化 // 序列化
String value = date.write(); String value = date.writeBase64();
// 打印 // 打印
System.out.println(value); System.out.println(value);
// 创建新的对象 // 创建新的对象
SimpleData dataCopy = new SimpleData(); SimpleData dataCopy = new SimpleData();
// 反序列化 // 反序列化
dataCopy.read(value); dataCopy.readBase64(value);
// 打印 // 打印
System.out.println(dataCopy); System.out.println(dataCopy);
} }
@ -43,10 +45,14 @@ public class TSerializerExample {
/** /**
* 特殊类型需要进行手动序列化 * 特殊类型需要进行手动序列化
* 本工具提供了基本容器的序列化方法 * 标注 @TSerializeCollection @TSerializeMap 来进行自动序列化未完成
*/ */
// @TSerializeCollection
private List<Double> list = Lists.newArrayList(1.0, 2.0, 3.0); private List<Double> list = Lists.newArrayList(1.0, 2.0, 3.0);
// @TSerializeMap
private Map<String, String> map = ImmutableMap.of("abc", "def");
/** /**
* 跳过序列化 * 跳过序列化
*/ */
@ -59,24 +65,17 @@ public class TSerializerExample {
*/ */
@Override @Override
public void read(String fieldName, String value) { public void read(String fieldName, String value) {
switch (fieldName) { if (fieldName.equals("list")) {
case "list": {
// List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化 // List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化
TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE); TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE);
break;
}
default:
} }
} }
@Override @Override
public String write(String fieldName, Object value) { public String write(String fieldName, Object value) {
switch (fieldName) { if (fieldName.equals("list")) {
case "list": {
// 序列化同理
return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE); return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE);
} }
}
return null; return null;
} }

View File

@ -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<? extends Collection> type() default ArrayList.class;
Class<? extends TSerializerElement> element() default TSerializerElement.class;
}

View File

@ -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<? extends Map> type() default HashMap.class;
}

View File

@ -32,9 +32,7 @@ public class SQLColumn extends IColumn {
/** /**
* CHAR 类型常用构造器 * CHAR 类型常用构造器
* * new SQLColumn(SQLColumnType.CHAR, 1, "data");
* @param columnType
* @param columnName
*/ */
public SQLColumn(SQLColumnType columnType, int m, String columnName) { public SQLColumn(SQLColumnType columnType, int m, String columnName) {
this(columnType, m, 0, columnName, null); this(columnType, m, 0, columnName, null);
@ -56,6 +54,10 @@ public class SQLColumn extends IColumn {
this(columnType, 0, 0, columnName, defaultValue); this(columnType, 0, 0, columnName, defaultValue);
} }
public SQLColumn(SQLColumnType columnType, String columnName, Object defaultValue, SQLColumnOption... columnOptions) {
this(columnType, 0, 0, columnName, defaultValue, columnOptions);
}
/** /**
* 完整构造器 * 完整构造器
* *

View File

@ -49,11 +49,11 @@ public class SQLTable {
} }
public String deleteQuery() { public String deleteQuery() {
return Strings.replaceWithOrder("drop table if exists `{0}`" + tableName); return Strings.replaceWithOrder("drop table if exists `{0}`", tableName);
} }
public String cleanQuery() { public String cleanQuery() {
return Strings.replaceWithOrder("delete from `{0}`" + tableName); return Strings.replaceWithOrder("delete from `{0}`", tableName);
} }
public String truncateQuery() { public String truncateQuery() {

View File

@ -19,6 +19,7 @@ public class RunnableQuery {
private TaskStatement statement; private TaskStatement statement;
private TaskResult result; private TaskResult result;
private TaskResult resultNext; private TaskResult resultNext;
private TaskResult resultAutoNext;
private Connection connection; private Connection connection;
private boolean autoClose; private boolean autoClose;
private String query; private String query;
@ -47,6 +48,11 @@ public class RunnableQuery {
return this; return this;
} }
public RunnableQuery resultAutoNext(TaskResult result) {
this.resultAutoNext = result;
return this;
}
public RunnableQuery connection(Connection connection) { public RunnableQuery connection(Connection connection) {
return connection(connection, false); return connection(connection, false);
} }
@ -113,6 +119,12 @@ public class RunnableQuery {
return resultNext.execute(resultSet); return resultNext.execute(resultSet);
} else if (result != null) { } else if (result != null) {
return result.execute(resultSet); return result.execute(resultSet);
} else if (resultAutoNext != null) {
Object result = null;
while (resultSet.next()) {
result = resultAutoNext.execute(resultSet);
}
return result;
} else { } else {
return null; return null;
} }

View File

@ -30,16 +30,6 @@ public class SQLiteColumn extends IColumn {
this(columnType, 0, 0, columnName, null); 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); * 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); this(columnType, 0, 0, columnName, defaultValue);
} }
public SQLiteColumn(SQLiteColumnType columnType, String columnName, Object defaultValue, SQLiteColumnOption... columnOptions) {
this(columnType, 0, 0, columnName, defaultValue, columnOptions);
}
/** /**
* 完整构造器 * 完整构造器
* *