From b51259cb91c2ee95f19836bf9ce4a35f3d16b72e Mon Sep 17 00:00:00 2001 From: redkale Date: Fri, 24 May 2024 17:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96DataNativeSqlParser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redkale/source/AbstractDataSqlSource.java | 48 +++++++++++-------- .../org/redkale/source/DataJdbcSource.java | 7 +-- .../redkale/source/DataNativeSqlParser.java | 2 +- .../source/DataNativeSqlStatement.java | 4 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/redkale/source/AbstractDataSqlSource.java b/src/main/java/org/redkale/source/AbstractDataSqlSource.java index bff23a486..b30e1e118 100644 --- a/src/main/java/org/redkale/source/AbstractDataSqlSource.java +++ b/src/main/java/org/redkale/source/AbstractDataSqlSource.java @@ -239,9 +239,11 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement if (slowmsError > 0 || slowmsWarn > 0) { long cost = System.currentTimeMillis() - startTime; if (slowmsError > 0 && cost > slowmsError) { - logger.log(Level.SEVERE, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') very slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls)); + logger.log(Level.SEVERE, DataSource.class.getSimpleName() + "(name='" + resourceName() + + "') very slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls)); } else if (slowmsWarn > 0 && cost > slowmsWarn) { - logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls)); + logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls)); } } } @@ -287,7 +289,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement if (!props.containsKey(DATA_SOURCE_TABLECOPY_SQLTEMPLATE)) { //注意:此语句复制表结构会导致默认值和主键信息的丢失 //注意:postgresql不支持跨库复制表结构 //props.setProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE #{newtable} AS (SELECT * FROM #{oldtable} LIMIT 0)"); - props.setProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE IF NOT EXISTS #{newtable} (LIKE #{oldtable} INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING COMMENTS INCLUDING INDEXES)"); + props.setProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, + "CREATE TABLE IF NOT EXISTS #{newtable} (LIKE #{oldtable} INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING COMMENTS INCLUDING INDEXES)"); } if (!props.containsKey(DATA_SOURCE_TABLENOTEXIST_SQLSTATES)) { props.setProperty(DATA_SOURCE_TABLENOTEXIST_SQLSTATES, "42P01;3F000"); @@ -306,7 +309,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement if (pos > 0) { url = url.substring(0, pos) + "..."; } - return getClass().getSimpleName() + "{url=" + url + ", maxconns=" + readMaxConns() + ", dbtype=" + dbtype() + ", " + DATA_SOURCE_TABLE_AUTODDL + "=" + autoDDL + executorToString() + "}"; + return getClass().getSimpleName() + "{url=" + url + ", maxconns=" + readMaxConns() + + ", dbtype=" + dbtype() + ", " + DATA_SOURCE_TABLE_AUTODDL + "=" + autoDDL + executorToString() + "}"; } else { String readUrl = readConfProps.getProperty(DATA_SOURCE_URL); int pos = readUrl.indexOf('?'); @@ -440,7 +444,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement sqlnull = "NOT NULL"; } } - } else if (column.type == java.time.LocalDate.class || column.type == java.util.Date.class || "java.sql.Date".equals(column.type.getName())) { + } else if (column.type == java.time.LocalDate.class + || column.type == java.util.Date.class || "java.sql.Date".equals(column.type.getName())) { sqltype = "DATE"; } else if (column.type == java.time.LocalTime.class || "java.sql.Time".equals(column.type.getName())) { sqltype = "TIME"; @@ -499,7 +504,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement sqltype = "INT4"; Number val = one == null ? null : (Number) info.getAttribute(column.field).get(one); sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val); - } else if (column.type == long.class || column.type == Long.class || column.type == AtomicLong.class || column.type == LongAdder.class) { + } else if (column.type == long.class || column.type == Long.class + || column.type == AtomicLong.class || column.type == LongAdder.class) { sqltype = "INT8"; Number val = one == null ? null : (Number) info.getAttribute(column.field).get(one); sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val); @@ -552,7 +558,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement if (!column.nullable) { sqlnull = "NOT NULL"; } - } else if (column.type == java.time.LocalDate.class || column.type == java.util.Date.class || "java.sql.Date".equals(column.type.getName())) { + } else if (column.type == java.time.LocalDate.class + || column.type == java.util.Date.class || "java.sql.Date".equals(column.type.getName())) { sqltype = "DATE"; } else if (column.type == java.time.LocalTime.class || "java.sql.Time".equals(column.type.getName())) { sqltype = "TIME"; @@ -569,7 +576,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement .append(column.primary && info.isAutoGenerated() ? "" : sqlnull); if (column.comment != null && !column.comment.isEmpty()) { //postgresql不支持DDL中直接带comment - comments.add("COMMENT ON COLUMN " + info.getOriginTable() + "." + column.column + " IS '" + column.comment.replace('\'', '"') + "'"); + comments.add("COMMENT ON COLUMN " + info.getOriginTable() + + "." + column.column + " IS '" + column.comment.replace('\'', '"') + "'"); } sb.append(",\n"); } @@ -653,7 +661,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement protected DataNativeSqlStatement nativeParse(String nativeSql, boolean countable, Map params) { if (nativeSqlParser == null) { - throw new SourceException("not found DataNativeSqlParser instance"); + throw new SourceException("not found " + DataNativeSqlParser.class.getSimpleName() + " instance"); } return nativeSqlParser.parse(signFunc, dbtype(), nativeSql, countable, params == null ? Collections.emptyMap() : params); } @@ -695,17 +703,17 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement protected abstract CompletableFuture insertDBAsync(final EntityInfo info, T... entitys); //删除记录 - protected abstract CompletableFuture deleteDBAsync(final EntityInfo info, + protected abstract CompletableFuture deleteDBAsync(final EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, final String... sqls); //清空表 - protected abstract CompletableFuture clearTableDBAsync(final EntityInfo info, String[] tables, FilterNode node, final String... sqls); + protected abstract CompletableFuture clearTableDBAsync(EntityInfo info, String[] tables, FilterNode node, String... sqls); //建表 - protected abstract CompletableFuture createTableDBAsync(final EntityInfo info, String copyTableSql, Serializable pk, final String... sqls); + protected abstract CompletableFuture createTableDBAsync(EntityInfo info, String copyTableSql, Serializable pk, String... sqls); //删除表 - protected abstract CompletableFuture dropTableDBAsync(final EntityInfo info, String[] tables, FilterNode node, final String... sqls); + protected abstract CompletableFuture dropTableDBAsync(EntityInfo info, String[] tables, FilterNode node, String... sqls); //更新纪录 protected abstract CompletableFuture updateEntityDBAsync(final EntityInfo info, T... entitys); @@ -1532,7 +1540,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } @Override - public CompletableFuture updateColumnAsync(final Class clazz, final Serializable pk, final String column, final Serializable colval) { + public CompletableFuture updateColumnAsync(Class clazz, Serializable pk, String column, Serializable colval) { final EntityInfo info = loadEntityInfo(clazz); if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, pk, column, colval)); @@ -1602,7 +1610,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } @Override - public CompletableFuture updateColumnAsync(final Class clazz, final String column, final Serializable colval, final FilterNode node) { + public CompletableFuture updateColumnAsync(Class clazz, String column, Serializable colval, FilterNode node) { final EntityInfo info = loadEntityInfo(clazz); if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, column, colval, node)); @@ -1627,7 +1635,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } } - protected UpdateSqlInfo updateColumnSql(final EntityInfo info, final String column, final Serializable colval, final FilterNode node) { + protected UpdateSqlInfo updateColumnSql(final EntityInfo info, String column, Serializable colval, FilterNode node) { Map joinTabalis = node.getJoinTabalis(); CharSequence join = node.createSQLJoin(this, true, joinTabalis, new HashSet<>(), info); CharSequence where = node.createSQLExpress(this, info, joinTabalis); @@ -1692,7 +1700,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } @Override - public CompletableFuture updateColumnAsync(final Class clazz, final Serializable pk, final ColumnValue... values) { + public CompletableFuture updateColumnAsync(final Class clazz, Serializable pk, ColumnValue... values) { if (values == null || values.length < 1) { return CompletableFuture.completedFuture(-1); } @@ -1776,7 +1784,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } @Override - public CompletableFuture updateColumnAsync(final Class clazz, final FilterNode node, final Flipper flipper, final ColumnValue... values) { + public CompletableFuture updateColumnAsync(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values) { if (values == null || values.length < 1) { return CompletableFuture.completedFuture(-1); } @@ -1804,7 +1812,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } } - protected UpdateSqlInfo updateColumnSql(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { + protected UpdateSqlInfo updateColumnSql(final EntityInfo info, FilterNode node, Flipper flipper, ColumnValue... values) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -2005,7 +2013,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement } } - protected UpdateSqlInfo updateColumnSql(final EntityInfo info, boolean needNode, T entity, FilterNode node, final SelectColumn selects) { + protected UpdateSqlInfo updateColumnSql(EntityInfo info, boolean needNode, T entity, FilterNode node, SelectColumn selects) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index 977c6b47b..994c7a57e 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -2539,9 +2539,10 @@ public class DataJdbcSource extends AbstractDataSqlSource { * 直接本地执行SQL语句进行查询,远程模式不可用
* 通常用于复杂的关联查询
* - * @param 泛型 - * @param sql SQL语句 - * @param handler 回调函数 + * @param 泛型 + * @param sql SQL语句 + * @param consumer 连接处理函数 + * @param handler 回调函数 * * @return 结果 */ diff --git a/src/main/java/org/redkale/source/DataNativeSqlParser.java b/src/main/java/org/redkale/source/DataNativeSqlParser.java index 8fb52290a..bfe5e5e20 100644 --- a/src/main/java/org/redkale/source/DataNativeSqlParser.java +++ b/src/main/java/org/redkale/source/DataNativeSqlParser.java @@ -13,7 +13,7 @@ import org.redkale.util.RedkaleClassLoader; * 原生的sql解析器
* 参数变量有三种方式(与Mybatis的占位符类似):
* ${xx.xx}: 用于直接拼接sql的变量,不做任何转义, 变量值必需的 - * #{xx.xx}: 用于预编译的sql的参数变量 + * #{xx.xx}: 用于预编译的sql的参数变量, UPDATE SET中的#{xx.xx}变量自动转成必需的 * ##{xx.xx}: 用于预编译的sql的参数变量, 变量值必需的 * *

diff --git a/src/main/java/org/redkale/source/DataNativeSqlStatement.java b/src/main/java/org/redkale/source/DataNativeSqlStatement.java index 7c480541c..3c14faf2d 100644 --- a/src/main/java/org/redkale/source/DataNativeSqlStatement.java +++ b/src/main/java/org/redkale/source/DataNativeSqlStatement.java @@ -25,13 +25,13 @@ public class DataNativeSqlStatement { static final DataNativeSqlParser PARSER_NIL = new DataNativeSqlParser() { @Override public DataNativeSqlInfo parse(IntFunction signFunc, String dbType, String rawSql) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("No available instances found"); } @Override public DataNativeSqlStatement parse(IntFunction signFunc, String dbType, String rawSql, boolean countable, Map params) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("No available instances found"); } };