Skip to content

Commit 5dcd1a9

Browse files
authored
Add PostgreSQLConstraintsPropertiesAppenderTest (#37451)
* Add PostgreSQLConstraintsPropertiesAppenderTest * Add PostgreSQLConstraintsPropertiesAppenderTest * Add PostgreSQLConstraintsPropertiesAppenderTest * Add PostgreSQLConstraintsPropertiesAppenderTest
1 parent 305d288 commit 5dcd1a9

2 files changed

Lines changed: 343 additions & 70 deletions

File tree

kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/constraints/PostgreSQLConstraintsPropertiesAppender.java

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import java.sql.Connection;
2323
import java.util.Collection;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.HashSet;
26-
import java.util.LinkedHashMap;
2727
import java.util.LinkedList;
2828
import java.util.Map;
2929
import java.util.Optional;
@@ -46,7 +46,7 @@ public PostgreSQLConstraintsPropertiesAppender(final Connection connection, fina
4646
/**
4747
* Append constraints properties.
4848
*
49-
* @param context create table sql context
49+
* @param context create table SQL context
5050
*/
5151
public void append(final Map<String, Object> context) {
5252
loadPrimaryOrUniqueConstraint(context, "primary_key", "p");
@@ -56,54 +56,34 @@ public void append(final Map<String, Object> context) {
5656
context.put("exclude_constraint", getExclusionConstraints(context));
5757
}
5858

59-
private Collection<Map<String, Object>> fetchCheckConstraints(final Map<String, Object> context) {
60-
Collection<Map<String, Object>> result = new LinkedList<>();
61-
for (Map<String, Object> each : getCheckConstraints((Long) context.get("tid"))) {
62-
if (!isPartitionAndConstraintInherited(each, context)) {
63-
result.add(each);
64-
}
65-
}
66-
return result;
67-
}
68-
69-
private Collection<Map<String, Object>> fetchForeignKeys(final Map<String, Object> context) {
70-
Collection<Map<String, Object>> result = new LinkedList<>();
71-
for (Map<String, Object> each : getForeignKeys((Long) context.get("tid"))) {
72-
if (!isPartitionAndConstraintInherited(each, context)) {
73-
result.add(each);
74-
}
75-
}
76-
return result;
77-
}
78-
7959
private void loadPrimaryOrUniqueConstraint(final Map<String, Object> context, final String name, final String type) {
8060
Collection<Map<String, Object>> constraintsProps = fetchConstraintsProperties(context, type);
8161
fetchConstraintsColumns(constraintsProps);
8262
context.put(name, constraintsProps.stream().filter(each -> !isPartitionAndConstraintInherited(each, context)).collect(Collectors.toList()));
8363
}
8464

65+
private Collection<Map<String, Object>> fetchConstraintsProperties(final Map<String, Object> context, final String constraintType) {
66+
Map<String, Object> params = new HashMap<>(4, 1F);
67+
params.put("did", context.get("did"));
68+
params.put("tid", context.get("tid"));
69+
params.put("cid", context.get("cid"));
70+
params.put("constraint_type", constraintType);
71+
return templateExecutor.executeByTemplate(params, "component/index_constraint/%s/properties.ftl");
72+
}
73+
8574
private void fetchConstraintsColumns(final Collection<Map<String, Object>> constraintsProps) {
8675
for (Map<String, Object> each : constraintsProps) {
87-
Collection<Map<String, Object>> columns = new LinkedList<>();
88-
for (Map<String, Object> col : fetchConstraintsCols(each)) {
89-
Map<String, Object> column = new HashMap<>();
90-
column.put("column", stripQuote((String) col.get("column")));
91-
columns.add(column);
92-
}
93-
each.put("columns", columns);
76+
each.put("columns",
77+
fetchConstraintsCols(each).stream().<Map<String, Object>>map(col -> Collections.singletonMap("column", stripQuote((String) col.get("column")))).collect(Collectors.toList()));
9478
appendConstraintsInclude(each);
9579
}
9680
}
9781

9882
private void appendConstraintsInclude(final Map<String, Object> constraintsProp) {
99-
Map<String, Object> params = new LinkedHashMap<>();
100-
params.put("cid", constraintsProp.get("oid"));
101-
Collection<Object> includes = new LinkedList<>();
102-
if (templateExecutor.getMajorVersion() >= PG_CONSTRAINTS_INCLUDE_VERSION) {
103-
for (Map<String, Object> each : templateExecutor.executeByTemplate(params, "component/index_constraint/%s/get_constraint_include.ftl")) {
104-
includes.add(each.get("colname"));
105-
}
106-
}
83+
Collection<Object> includes = templateExecutor.getMajorVersion() >= PG_CONSTRAINTS_INCLUDE_VERSION
84+
? templateExecutor.executeByTemplate(Collections.singletonMap("cid", constraintsProp.get("oid")),
85+
"component/index_constraint/%s/get_constraint_include.ftl").stream().map(each -> each.get("colname")).collect(Collectors.toList())
86+
: Collections.emptyList();
10787
constraintsProp.put("include", includes);
10888
}
10989

@@ -119,23 +99,22 @@ private String stripQuote(final String column) {
11999
}
120100

121101
private Collection<Map<String, Object>> fetchConstraintsCols(final Map<String, Object> constraintColProps) {
122-
Map<String, Object> params = new HashMap<>();
102+
Map<String, Object> params = new HashMap<>(2, 1F);
123103
params.put("cid", constraintColProps.get("oid"));
124104
params.put("colcnt", constraintColProps.get("col_count"));
125105
return templateExecutor.executeByTemplate(params, "component/index_constraint/%s/get_costraint_cols.ftl");
126106
}
127107

128-
private Collection<Map<String, Object>> fetchConstraintsProperties(final Map<String, Object> context, final String constraintType) {
129-
Map<String, Object> params = new HashMap<>();
130-
params.put("did", context.get("did"));
131-
params.put("tid", context.get("tid"));
132-
params.put("cid", context.get("cid"));
133-
params.put("constraint_type", constraintType);
134-
return templateExecutor.executeByTemplate(params, "component/index_constraint/%s/properties.ftl");
108+
private Collection<Map<String, Object>> fetchForeignKeys(final Map<String, Object> context) {
109+
return getForeignKeys((Long) context.get("tid")).stream().filter(each -> !isPartitionAndConstraintInherited(each, context)).collect(Collectors.toList());
110+
}
111+
112+
private Collection<Map<String, Object>> fetchCheckConstraints(final Map<String, Object> context) {
113+
return getCheckConstraints((Long) context.get("tid")).stream().filter(each -> !isPartitionAndConstraintInherited(each, context)).collect(Collectors.toList());
135114
}
136115

137116
private Collection<Map<String, Object>> getExclusionConstraints(final Map<String, Object> context) {
138-
Map<String, Object> params = new HashMap<>();
117+
Map<String, Object> params = new HashMap<>(2, 1F);
139118
params.put("tid", context.get("tid"));
140119
params.put("did", context.get("did"));
141120
Collection<Map<String, Object>> result = templateExecutor.executeByTemplate(params, "component/exclusion_constraint/%s/properties.ftl");
@@ -146,14 +125,14 @@ private Collection<Map<String, Object>> getExclusionConstraints(final Map<String
146125
}
147126

148127
private void getExclusionConstraintsColumns(final Map<String, Object> exclusionConstraintsProps) {
149-
Map<String, Object> params = new HashMap<>();
128+
Map<String, Object> params = new HashMap<>(2, 1F);
150129
params.put("cid", exclusionConstraintsProps.get("oid"));
151130
params.put("col_count", exclusionConstraintsProps.get("col_count"));
152131
Collection<Map<String, Object>> columns = new LinkedList<>();
153132
for (Map<String, Object> each : templateExecutor.executeByTemplate(params, "component/exclusion_constraint/%s/get_constraint_cols.ftl")) {
154133
boolean order = 0 == (((int) each.get("options")) & 1);
155134
boolean nullsOrder = 0 != (((int) each.get("options")) & 2);
156-
Map<String, Object> col = new HashMap<>();
135+
Map<String, Object> col = new HashMap<>(7, 1F);
157136
col.put("column", strip((String) each.get("coldef")));
158137
col.put("oper_class", each.get("opcname"));
159138
col.put("order", order);
@@ -164,10 +143,9 @@ private void getExclusionConstraintsColumns(final Map<String, Object> exclusionC
164143
columns.add(col);
165144
}
166145
exclusionConstraintsProps.put("columns", columns);
167-
Map<String, Object> map = new HashMap<>();
168-
map.put("cid", exclusionConstraintsProps.get("oid"));
169146
Collection<String> include = new LinkedList<>();
170147
if (templateExecutor.getMajorVersion() >= PG_CONSTRAINTS_INCLUDE_VERSION) {
148+
Map<String, Object> map = Collections.singletonMap("cid", exclusionConstraintsProps.get("oid"));
171149
for (Map<String, Object> each : templateExecutor.executeByTemplate(map, "exclusion_constraint/%s/get_constraint_include.ftl")) {
172150
include.add(each.get("colname").toString());
173151
}
@@ -176,14 +154,12 @@ private void getExclusionConstraintsColumns(final Map<String, Object> exclusionC
176154
}
177155

178156
private Collection<Map<String, Object>> getForeignKeys(final Long tid) {
179-
Map<String, Object> params = new HashMap<>();
180-
params.put("tid", tid);
181-
Collection<Map<String, Object>> result = templateExecutor.executeByTemplate(params, "component/foreign_key/%s/properties.ftl");
157+
Collection<Map<String, Object>> result = templateExecutor.executeByTemplate(Collections.singletonMap("tid", tid), "component/foreign_key/%s/properties.ftl");
182158
for (Map<String, Object> each : result) {
183159
Collection<Map<String, Object>> columns = new LinkedList<>();
184160
Set<String> cols = new HashSet<>();
185161
for (Map<String, Object> col : getForeignKeysCols(tid, each)) {
186-
Map<String, Object> foreignKeysRef = new HashMap<>();
162+
Map<String, Object> foreignKeysRef = new HashMap<>(4, 1F);
187163
foreignKeysRef.put("local_column", col.get("conattname"));
188164
foreignKeysRef.put("references", each.get("confrelid"));
189165
foreignKeysRef.put("referenced", col.get("confattname"));
@@ -202,22 +178,19 @@ private Collection<Map<String, Object>> getForeignKeys(final Long tid) {
202178
}
203179

204180
private void setRemoteName(final Map<String, Object> foreignKey, final Collection<Map<String, Object>> columns) {
205-
Map<String, Object> params = new HashMap<>();
206-
params.put("tid", columns.iterator().next().get("references"));
207-
Map<String, Object> parents = templateExecutor.executeByTemplateForSingleRow(params, "component/foreign_key/%s/get_parent.ftl");
181+
Map<String, Object> parents = templateExecutor.executeByTemplateForSingleRow(
182+
Collections.singletonMap("tid", columns.iterator().next().get("references")), "component/foreign_key/%s/get_parent.ftl");
208183
foreignKey.put("remote_schema", parents.get("schema"));
209184
foreignKey.put("remote_table", parents.get("table"));
210185
}
211186

212187
private Collection<Map<String, Object>> getForeignKeysCols(final Long tid, final Map<String, Object> foreignKeyProps) {
213-
Map<String, Object> params = new HashMap<>();
188+
Map<String, Object> params = new HashMap<>(2, 1F);
214189
params.put("tid", tid);
215-
Collection<Map<String, Object>> keys = new LinkedList<>();
216-
Map<String, Object> key = new HashMap<>();
190+
Map<String, Object> key = new HashMap<>(2, 1F);
217191
key.put("confkey", foreignKeyProps.get("confkey"));
218192
key.put("conkey", foreignKeyProps.get("conkey"));
219-
keys.add(key);
220-
params.put("keys", keys);
193+
params.put("keys", Collections.singleton(key));
221194
return templateExecutor.executeByTemplate(params, "component/foreign_key/%s/get_constraint_cols.ftl");
222195
}
223196

@@ -226,14 +199,12 @@ private boolean isPartitionAndConstraintInherited(final Map<String, Object> cons
226199
}
227200

228201
private Optional<String> searchCoveringIndex(final Long tid, final Set<String> cols) {
229-
Map<String, Object> params = new HashMap<>();
230-
params.put("tid", tid);
231-
for (Map<String, Object> each : templateExecutor.executeByTemplate(params, "component/foreign_key/%s/get_constraints.ftl")) {
232-
Map<String, Object> map = new HashMap<>();
202+
for (Map<String, Object> each : templateExecutor.executeByTemplate(Collections.singletonMap("tid", tid), "component/foreign_key/%s/get_constraints.ftl")) {
203+
Map<String, Object> map = new HashMap<>(2, 1F);
233204
map.put("cid", each.get("oid"));
234205
map.put("colcnt", each.get("col_count"));
235206
Collection<Map<String, Object>> rows = templateExecutor.executeByTemplate(map, "component/foreign_key/%s/get_cols.ftl");
236-
Set<String> indexCols = new HashSet<>();
207+
Set<String> indexCols = new HashSet<>(rows.size(), 1F);
237208
for (Map<String, Object> row : rows) {
238209
indexCols.add(strip(row.get("column").toString()));
239210
}
@@ -266,8 +237,6 @@ private String strip(final String column) {
266237
}
267238

268239
private Collection<Map<String, Object>> getCheckConstraints(final Long tid) {
269-
Map<String, Object> params = new HashMap<>();
270-
params.put("tid", tid);
271-
return templateExecutor.executeByTemplate(params, "component/check_constraint/%s/properties.ftl");
240+
return templateExecutor.executeByTemplate(Collections.singletonMap("tid", tid), "component/check_constraint/%s/properties.ftl");
272241
}
273242
}

0 commit comments

Comments
 (0)