You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/05/11 11:09:26 UTC
[shardingsphere] branch master updated: little refactor about generate ddl sql (#17587)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 4005f5c04fb little refactor about generate ddl sql (#17587)
4005f5c04fb is described below
commit 4005f5c04fb371054cdf9492dac6f02b0de52f4b
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Wed May 11 19:09:15 2022 +0800
little refactor about generate ddl sql (#17587)
---
...Loader.java => AbstractPostgresDDLAdapter.java} | 6 +++---
.../ddlgenerator/PostgreDDLGenerator.java | 22 +++++++++++++---------
....java => PostgresColumnPropertiesAppender.java} | 12 ++++++------
... => PostgresConstraintsPropertiesAppender.java} | 12 ++++++------
...xLoader.java => PostgresIndexSqlGenerator.java} | 10 +++++-----
.../PostgresTablePropertiesLoader.java | 2 +-
.../core/database/visitor/SQLVisitorRule.java | 4 ++--
7 files changed, 36 insertions(+), 32 deletions(-)
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresAbstractLoader.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/AbstractPostgresDDLAdapter.java
similarity index 94%
rename from shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresAbstractLoader.java
rename to shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/AbstractPostgresDDLAdapter.java
index 8e8eef54160..eb9f8a115f4 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresAbstractLoader.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/AbstractPostgresDDLAdapter.java
@@ -35,10 +35,10 @@ import java.util.Map;
import java.util.stream.Collectors;
/**
- * Postgres abstract loader.
+ * Abstract postgres ddl adapter.
*/
@Getter
-public abstract class PostgresAbstractLoader {
+public abstract class AbstractPostgresDDLAdapter {
private static final String SECURITY_LABEL_SPLIT = "=";
@@ -48,7 +48,7 @@ public abstract class PostgresAbstractLoader {
private final int minorVersion;
- protected PostgresAbstractLoader(final Connection connection, final int majorVersion, final int minorVersion) {
+ protected AbstractPostgresDDLAdapter(final Connection connection, final int majorVersion, final int minorVersion) {
this.connection = connection;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreDDLGenerator.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreDDLGenerator.java
index e5be002112d..dd046166000 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreDDLGenerator.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreDDLGenerator.java
@@ -37,23 +37,27 @@ public final class PostgreDDLGenerator implements DialectDDLGenerator {
try (Connection connection = dataSource.getConnection()) {
int majorVersion = connection.getMetaData().getDatabaseMajorVersion();
int minorVersion = connection.getMetaData().getDatabaseMinorVersion();
- Map<String, Object> context = loadGenerateContext(tableName, schemaName, connection, majorVersion, minorVersion);
- String tableSql = generateCreateTableSql(context, majorVersion, minorVersion);
- String indexSql = new PostgresIndexLoader(connection, majorVersion, minorVersion).loadIndexSql(context);
+ Map<String, Object> materials = loadMaterials(tableName, schemaName, connection, majorVersion, minorVersion);
+ String tableSql = generateCreateTableSql(materials, majorVersion, minorVersion);
+ String indexSql = generateCreateIndexSql(materials, majorVersion, minorVersion, connection);
return tableSql + System.lineSeparator() + indexSql;
}
}
- private Map<String, Object> loadGenerateContext(final String tableName, final String schemaName, final Connection connection, final int majorVersion, final int minorVersion) {
+ private Map<String, Object> loadMaterials(final String tableName, final String schemaName, final Connection connection, final int majorVersion, final int minorVersion) {
Map<String, Object> result = new PostgresTablePropertiesLoader(connection, tableName, schemaName, majorVersion, minorVersion).loadTableProperties();
- new PostgresColumnPropertiesLoader(connection, majorVersion, minorVersion).loadColumnProperties(result);
- new PostgresConstraintsLoader(connection, majorVersion, minorVersion).loadConstraints(result);
+ new PostgresColumnPropertiesAppender(connection, majorVersion, minorVersion).append(result);
+ new PostgresConstraintsPropertiesAppender(connection, majorVersion, minorVersion).append(result);
+ formatColumnList(result);
return result;
}
- private String generateCreateTableSql(final Map<String, Object> context, final int majorVersion, final int minorVersion) {
- formatColumnList(context);
- return FreemarkerManager.getSqlByPgVersion(context, "table/%s/create.ftl", majorVersion, minorVersion).trim();
+ private String generateCreateTableSql(final Map<String, Object> materials, final int majorVersion, final int minorVersion) {
+ return FreemarkerManager.getSqlByPgVersion(materials, "table/%s/create.ftl", majorVersion, minorVersion).trim();
+ }
+
+ private String generateCreateIndexSql(final Map<String, Object> materials, final int majorVersion, final int minorVersion, final Connection connection) {
+ return new PostgresIndexSqlGenerator(connection, majorVersion, minorVersion).generate(materials);
}
@SuppressWarnings("unchecked")
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesLoader.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesAppender.java
similarity index 97%
rename from shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesLoader.java
rename to shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesAppender.java
index 5aeb1969387..fdc70c51e5b 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesLoader.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresColumnPropertiesAppender.java
@@ -33,9 +33,9 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
- * Postgres column properties loader.
+ * Postgres column properties appender.
*/
-public final class PostgresColumnPropertiesLoader extends PostgresAbstractLoader {
+public final class PostgresColumnPropertiesAppender extends AbstractPostgresDDLAdapter {
private static final Pattern LENGTH_PRECISION_PATTERN = Pattern.compile("(\\d+),(\\d+)");
@@ -45,17 +45,17 @@ public final class PostgresColumnPropertiesLoader extends PostgresAbstractLoader
private static final String ATT_OPTION_SPLIT = "=";
- public PostgresColumnPropertiesLoader(final Connection connection, final int majorVersion, final int minorVersion) {
+ public PostgresColumnPropertiesAppender(final Connection connection, final int majorVersion, final int minorVersion) {
super(connection, majorVersion, minorVersion);
}
/**
- * Load column properties.
+ * Append column properties.
*
- * @param context load context
+ * @param context create table sql context
*/
@SneakyThrows
- public void loadColumnProperties(final Map<String, Object> context) {
+ public void append(final Map<String, Object> context) {
Collection<Map<String, Object>> typeAndInheritedColumns = getTypeAndInheritedColumns(context);
Collection<Map<String, Object>> allColumns = executeByTemplate(context, "columns/%s/properties.ftl");
for (Map<String, Object> each : allColumns) {
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsLoader.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsPropertiesAppender.java
similarity index 96%
rename from shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsLoader.java
rename to shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsPropertiesAppender.java
index b65db0922c8..db73ced2664 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsLoader.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresConstraintsPropertiesAppender.java
@@ -29,22 +29,22 @@ import java.util.Set;
import java.util.stream.Collectors;
/**
- * Postgres constraints loader.
+ * Postgres constraints properties appender.
*/
-public final class PostgresConstraintsLoader extends PostgresAbstractLoader {
+public final class PostgresConstraintsPropertiesAppender extends AbstractPostgresDDLAdapter {
private static final Integer PG_CONSTRAINTS_INCLUDE_VERSION = 11;
- public PostgresConstraintsLoader(final Connection connection, final int majorVersion, final int minorVersion) {
+ public PostgresConstraintsPropertiesAppender(final Connection connection, final int majorVersion, final int minorVersion) {
super(connection, majorVersion, minorVersion);
}
/**
- * Load constraints.
+ * Append constraints properties.
*
- * @param context load context
+ * @param context create table sql context
*/
- public void loadConstraints(final Map<String, Object> context) {
+ public void append(final Map<String, Object> context) {
loadPrimaryOrUniqueConstraint(context, "primary_key", "p");
loadPrimaryOrUniqueConstraint(context, "unique_constraint", "u");
context.put("foreign_key", fetchForeignKeys(context));
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexLoader.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexSqlGenerator.java
similarity index 95%
rename from shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexLoader.java
rename to shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexSqlGenerator.java
index 3a9260b0c85..fc93b72e41e 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexLoader.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresIndexSqlGenerator.java
@@ -29,24 +29,24 @@ import java.util.LinkedList;
import java.util.Map;
/**
- * Postgres index loader.
+ * Postgres index sql generator.
*/
-public final class PostgresIndexLoader extends PostgresAbstractLoader {
+public final class PostgresIndexSqlGenerator extends AbstractPostgresDDLAdapter {
private static final Integer PG_INDEX_INCLUDE_VERSION = 11;
- public PostgresIndexLoader(final Connection connection, final int majorVersion, final int minorVersion) {
+ public PostgresIndexSqlGenerator(final Connection connection, final int majorVersion, final int minorVersion) {
super(connection, majorVersion, minorVersion);
}
/**
- * Load index sql.
+ * Generate create index sql.
*
* @param context context
* @return index sql
*/
@SneakyThrows
- public String loadIndexSql(final Map<String, Object> context) {
+ public String generate(final Map<String, Object> context) {
StringBuilder result = new StringBuilder();
Collection<Map<String, Object>> indexNodes = getIndexNodes(context);
for (Map<String, Object> each : indexNodes) {
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresTablePropertiesLoader.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresTablePropertiesLoader.java
index 698445ec6e3..a491b8ecb27 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresTablePropertiesLoader.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgresTablePropertiesLoader.java
@@ -29,7 +29,7 @@ import java.util.Map;
/**
* Postgres table properties loader.
*/
-public final class PostgresTablePropertiesLoader extends PostgresAbstractLoader {
+public final class PostgresTablePropertiesLoader extends AbstractPostgresDDLAdapter {
private final String tableName;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
index eb144fc1561..0f434922570 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
@@ -499,9 +499,9 @@ public enum SQLVisitorRule {
DROP_OUTLINE("DropOutline", SQLStatementType.DDL),
ALTER_OUTLINE("AlterOutline", SQLStatementType.DDL),
-
+
ALTER_ANALYTIC_VIEW("AlterAnalyticView", SQLStatementType.DDL),
-
+
DROP_EDITION("DropEdition", SQLStatementType.DDL);
private final String name;