Skip to content

Commit

Permalink
Refactor ShardingSphereTable.columnNames type as ShardingSphereIdenti…
Browse files Browse the repository at this point in the history
…fier (apache#33971)
  • Loading branch information
terrymanu authored Dec 8, 2024
1 parent 934ff92 commit 7511c72
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class ShardingSphereTable {
@Getter(AccessLevel.NONE)
private final Map<ShardingSphereIdentifier, ShardingSphereColumn> columns;

private final List<String> columnNames = new ArrayList<>();
private final List<ShardingSphereIdentifier> columnNames = new ArrayList<>();

private final List<String> primaryKeyColumns = new ArrayList<>();

Expand Down Expand Up @@ -81,8 +81,9 @@ private Map<ShardingSphereIdentifier, ShardingSphereColumn> createColumns(final
Map<ShardingSphereIdentifier, ShardingSphereColumn> result = new LinkedHashMap<>(columns.size(), 1F);
int index = 0;
for (ShardingSphereColumn each : columns) {
result.put(new ShardingSphereIdentifier(each.getName()), each);
columnNames.add(each.getName());
ShardingSphereIdentifier columnName = new ShardingSphereIdentifier(each.getName());
result.put(columnName, each);
columnNames.add(columnName);
if (each.isPrimaryKey()) {
primaryKeyColumns.add(each.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.InsertValuesSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
Expand Down Expand Up @@ -67,7 +68,9 @@ private static List<ShardingSphereColumn> findColumnsOfParameterMarkersForInsert
}

private static List<String> getColumnNamesOfInsertStatement(final InsertStatement insertStatement, final ShardingSphereTable table) {
return insertStatement.getColumns().isEmpty() ? table.getColumnNames() : insertStatement.getColumns().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList());
return insertStatement.getColumns().isEmpty()
? table.getColumnNames().stream().map(ShardingSphereIdentifier::getValue).collect(Collectors.toList())
: insertStatement.getColumns().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList());
}

private static List<ShardingSphereColumn> getParameterMarkerColumns(final InsertStatement insertStatement, final ShardingSphereTable table, final List<String> columnNamesOfInsert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
Expand Down Expand Up @@ -131,7 +132,7 @@ private void describeInsertStatementByDatabaseMetaData(final PostgreSQLServerPre
}
String logicTableName = insertStatement.getTable().map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("");
ShardingSphereTable table = getTableFromMetaData(connectionSession.getUsedDatabaseName(), insertStatement, logicTableName);
List<String> columnNamesOfInsert = getColumnNamesOfInsertStatement(insertStatement, table);
List<ShardingSphereIdentifier> columnNamesOfInsert = getColumnNamesOfInsertStatement(insertStatement, table);
preparedStatement.setRowDescription(returningSegment.<PostgreSQLPacket>map(returning -> describeReturning(returning, table)).orElseGet(PostgreSQLNoDataPacket::getInstance));
int parameterMarkerIndex = 0;
for (InsertValuesSegment each : insertStatement.getValues()) {
Expand All @@ -144,7 +145,7 @@ private void describeInsertStatementByDatabaseMetaData(final PostgreSQLServerPre
parameterMarkerIndex++;
continue;
}
String columnName = columnNamesOfInsert.get(i);
String columnName = columnNamesOfInsert.get(i).toString();
ShardingSpherePreconditions.checkState(table.containsColumn(columnName), () -> new ColumnNotFoundException(logicTableName, columnName));
preparedStatement.getParameterTypes().set(parameterMarkerIndex++, PostgreSQLColumnType.valueOfJDBCType(table.getColumn(columnName).getDataType()));
}
Expand All @@ -169,8 +170,10 @@ private ShardingSphereTable getTableFromMetaData(final String databaseName, fina
return database.getSchema(schemaName).getTable(logicTableName);
}

private List<String> getColumnNamesOfInsertStatement(final InsertStatement insertStatement, final ShardingSphereTable table) {
return insertStatement.getColumns().isEmpty() ? table.getColumnNames() : insertStatement.getColumns().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList());
private List<ShardingSphereIdentifier> getColumnNamesOfInsertStatement(final InsertStatement insertStatement, final ShardingSphereTable table) {
return insertStatement.getColumns().isEmpty()
? table.getColumnNames()
: insertStatement.getColumns().stream().map(each -> new ShardingSphereIdentifier(each.getIdentifier().getValue())).collect(Collectors.toList());
}

private PostgreSQLRowDescriptionPacket describeReturning(final ReturningSegment returningSegment, final ShardingSphereTable table) {
Expand Down

0 comments on commit 7511c72

Please sign in to comment.