Skip to content

Commit

Permalink
Increase Support for dmlTableClause for Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
kanha-gupta committed Nov 5, 2023
1 parent ef2647e commit af085b9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static Optional<SqlNode> convert(final SimpleTableSegment segment) {
addOwnerNames(names, segment.getOwner().get());
}
names.add(tableName.getIdentifier().getValue());
if (segment.getDbLink().isPresent()) {
names.add(segment.getAt().get().getValue());
names.add(segment.getDbLink().get().getValue());
}
SqlNode tableNameSQLNode = new SqlIdentifier(names, SqlParserPos.ZERO);
if (segment.getAliasName().isPresent()) {
SqlNode aliasSQLNode = new SqlIdentifier(segment.getAliasName().get(), SqlParserPos.ZERO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.CaseWhenExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.CollateExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.segment.oracle.datetime.DatetimeExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionWithParamsSegment;
Expand Down Expand Up @@ -458,6 +459,16 @@ public ASTNode visitUnpivotClause(final UnpivotClauseContext ctx) {

@Override
public ASTNode visitDmlTableClause(final DmlTableClauseContext ctx) {
if (null != ctx.AT_() && null != ctx.dbLink()) {
SimpleTableSegment result = new SimpleTableSegment(new TableNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(ctx.tableName().name().getText())));
if (null != ctx.tableName().owner()) {
result.setOwner(
new OwnerSegment(ctx.tableName().owner().start.getStartIndex(), ctx.tableName().owner().stop.getStopIndex(), (IdentifierValue) visit(ctx.tableName().owner().identifier())));
}
result.setAt(new IdentifierValue(ctx.AT_().getText()));
result.setDbLink(new IdentifierValue(ctx.dbLink().identifier(0).getText()));
return result;
}
return visit(ctx.tableName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public final class SimpleTableSegment implements TableSegment, OwnerAvailable {
@Setter
private PivotSegment pivot;

@Setter
private IdentifierValue dbLink;

@Setter
private IdentifierValue at;

@Override
public int getStartIndex() {
if (null == owner) {
Expand All @@ -64,6 +70,14 @@ public Optional<OwnerSegment> getOwner() {
return Optional.ofNullable(owner);
}

public Optional<IdentifierValue> getDbLink() {
return Optional.ofNullable(dbLink);
}

public Optional<IdentifierValue> getAt() {
return Optional.ofNullable(at);
}

@Override
public Optional<String> getAliasName() {
return null == alias ? Optional.empty() : Optional.ofNullable(alias.getIdentifier().getValue());
Expand Down
2 changes: 2 additions & 0 deletions test/it/optimizer/src/test/resources/converter/delete.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@
<test-cases sql-case-id="delete_with_alias" expected-sql="DELETE FROM [t_order] AS [o] AS [o] WHERE [status] = ?" db-types="SQLServer" sql-case-types="PLACEHOLDER" />
<test-cases sql-case-id="delete_with_with_clause" expected-sql="WITH [cte] ([order_id], [user_id]) AS (SELECT [order_id], [user_id] FROM [t_order]) DELETE FROM ([cte], [t_order]) WHERE [t_order].[order_id] = [cte].[order_id]" db-types="SQLServer" />
<test-cases sql-case-id="delete_without_columns_with_with_clause" expected-sql="WITH [cte] AS (SELECT [order_id], [user_id] FROM [t_order]) DELETE FROM ([cte], [t_order]) WHERE [t_order].[order_id] = [cte].[order_id]" db-types="SQLServer" />
<test-cases sql-case-id="delete_with_simple_table" expected-sql="DELETE FROM &quot;product_descriptions&quot; WHERE &quot;language_id&quot; = 'AR'" db-types="Oracle" />
<test-cases sql-case-id="delete_with_remote_database_table" expected-sql="DELETE FROM &quot;hr&quot;.&quot;locations&quot;.&quot;@&quot;.&quot;remote&quot; WHERE &quot;location_id&quot; &gt; 3000" db-types="Oracle" />
</sql-node-converter-test-cases>
2 changes: 1 addition & 1 deletion test/it/parser/src/main/resources/case/dml/delete.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@
</delete>

<delete sql-case-id="delete_with_remote_database_table">
<table name="locations" start-index="12" stop-index="23">
<table name="locations" start-index="12" stop-index="30">
<owner name="hr" start-index="12" stop-index="13" />
</table>
<where start-index="32" stop-index="55">
Expand Down

0 comments on commit af085b9

Please sign in to comment.