From 04da02260e91b759deb424fc72d042548a20acef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 14 Jun 2023 09:00:00 +0800 Subject: [PATCH] optimize SqlConsts.java --- .../mybatisflex/core/constant/SqlConsts.java | 27 +++++++---- .../core/dialect/impl/CommonsDialectImpl.java | 46 +++++++++---------- .../core/dialect/impl/OracleDialect.java | 6 +-- .../core/query/ArithmeticQueryColumn.java | 2 +- .../core/query/OperatorQueryCondition.java | 4 +- .../core/query/OperatorSelectCondition.java | 4 +- .../core/query/QueryCondition.java | 8 ++-- .../mybatisflex/core/query/UnionWrapper.java | 4 +- .../mybatisflex/core/query/WrapperUtil.java | 4 +- 9 files changed, 57 insertions(+), 48 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java index e4a54b86..7ed1ec6d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java @@ -35,12 +35,13 @@ public final class SqlConsts { public static final String PLACEHOLDER = "?"; public static final String PERCENT_SIGN = "%"; public static final String SINGLE_QUOTE = "'"; - public static final String LEFT_BRACKET = "("; - public static final String RIGHT_BRACKET = ")"; + public static final String BRACKET_LEFT = "("; + public static final String BRACKET_RIGHT = ")"; public static final String HINT_START = "/*+ "; + public static final String HINT_END = " */ "; + // === SQL 关键字 === - public static final String HINT_END = " */ "; public static final String AS = " AS "; public static final String OR = " OR "; public static final String END = " END"; @@ -65,13 +66,16 @@ public final class SqlConsts { public static final String INSERT_INTO = INSERT + INTO; public static final String DELETE_FROM = DELETE + FROM; public static final String SELECT_ALL_FROM = SELECT + ASTERISK + FROM; - public static final String INSERT_ALL = "INSERT ALL "; + // === Oracle SQl === + public static final String INSERT_ALL = "INSERT ALL "; public static final String INSERT_ALL_END = " SELECT 1 FROM DUAL"; - public static final String ON = " ON "; + + // === 联表查询关键字 === + public static final String ON = " ON "; public static final String JOIN = " JOIN "; public static final String UNION = " UNION "; public static final String UNION_ALL = " UNION ALL "; @@ -80,9 +84,10 @@ public final class SqlConsts { public static final String RIGHT_JOIN = " RIGHT JOIN "; public static final String INNER_JOIN = " INNER JOIN "; public static final String CROSS_JOIN = " CROSS JOIN "; - public static final String GT = " > "; + // === 逻辑符号 === + public static final String GT = " > "; public static final String GE = " >= "; public static final String LT = " < "; public static final String LE = " <= "; @@ -98,15 +103,17 @@ public final class SqlConsts { public static final String NOT_BETWEEN = " NOT BETWEEN "; public static final String EXISTS = " EXISTS "; public static final String NOT_EXISTS = " NOT EXISTS "; - public static final String ASC = " ASC"; + // === 排序相关关键字 === + public static final String ASC = " ASC"; public static final String DESC = " DESC"; public static final String NULLS_FIRST = " NULLS FIRST"; public static final String NULLS_LAST = " NULLS LAST"; - public static final String SUM = "SUM"; + // === SQL 函数名 === + public static final String SUM = "SUM"; public static final String MAX = "MIX"; public static final String MIN = "MIN"; public static final String AVG = "AVG"; @@ -115,9 +122,10 @@ public final class SqlConsts { public static final String MONTH = "MONTH"; public static final String COUNT = "COUNT"; public static final String CONVERT = "CONVERT"; - public static final String PLUS_SIGN = " + "; + // === 数学运算符 === + public static final String PLUS_SIGN = " + "; public static final String MINUS_SIGN = " - "; public static final String DIVISION_SIGN = " / "; public static final String MULTIPLICATION_SIGN = " * "; @@ -125,6 +133,7 @@ public final class SqlConsts { // === 其他拼接需要的字符串 === public static final String AND_PLACEHOLDER = BLANK + PLACEHOLDER + AND + PLACEHOLDER + BLANK; + private SqlConsts() { } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index e1d2062b..24ce58f2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -91,8 +91,8 @@ public class CommonsDialectImpl implements IDialect { sql.append(wrap(getRealSchema(schema))).append(REFERENCE); } sql.append(wrap(getRealTable(tableName))); - sql.append(LEFT_BRACKET).append(fields).append(RIGHT_BRACKET); - sql.append(VALUES).append(LEFT_BRACKET).append(questions).append(RIGHT_BRACKET); + sql.append(BRACKET_LEFT).append(fields).append(BRACKET_RIGHT); + sql.append(VALUES).append(BRACKET_LEFT).append(questions).append(BRACKET_RIGHT); return sql.toString(); } @@ -127,9 +127,9 @@ public class CommonsDialectImpl implements IDialect { sql.append(wrap(getRealSchema(schema))).append(REFERENCE); } sql.append(wrap(getRealTable(tableName))); - sql.append(BLANK).append(LEFT_BRACKET) + sql.append(BLANK).append(BRACKET_LEFT) .append(fields) - .append(RIGHT_BRACKET).append(BLANK); + .append(BRACKET_RIGHT).append(BLANK); sql.append(VALUES).append(questions); return sql.toString(); } @@ -170,14 +170,14 @@ public class CommonsDialectImpl implements IDialect { if (i > 0) { sql.append(OR); } - sql.append(LEFT_BRACKET); + sql.append(BRACKET_LEFT); for (int j = 0; j < primaryKeys.length; j++) { if (j > 0) { sql.append(AND); } sql.append(wrap(primaryKeys[j])).append(EQUALS_PLACEHOLDER); } - sql.append(RIGHT_BRACKET); + sql.append(BRACKET_RIGHT); } } // 单主键 @@ -318,7 +318,7 @@ public class CommonsDialectImpl implements IDialect { List unions = CPI.getUnions(queryWrapper); if (CollectionUtil.isNotEmpty(unions)) { - sqlBuilder.insert(0, LEFT_BRACKET).append(RIGHT_BRACKET); + sqlBuilder.insert(0, BRACKET_LEFT).append(BRACKET_RIGHT); for (UnionWrapper unionWrapper : unions) { unionWrapper.buildSql(sqlBuilder, this); } @@ -419,9 +419,9 @@ public class CommonsDialectImpl implements IDialect { } } - return sql.append(LEFT_BRACKET).append(sqlFields).append(RIGHT_BRACKET) + return sql.append(BRACKET_LEFT).append(sqlFields).append(BRACKET_RIGHT) .append(VALUES) - .append(LEFT_BRACKET).append(sqlValues).append(RIGHT_BRACKET) + .append(BRACKET_LEFT).append(sqlValues).append(BRACKET_RIGHT) .toString(); } @@ -434,14 +434,14 @@ public class CommonsDialectImpl implements IDialect { for (int i = 0; i < insertColumns.length; i++) { warpedInsertColumns[i] = wrap(insertColumns[i]); } - sql.append(LEFT_BRACKET) + sql.append(BRACKET_LEFT) .append(StringUtil.join(DELIMITER, warpedInsertColumns)) - .append(RIGHT_BRACKET); + .append(BRACKET_RIGHT); sql.append(VALUES); Map onInsertColumns = tableInfo.getOnInsertColumns(); for (int i = 0; i < entities.size(); i++) { - StringJoiner stringJoiner = new StringJoiner(DELIMITER, LEFT_BRACKET, RIGHT_BRACKET); + StringJoiner stringJoiner = new StringJoiner(DELIMITER, BRACKET_LEFT, BRACKET_RIGHT); for (String insertColumn : insertColumns) { if (onInsertColumns != null && onInsertColumns.containsKey(insertColumn)) { //直接读取 onInsert 配置的值,而不用 "?" 代替 @@ -509,7 +509,7 @@ public class CommonsDialectImpl implements IDialect { //多租户 if (ArrayUtil.isNotEmpty(tenantIdArgs)) { - deleteSQL = deleteSQL.replace(WHERE, WHERE + LEFT_BRACKET) + RIGHT_BRACKET; + deleteSQL = deleteSQL.replace(WHERE, WHERE + BRACKET_LEFT) + BRACKET_RIGHT; deleteSQL += AND + wrap(tableInfo.getTenantIdColumn()) + IN + buildQuestion(tenantIdArgs.length); } return deleteSQL; @@ -520,7 +520,7 @@ public class CommonsDialectImpl implements IDialect { sql.append(tableInfo.getWrapSchemaAndTableName(this)); sql.append(SET).append(wrap(logicDeleteColumn)).append(EQUALS).append(getLogicDeletedValue()); sql.append(WHERE); - sql.append(LEFT_BRACKET); + sql.append(BRACKET_LEFT); String[] primaryKeys = tableInfo.getPrimaryKeys(); @@ -530,14 +530,14 @@ public class CommonsDialectImpl implements IDialect { if (i > 0) { sql.append(OR); } - sql.append(LEFT_BRACKET); + sql.append(BRACKET_LEFT); for (int j = 0; j < primaryKeys.length; j++) { if (j > 0) { sql.append(AND); } sql.append(wrap(primaryKeys[j])).append(EQUALS_PLACEHOLDER); } - sql.append(RIGHT_BRACKET); + sql.append(BRACKET_RIGHT); } } // 单主键 @@ -550,7 +550,7 @@ public class CommonsDialectImpl implements IDialect { } } - sql.append(RIGHT_BRACKET).append(AND).append(wrap(logicDeleteColumn)).append(EQUALS).append(getLogicNormalValue()); + sql.append(BRACKET_RIGHT).append(AND).append(wrap(logicDeleteColumn)).append(EQUALS).append(getLogicNormalValue()); if (ArrayUtil.isNotEmpty(tenantIdArgs)) { sql.append(AND).append(wrap(tableInfo.getTenantIdColumn())).append(IN).append(buildQuestion(tenantIdArgs.length)); @@ -807,7 +807,7 @@ public class CommonsDialectImpl implements IDialect { String logicDeleteColumn = tableInfo.getLogicDeleteColumn(); Object[] tenantIdArgs = tableInfo.buildTenantIdArgs(); if (StringUtil.isNotBlank(logicDeleteColumn) || ArrayUtil.isNotEmpty(tenantIdArgs)) { - sql.append(LEFT_BRACKET); + sql.append(BRACKET_LEFT); } //多主键的场景 @@ -816,14 +816,14 @@ public class CommonsDialectImpl implements IDialect { if (i > 0) { sql.append(OR); } - sql.append(LEFT_BRACKET); + sql.append(BRACKET_LEFT); for (int j = 0; j < primaryKeys.length; j++) { if (j > 0) { sql.append(AND); } sql.append(wrap(primaryKeys[j])).append(EQUALS_PLACEHOLDER); } - sql.append(RIGHT_BRACKET); + sql.append(BRACKET_RIGHT); } } // 单主键 @@ -837,7 +837,7 @@ public class CommonsDialectImpl implements IDialect { } if (StringUtil.isNotBlank(logicDeleteColumn) || ArrayUtil.isNotEmpty(tenantIdArgs)) { - sql.append(RIGHT_BRACKET); + sql.append(BRACKET_RIGHT); } @@ -932,14 +932,14 @@ public class CommonsDialectImpl implements IDialect { protected String buildQuestion(int count) { - StringBuilder sb = new StringBuilder(LEFT_BRACKET); + StringBuilder sb = new StringBuilder(BRACKET_LEFT); for (int i = 0; i < count; i++) { sb.append(PLACEHOLDER); if (i != count - 1) { sb.append(DELIMITER); } } - sb.append(RIGHT_BRACKET); + sb.append(BRACKET_RIGHT); return sb.toString(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java index e8d1496a..cea033dc 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java @@ -94,10 +94,10 @@ public class OracleDialect extends CommonsDialectImpl { Map onInsertColumns = tableInfo.getOnInsertColumns(); for (int i = 0; i < entities.size(); i++) { sql.append(INTO).append(tableInfo.getWrapSchemaAndTableName(this)); - sql.append(BLANK).append(LEFT_BRACKET).append(StringUtil.join(DELIMITER, warpedInsertColumns)).append(RIGHT_BRACKET); + sql.append(BLANK).append(BRACKET_LEFT).append(StringUtil.join(DELIMITER, warpedInsertColumns)).append(BRACKET_RIGHT); sql.append(VALUES); - StringJoiner stringJoiner = new StringJoiner(DELIMITER, LEFT_BRACKET, RIGHT_BRACKET); + StringJoiner stringJoiner = new StringJoiner(DELIMITER, BRACKET_LEFT, BRACKET_RIGHT); for (String insertColumn : insertColumns) { if (onInsertColumns != null && onInsertColumns.containsKey(insertColumn)) { //直接读取 onInsert 配置的值,而不用 "?" 代替 @@ -147,7 +147,7 @@ public class OracleDialect extends CommonsDialectImpl { for (int i = 0; i < rows.size(); i++) { sql.append(INTO).append(tableNameWrap); - sql.append(BLANK).append(LEFT_BRACKET).append(fields).append(RIGHT_BRACKET); + sql.append(BLANK).append(BRACKET_LEFT).append(fields).append(BRACKET_RIGHT); sql.append(VALUES).append(questionStrings); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java index a28a94ab..c5780e64 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java @@ -116,7 +116,7 @@ public class ArithmeticQueryColumn extends QueryColumn { for (int i = 0; i < arithmeticInfos.size(); i++) { sql.append(arithmeticInfos.get(i).toSql(queryTables, dialect, i)); } - return SqlConsts.LEFT_BRACKET + sql + SqlConsts.RIGHT_BRACKET; + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java index 021af89c..bf50a815 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java @@ -49,9 +49,9 @@ public class OperatorQueryCondition extends QueryCondition { sql.append(prevEffectiveCondition.connector); } sql.append(operator) - .append(SqlConsts.LEFT_BRACKET) + .append(SqlConsts.BRACKET_LEFT) .append(childSql) - .append(SqlConsts.RIGHT_BRACKET); + .append(SqlConsts.BRACKET_RIGHT); } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java index f40792be..8d690475 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java @@ -51,9 +51,9 @@ public class OperatorSelectCondition extends QueryCondition { sql.append(prevEffectiveCondition.connector); } sql.append(operator) - .append(SqlConsts.LEFT_BRACKET) + .append(SqlConsts.BRACKET_LEFT) .append(childSql) - .append(SqlConsts.RIGHT_BRACKET); + .append(SqlConsts.BRACKET_RIGHT); } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java index ad43e364..32c7704f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java @@ -181,9 +181,9 @@ public class QueryCondition implements CloneSupport { } //子查询 else if (value instanceof QueryWrapper) { - sql.append(SqlConsts.LEFT_BRACKET) + sql.append(SqlConsts.BRACKET_LEFT) .append(dialect.buildSelectSql((QueryWrapper) value)) - .append(SqlConsts.RIGHT_BRACKET); + .append(SqlConsts.BRACKET_RIGHT); } //原生sql else if (value instanceof RawFragment) { @@ -228,14 +228,14 @@ public class QueryCondition implements CloneSupport { //in, not in else if (SqlConsts.IN.equals(logic) || SqlConsts.NOT_IN.equals(logic)) { int paramsCount = calculateValueArrayCount(); - sqlBuilder.append(SqlConsts.LEFT_BRACKET); + sqlBuilder.append(SqlConsts.BRACKET_LEFT); for (int i = 0; i < paramsCount; i++) { sqlBuilder.append(SqlConsts.PLACEHOLDER); if (i != paramsCount - 1) { sqlBuilder.append(SqlConsts.DELIMITER); } } - sqlBuilder.append(SqlConsts.RIGHT_BRACKET); + sqlBuilder.append(SqlConsts.BRACKET_RIGHT); } else { sqlBuilder.append(SqlConsts.PLACEHOLDER); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/UnionWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/UnionWrapper.java index c4d2223c..a67049cd 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/UnionWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/UnionWrapper.java @@ -61,9 +61,9 @@ public class UnionWrapper implements CloneSupport { public void buildSql(StringBuilder sqlBuilder, IDialect dialect) { sqlBuilder.append(key) - .append(SqlConsts.LEFT_BRACKET) + .append(SqlConsts.BRACKET_LEFT) .append(dialect.buildSelectSql(queryWrapper)) - .append(SqlConsts.RIGHT_BRACKET); + .append(SqlConsts.BRACKET_RIGHT); } @Override diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java index 5ad50d50..8c8624c2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java @@ -129,11 +129,11 @@ class WrapperUtil { static String withBracket(String sql) { - return SqlConsts.LEFT_BRACKET + sql + SqlConsts.RIGHT_BRACKET; + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT; } static String withAlias(String sql, String alias) { - return SqlConsts.LEFT_BRACKET + sql + SqlConsts.RIGHT_BRACKET + SqlConsts.AS + alias; + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + SqlConsts.AS + alias; } static String withAliasIf(String alias, IDialect dialect) {