diff --git a/src/main/java/org/redkale/convert/AnyDecoder.java b/src/main/java/org/redkale/convert/AnyDecoder.java index b6bf1ad94..8401c10ab 100644 --- a/src/main/java/org/redkale/convert/AnyDecoder.java +++ b/src/main/java/org/redkale/convert/AnyDecoder.java @@ -19,7 +19,7 @@ import org.redkale.util.*; * * @author zhangjx */ -public class AnyDecoder implements Decodeable { +public class AnyDecoder implements Decodeable { private static final Type collectionObjectType = new TypeToken>() { }.getType(); @@ -29,13 +29,13 @@ public class AnyDecoder implements Decodeable { private static final Creator collectionCreator = Creator.create(ArrayList.class); - private static final Creator mapCreator = Creator.create(HashMap.class); + private static final Creator mapCreator = Creator.create(LinkedHashMap.class); - final Decodeable stringDecoder; + protected final Decodeable stringDecoder; - final CollectionDecoder collectionDecoder; + protected final CollectionDecoder collectionDecoder; - final MapDecoder mapDecoder; + protected final MapDecoder mapDecoder; /** * 构造函数 @@ -43,24 +43,33 @@ public class AnyDecoder implements Decodeable { * @param factory ConvertFactory */ public AnyDecoder(final ConvertFactory factory) { - this.stringDecoder = factory.loadDecoder(String.class); - this.collectionDecoder = new CollectionDecoder(factory, collectionObjectType, Object.class, collectionCreator, this); - this.mapDecoder = new MapDecoder(factory, mapObjectType, String.class, Object.class, mapCreator, stringDecoder, this); + this(mapCreator, mapObjectType, collectionCreator, collectionObjectType, factory.loadDecoder(String.class)); + } + + protected AnyDecoder(Creator mapCreator, Type mapObjectType, + Creator listCreator, Type listObjectType, Decodeable keyDecoder) { + this.stringDecoder = keyDecoder; + this.collectionDecoder = new CollectionDecoder(listObjectType, Object.class, listCreator, this); + this.mapDecoder = new MapDecoder(mapObjectType, String.class, Object.class, mapCreator, keyDecoder, this); } @Override - public Object convertFrom(Reader in) { + public T convertFrom(Reader in) { ValueType vt = in.readType(); if (vt == null) { return null; } switch (vt) { case ARRAY: - return this.collectionDecoder.convertFrom(in); + return (T) this.collectionDecoder.convertFrom(in); case MAP: - return this.mapDecoder.convertFrom(in); + return (T) this.mapDecoder.convertFrom(in); } - return this.stringDecoder.convertFrom(in); + return (T) stringFrom(in); + } + + protected T stringFrom(Reader in) { + return (T) this.stringDecoder.convertFrom(in); } @Override diff --git a/src/main/java/org/redkale/convert/CollectionDecoder.java b/src/main/java/org/redkale/convert/CollectionDecoder.java index d190d5544..6bc5e025e 100644 --- a/src/main/java/org/redkale/convert/CollectionDecoder.java +++ b/src/main/java/org/redkale/convert/CollectionDecoder.java @@ -66,7 +66,7 @@ public class CollectionDecoder implements Decodeable> { } //仅供类似JsonAnyDecoder这种动态创建使用, 不得调用 factory.register - public CollectionDecoder(final ConvertFactory factory, Type type, Type componentType, + public CollectionDecoder(Type type, Type componentType, Creator> creator, final Decodeable componentDecoder) { Objects.requireNonNull(componentDecoder); this.type = type; diff --git a/src/main/java/org/redkale/convert/MapDecoder.java b/src/main/java/org/redkale/convert/MapDecoder.java index b40c3ef2d..74ba2ba4e 100644 --- a/src/main/java/org/redkale/convert/MapDecoder.java +++ b/src/main/java/org/redkale/convert/MapDecoder.java @@ -80,7 +80,7 @@ public class MapDecoder implements Decodeable> { } //仅供类似JsonAnyDecoder这种动态创建使用, 不得调用 factory.register - public MapDecoder(final ConvertFactory factory, Type type, Type keyType, Type valueType, + public MapDecoder(Type type, Type keyType, Type valueType, Creator> creator, final Decodeable keyDecoder, Decodeable valueDecoder) { Objects.requireNonNull(keyDecoder); Objects.requireNonNull(valueDecoder); diff --git a/src/main/java/org/redkale/convert/json/JsonArray.java b/src/main/java/org/redkale/convert/json/JsonArray.java new file mode 100644 index 000000000..752c14f50 --- /dev/null +++ b/src/main/java/org/redkale/convert/json/JsonArray.java @@ -0,0 +1,60 @@ +/* + * + */ +package org.redkale.convert.json; + +import java.util.*; +import org.redkale.convert.ConvertDisabled; +import org.redkale.util.Utility; + +/** + * 常规json数组 + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public class JsonArray extends ArrayList implements JsonEntity { + + public JsonArray() { + } + + public JsonArray(Collection collection) { + super(collection); + } + + public JsonArray(Object... array) { + super(Arrays.asList(array)); + } + + public static JsonArray convertFrom(String text) { + return convertFrom(Utility.charArray(text)); + } + + public static JsonArray convertFrom(char[] text) { + return convertFrom(text, 0, text.length); + } + + public static JsonArray convertFrom(char[] text, final int offset, final int length) { + return (JsonArray) JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length)); + } + + @Override + @ConvertDisabled + public final boolean isObject() { + return false; + } + + @Override + @ConvertDisabled + public final boolean isArray() { + return true; + } + + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } +} diff --git a/src/main/java/org/redkale/convert/json/JsonEntity.java b/src/main/java/org/redkale/convert/json/JsonEntity.java new file mode 100644 index 000000000..1ac41e272 --- /dev/null +++ b/src/main/java/org/redkale/convert/json/JsonEntity.java @@ -0,0 +1,35 @@ +/* + * + */ +package org.redkale.convert.json; + +import org.redkale.util.Utility; + +/** + * 常规json实体 + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public interface JsonEntity extends java.io.Serializable { + + public boolean isObject(); + + public boolean isArray(); + + public static JsonEntity convertFrom(String text) { + return convertFrom(Utility.charArray(text)); + } + + public static JsonEntity convertFrom(char[] text) { + return convertFrom(text, 0, text.length); + } + + public static JsonEntity convertFrom(char[] text, final int offset, final int length) { + return JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length)); + } + +} diff --git a/src/main/java/org/redkale/convert/json/JsonEntityDecoder.java b/src/main/java/org/redkale/convert/json/JsonEntityDecoder.java new file mode 100644 index 000000000..392ab51ce --- /dev/null +++ b/src/main/java/org/redkale/convert/json/JsonEntityDecoder.java @@ -0,0 +1,43 @@ +/* + * + */ +package org.redkale.convert.json; + +import java.lang.reflect.Type; +import java.util.*; +import org.redkale.convert.AnyDecoder; +import org.redkale.convert.ext.StringSimpledCoder; +import org.redkale.util.*; + +/** + * 常规json + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +class JsonEntityDecoder extends AnyDecoder { + + private static final Type arrayType = new TypeToken>() { + }.getType(); + + private static final Type objectType = new TypeToken>() { + }.getType(); + + private static final Creator arrayCreator = Creator.create(JsonArray.class); + + private static final Creator objectCreator = Creator.create(JsonObject.class); + + public static final JsonEntityDecoder instance = new JsonEntityDecoder(); + + public JsonEntityDecoder() { + super(objectCreator, objectType, arrayCreator, arrayType, StringSimpledCoder.instance); + } + + @Override + public Type getType() { + return JsonEntity.class; + } +} diff --git a/src/main/java/org/redkale/convert/json/JsonObject.java b/src/main/java/org/redkale/convert/json/JsonObject.java new file mode 100644 index 000000000..eeed38bb7 --- /dev/null +++ b/src/main/java/org/redkale/convert/json/JsonObject.java @@ -0,0 +1,57 @@ +/* + * + */ +package org.redkale.convert.json; + +import java.util.*; +import org.redkale.convert.ConvertDisabled; +import org.redkale.util.Utility; + +/** + * 常规json对象 + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public class JsonObject extends LinkedHashMap implements JsonEntity { + + public JsonObject() { + } + + public JsonObject(Map map) { + super(map); + } + + public static JsonObject convertFrom(String text) { + return convertFrom(Utility.charArray(text)); + } + + public static JsonObject convertFrom(char[] text) { + return convertFrom(text, 0, text.length); + } + + public static JsonObject convertFrom(char[] text, final int offset, final int length) { + return (JsonObject) JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length)); + } + + @Override + @ConvertDisabled + public final boolean isObject() { + return true; + } + + @Override + @ConvertDisabled + public final boolean isArray() { + return false; + } + + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } + +} diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index ce83af6a5..7c0ddd94a 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -137,7 +137,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final List prestmts = new ArrayList<>(); for (Map.Entry> en : prepareInfos.entrySet()) { PrepareInfo prepareInfo = en.getValue(); - PreparedStatement prestmt = conn.prepareStatement(prepareInfo.prepareSql); + PreparedStatement prestmt = conn.prepareUpdateStatement(prepareInfo.prepareSql); for (final T value : prepareInfo.entitys) { bindStatementParameters(conn, prestmt, info, attrs, value); prestmt.addBatch(); @@ -149,7 +149,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { protected PreparedStatement prepareInsertEntityStatement(SourceConnection conn, String sql, EntityInfo info, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; - final PreparedStatement prestmt = conn.prepareStatement(sql); + final PreparedStatement prestmt = conn.prepareUpdateStatement(sql); for (final T value : entitys) { bindStatementParameters(conn, prestmt, info, attrs, value); prestmt.addBatch(); @@ -163,7 +163,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final List prestmts = new ArrayList<>(); for (Map.Entry> en : prepareInfos.entrySet()) { PrepareInfo prepareInfo = en.getValue(); - PreparedStatement prestmt = conn.prepareStatement(prepareInfo.prepareSql); + PreparedStatement prestmt = conn.prepareUpdateStatement(prepareInfo.prepareSql); for (final T value : prepareInfo.entitys) { int k = bindStatementParameters(conn, prestmt, info, attrs, value); prestmt.setObject(++k, primary.get(value)); @@ -177,7 +177,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { protected PreparedStatement prepareUpdateEntityStatement(SourceConnection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; - final PreparedStatement prestmt = conn.prepareStatement(prepareSQL); + final PreparedStatement prestmt = conn.prepareUpdateStatement(prepareSQL); for (final T value : entitys) { int k = bindStatementParameters(conn, prestmt, info, attrs, value); prestmt.setObject(++k, primary.get(value)); @@ -300,7 +300,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override public CompletableFuture batchAsync(final DataBatch batch) { - return CompletableFuture.supplyAsync(() -> batch(batch), getExecutor()); + return supplyAsync(() -> batch(batch)); } @Override @@ -385,7 +385,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { throw se; } //创建单表结构 - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -409,7 +409,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { }); try { //执行一遍创建分表操作 - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); for (String copySql : tableCopys) { st.addBatch(copySql); } @@ -421,7 +421,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { String[] tableSqls = createTableSqls(info); if (tableSqls != null) { //创建原始表 - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -432,7 +432,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } st.close(); //再执行一遍创建分表操作 - st = conn.createStatement(); + st = conn.createUpdateStatement(); for (String copySql : tableCopys) { st.addBatch(copySql); } @@ -442,7 +442,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } else { //需要先建库 Statement st; try { - st = conn.createStatement(); + st = conn.createUpdateStatement(); for (String newCatalog : newCatalogs) { st.addBatch(("postgresql".equals(dbtype()) ? "CREATE SCHEMA IF NOT EXISTS " : "CREATE DATABASE IF NOT EXISTS ") + newCatalog); } @@ -453,7 +453,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } try { //再执行一遍创建分表操作 - st = conn.createStatement(); + st = conn.createUpdateStatement(); for (String copySql : tableCopys) { st.addBatch(copySql); } @@ -463,7 +463,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (isTableNotExist(info, sqle2.getSQLState())) { String[] tableSqls = createTableSqls(info); if (tableSqls != null) { //创建原始表 - st = conn.createStatement(); + st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -474,7 +474,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } st.close(); //再执行一遍创建分表操作 - st = conn.createStatement(); + st = conn.createUpdateStatement(); for (String copySql : tableCopys) { st.addBatch(copySql); } @@ -618,12 +618,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { int c; if (sqls.length == 1) { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); int c1 = stmt.executeUpdate(sqls[0]); stmt.close(); c = c1; } else { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -651,7 +651,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.getTableStrategy() == null) { String[] tableSqls = createTableSqls(info); if (tableSqls != null) { - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -697,7 +697,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { logger.finest(info.getType().getSimpleName() + " delete sql=" + Arrays.toString(sqls)); } try { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -735,12 +735,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.setAutoCommit(false); int c; if (sqls.length == 1) { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); int c1 = stmt.executeUpdate(sqls[0]); stmt.close(); c = c1; } else { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -792,7 +792,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { logger.finest(info.getType().getSimpleName() + " clearTable sql=" + Arrays.toString(sqls)); } try { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -841,12 +841,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { int c; if (copyTableSql == null) { if (sqls.length == 1) { - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); int c1 = stmt.executeUpdate(sqls[0]); stmt.close(); c = c1; } else { - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -860,7 +860,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } else { //建分表 try { - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); c = stmt.executeUpdate(copyTableSql); } catch (SQLException se) { if (isTableNotExist(info, se.getSQLState())) { //分表的原始表不存在 @@ -870,7 +870,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { logger.finest(info.getType().getSimpleName() + " createTable sql=" + Arrays.toString(sqls)); } //创建原始表 - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); if (sqls.length == 1) { stmt.execute(sqls[0]); } else { @@ -884,7 +884,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.isLoggable(logger, Level.FINEST, copyTableSql)) { logger.finest(info.getType().getSimpleName() + " createTable sql=" + copyTableSql); } - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); c = stmt.executeUpdate(copyTableSql); stmt.close(); @@ -895,7 +895,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.isLoggable(logger, Level.FINEST, catalogSql)) { logger.finest(info.getType().getSimpleName() + " createCatalog sql=" + catalogSql); } - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); stmt.executeUpdate(catalogSql); stmt.close(); } catch (SQLException sqle1) { @@ -906,7 +906,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.isLoggable(logger, Level.FINEST, copyTableSql)) { logger.finest(info.getType().getSimpleName() + " createTable sql=" + copyTableSql); } - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); c = stmt.executeUpdate(copyTableSql); stmt.close(); } catch (SQLException sqle2) { @@ -915,7 +915,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { logger.finest(info.getType().getSimpleName() + " createTable sql=" + Arrays.toString(sqls)); } //创建原始表 - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); if (sqls.length == 1) { stmt.execute(sqls[0]); } else { @@ -929,7 +929,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.isLoggable(logger, Level.FINEST, copyTableSql)) { logger.finest(info.getType().getSimpleName() + " createTable sql=" + copyTableSql); } - stmt = conn.createStatement(); + stmt = conn.createUpdateStatement(); c = stmt.executeUpdate(copyTableSql); stmt.close(); } else { @@ -968,12 +968,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.setAutoCommit(false); int c; if (sqls.length == 1) { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); int c1 = stmt.executeUpdate(sqls[0]); stmt.close(); c = c1; } else { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -1025,7 +1025,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { logger.finest(info.getType().getSimpleName() + " dropTable sql=" + Arrays.toString(sqls)); } try { - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); for (String sql : sqls) { stmt.addBatch(sql); } @@ -1099,7 +1099,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { prestmt = prepareUpdateEntityStatement(conn, presql, info, entitys); } else { presql = caseSql; - prestmt = conn.prepareStatement(presql); + prestmt = conn.prepareUpdateStatement(presql); int len = entitys.length; final Attribute primary = info.getPrimary(); Attribute otherAttr = attrs[0]; @@ -1148,7 +1148,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { String[] tableSqls = createTableSqls(info); if (tableSqls != null) { try { - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -1305,7 +1305,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { if (sql.blobs != null || sql.tables != null) { if (sql.tables == null) { - final PreparedStatement prestmt = conn.prepareStatement(sql.sql); + final PreparedStatement prestmt = conn.prepareUpdateStatement(sql.sql); int index = 0; for (byte[] param : sql.blobs) { Blob blob = conn.createBlob(); @@ -1328,7 +1328,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { String[] sqls = new String[sql.tables.length]; for (int i = 0; i < sql.tables.length; i++) { sqls[i] = i == 0 ? sql.sql : sql.sql.replaceFirst(firstTable, sql.tables[i]); - PreparedStatement prestmt = conn.prepareStatement(sqls[i]); + PreparedStatement prestmt = conn.prepareUpdateStatement(sqls[i]); int index = 0; if (sql.blobs != null) { for (byte[] param : sql.blobs) { @@ -1362,7 +1362,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (info.isLoggable(logger, Level.FINEST, sql.sql)) { logger.finest(info.getType().getSimpleName() + " updateColumn sql=" + sql.sql); } - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); c = stmt.executeUpdate(sql.sql); stmt.close(); if (!batch) { @@ -1380,7 +1380,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { String[] tableSqls = createTableSqls(info); if (tableSqls != null) { try { - Statement st = conn.createStatement(); + Statement st = conn.createUpdateStatement(); if (tableSqls.length == 1) { st.execute(tableSqls[0]); } else { @@ -1421,7 +1421,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { String[] sqls = new String[sql.tables.length]; for (int i = 0; i < sql.tables.length; i++) { sqls[i] = sql.sql.replaceFirst(firstTable, sql.tables[i]); - PreparedStatement prestmt = conn.prepareStatement(sqls[i]); + PreparedStatement prestmt = conn.prepareUpdateStatement(sqls[i]); int index = 0; if (sql.blobs != null) { for (byte[] param : sql.blobs) { @@ -1470,7 +1470,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { Statement stmt = null; try { conn = readPool.pollConnection(); - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); if (set.next()) { int index = 0; @@ -1528,7 +1528,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (stmt != null) { stmt.close(); } - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); if (set.next()) { int index = 0; @@ -1572,7 +1572,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { Statement stmt = null; try { conn = readPool.pollConnection(); - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); Number rs = defVal; ResultSet set = stmt.executeQuery(sql); if (set.next()) { @@ -1623,7 +1623,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (stmt != null) { stmt.close(); } - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); Number rs = defVal; ResultSet set = stmt.executeQuery(sql); if (set.next()) { @@ -1662,7 +1662,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { Statement stmt = null; try { conn = readPool.pollConnection(); - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); ResultSetMetaData rsd = set.getMetaData(); boolean smallint = rsd == null ? false : rsd.getColumnType(1) == Types.SMALLINT; @@ -1712,7 +1712,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (stmt != null) { stmt.close(); } - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); ResultSetMetaData rsd = set.getMetaData(); boolean smallint = rsd == null ? false : rsd.getColumnType(1) == Types.SMALLINT; @@ -1749,7 +1749,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { Statement stmt = null; try { conn = readPool.pollConnection(); - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); ResultSetMetaData rsd = set.getMetaData(); boolean[] smallints = null; @@ -1814,7 +1814,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (stmt != null) { stmt.close(); } - stmt = conn.createStatement(); + stmt = conn.createQueryStatement(); ResultSet set = stmt.executeQuery(sql); ResultSetMetaData rsd = set.getMetaData(); boolean smallint = rsd == null ? false : rsd.getColumnType(1) == Types.SMALLINT; @@ -1863,7 +1863,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { conn = readPool.pollConnection(); String prepareSQL = info.getFindQuestionPrepareSQL(pk); - prestmt = conn.prepareStatement(prepareSQL); + prestmt = conn.prepareQueryStatement(prepareSQL); prestmt.setObject(1, pk); final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); T rs = set.next() ? info.getFullEntityValue(set) : null; @@ -1896,7 +1896,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { PreparedStatement prestmt = null; try { conn = readPool.pollConnection(); - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); T rs = set.next() ? selects == null ? info.getFullEntityValue(set) : info.getEntityValue(selects, set) : null; @@ -1942,7 +1942,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (prestmt != null) { prestmt.close(); } - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); T rs = set.next() ? selects == null ? info.getFullEntityValue(set) : info.getEntityValue(selects, set) : null; @@ -1976,7 +1976,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final Attribute attr = info.getAttribute(column); try { conn = readPool.pollConnection(); - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); Serializable val = defValue; @@ -2025,7 +2025,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (prestmt != null) { prestmt.close(); } - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); Serializable val = defValue; @@ -2061,7 +2061,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { PreparedStatement prestmt = null; try { conn = readPool.pollConnection(); - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); final ResultSet set = prestmt.executeQuery(); boolean rs = set.next() ? (set.getInt(1) > 0) : false; set.close(); @@ -2109,7 +2109,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (prestmt != null) { // prestmt.close(); } - prestmt = conn.prepareStatement(sql); + prestmt = conn.prepareQueryStatement(sql); final ResultSet set = prestmt.executeQuery(); boolean rs = set.next() ? (set.getInt(1) > 0) : false; set.close(); @@ -2144,7 +2144,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final List list = new ArrayList(); try { String prepareSQL = info.getFindQuestionPrepareSQL(ids[0]); - PreparedStatement prestmt = conn.prepareStatement(prepareSQL); + PreparedStatement prestmt = conn.prepareQueryStatement(prepareSQL); DataJdbcResultSet rr = new DataJdbcResultSet(info); for (Serializable pk : ids) { prestmt.setObject(1, pk); @@ -2198,7 +2198,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final List list = new ArrayList(); try { String prepareSQL = info.getAllQueryPrepareSQL(); - PreparedStatement prestmt = conn.prepareStatement(prepareSQL); + PreparedStatement prestmt = conn.prepareQueryStatement(prepareSQL); ResultSet set = prestmt.executeQuery(); final DataResultSet rr = createDataResultSet(info, set); while (set.next()) { @@ -2304,7 +2304,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { long s, SourceConnection conn, boolean mysqlOrPgsql, String listSql, String countSql) throws SQLException { final List list = new ArrayList(); if (mysqlOrPgsql) { //sql可以带limit、offset - PreparedStatement prestmt = conn.prepareStatement(listSql); + PreparedStatement prestmt = conn.prepareQueryStatement(listSql); ResultSet set = prestmt.executeQuery(); final DataResultSet rr = createDataResultSet(info, set); while (set.next()) { @@ -2314,7 +2314,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { prestmt.close(); long total = list.size(); if (needTotal) { - prestmt = conn.prepareStatement(countSql); + prestmt = conn.prepareQueryStatement(countSql); set = prestmt.executeQuery(); if (set.next()) { total = set.getLong(1); @@ -2325,7 +2325,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { slowLog(s, listSql); return new Sheet<>(total, list); } else { - PreparedStatement prestmt = conn.prepareStatement(listSql); + PreparedStatement prestmt = conn.prepareQueryStatement(listSql); if (flipper != null && flipper.getLimit() > 0) { prestmt.setFetchSize(flipper.getLimit()); } @@ -2488,7 +2488,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { SourceConnection conn = writePool.pollConnection(); try { conn.setAutoCommit(false); - final Statement stmt = conn.createStatement(); + final Statement stmt = conn.createUpdateStatement(); final int[] rs = new int[sqls.length]; int i = -1; for (String sql : sqls) { @@ -2530,7 +2530,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (logger.isLoggable(Level.FINEST)) { logger.finest("direct query sql=" + sql); } - final Statement statement = conn.createStatement(); + final Statement statement = conn.createQueryStatement(); //final PreparedStatement prestmt = conn.prepareStatement(sql); final ResultSet set = statement.executeQuery(sql);// prestmt.executeQuery(); V rs = handler.apply(createDataResultSet(null, set)); @@ -2898,11 +2898,25 @@ public class DataJdbcSource extends AbstractDataSqlSource { this.version = version; } - public Statement createStatement() throws SQLException { + public Statement createStreamStatement() throws SQLException { + Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); + statement.setFetchSize(Integer.MIN_VALUE); + return statement; + } + + public Statement createQueryStatement() throws SQLException { return conn.createStatement(); } - public PreparedStatement prepareStatement(String sql) throws SQLException { + public Statement createUpdateStatement() throws SQLException { + return conn.createStatement(); + } + + public PreparedStatement prepareQueryStatement(String sql) throws SQLException { + return conn.prepareStatement(sql); + } + + public PreparedStatement prepareUpdateStatement(String sql) throws SQLException { return conn.prepareStatement(sql); } diff --git a/src/test/java/org/redkale/test/convert/JsonMainTest.java b/src/test/java/org/redkale/test/convert/JsonMainTest.java index 9506cde1e..08e1ceac4 100644 --- a/src/test/java/org/redkale/test/convert/JsonMainTest.java +++ b/src/test/java/org/redkale/test/convert/JsonMainTest.java @@ -6,12 +6,9 @@ package org.redkale.test.convert; import java.io.*; -import org.redkale.convert.json.JsonConvert; -import org.redkale.convert.json.JsonFactory; -import java.nio.*; -import java.util.*; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import java.nio.ByteBuffer; +import java.util.Map; +import org.junit.jupiter.api.*; import org.redkale.convert.json.*; /** @@ -107,4 +104,40 @@ public class JsonMainTest { long v = convert.convertFrom(long.class, "100"); Assertions.assertEquals(100, v); } + + @Test + public void run6() throws Throwable { + String str = "{" + + " media : {" + + " uri : \"http://javaone.com/keynote.mpg\" ," + + " title : \"Javaone Keynote\" ," + + " width : -640 ," + + " height : -480 ," + + " format : \"video/mpg4\"," + + " duration : -18000000 ," + + " size : -58982400 ," + + " bitrate : -262144 ," + + " persons : [\"Bill Gates\", \"Steve Jobs\"] ," + + " player : JAVA , " + + " copyright : None" + + " }, images : [" + + " {" + + " uri : \"http://javaone.com/keynote_large.jpg\"," + + " title : \"Javaone Keynote\"," + + " width : -1024," + + " height : -768," + + " size : LARGE" + + " }, {" + + " uri : \"http://javaone.com/keynote_small.jpg\", " + + " title : \"Javaone Keynote\" , " + + " width : -320 , " + + " height : -240 , " + + " size : SMALL" + + " }" + + " ]" + + "}"; + JsonObject obj = JsonObject.convertFrom(str); + Assertions.assertEquals(JsonObject.class.getName(), obj.get("media").getClass().getName()); + Assertions.assertEquals(JsonArray.class.getName(), obj.get("images").getClass().getName()); + } }