Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support returning for some firebird requests #34047

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ grammar DMLStatement;
import BaseRule;

insert
: INSERT INTO? tableName (insertValuesClause | insertSelectClause)
: INSERT INTO? tableName (insertValuesClause | insertSelectClause) returningClause?
;

insertValuesClause
Expand All @@ -31,8 +31,12 @@ insertSelectClause
: columnNames? select
;

returningClause
: RETURNING projections
;

update
: UPDATE tableReferences setAssignmentsClause whereClause?
: UPDATE tableReferences setAssignmentsClause whereClause? returningClause?
;

assignment
Expand All @@ -57,7 +61,7 @@ blobValue
;

delete
: DELETE singleTableClause whereClause?
: DELETE singleTableClause whereClause? returningClause?
;

singleTableClause
Expand Down Expand Up @@ -199,4 +203,4 @@ variableList

variableListClause
: variableList (COMMA_ variableList)*
;
;
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.ProjectionContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.ProjectionsContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.QualifiedShorthandContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.ReturningClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.SelectClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.SelectContext;
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.SelectSpecificationContext;
Expand All @@ -55,6 +56,7 @@
import org.apache.shardingsphere.sql.parser.autogen.FirebirdStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.firebird.visitor.statement.FirebirdStatementVisitor;
import org.apache.shardingsphere.sql.parser.statement.core.enums.JoinType;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
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.assignment.SetAssignmentSegment;
Expand Down Expand Up @@ -110,6 +112,9 @@ public ASTNode visitInsert(final InsertContext ctx) {
FirebirdInsertStatement result = (FirebirdInsertStatement) visit(ctx.insertValuesClause());
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
result.addParameterMarkerSegments(getParameterMarkerSegments());
if (null != ctx.returningClause()) {
result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
}
return result;
}

Expand All @@ -136,6 +141,11 @@ private Collection<InsertValuesSegment> createInsertValuesSegments(final Collect
return result;
}

@Override
public ASTNode visitReturningClause(final ReturningClauseContext ctx) {
return new ReturningSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ProjectionsSegment) visit(ctx.projections()));
}

@Override
public ASTNode visitUpdate(final UpdateContext ctx) {
FirebirdUpdateStatement result = new FirebirdUpdateStatement();
Expand All @@ -145,6 +155,9 @@ public ASTNode visitUpdate(final UpdateContext ctx) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
}
result.addParameterMarkerSegments(getParameterMarkerSegments());
if (null != ctx.returningClause()) {
result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
}
return result;
}

Expand Down Expand Up @@ -194,6 +207,9 @@ public ASTNode visitDelete(final DeleteContext ctx) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
}
result.addParameterMarkerSegments(getParameterMarkerSegments());
if (null != ctx.returningClause()) {
result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.hint.OptionHintSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
Expand Down Expand Up @@ -172,4 +173,22 @@ public Optional<TableSegment> getFrom() {
*/
public void setFrom(final TableSegment from) {
}

/**
* Get returning segment of update statement.
*
* @return returning segment
*/
public Optional<ReturningSegment> getReturningSegment() {
return Optional.empty();
}

/**
* Set returning segment of update statement.
*
* @param returningSegment returning segment
*/
public void setReturningSegment(final ReturningSegment returningSegment) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@

package org.apache.shardingsphere.sql.parser.statement.firebird.dml;

import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.firebird.FirebirdStatement;

import java.util.Optional;

/**
* Firebird delete statement.
*/
@Setter
public final class FirebirdDeleteStatement extends DeleteStatement implements FirebirdStatement {

private ReturningSegment returningSegment;

@Override
public Optional<ReturningSegment> getReturningSegment() {
return Optional.ofNullable(returningSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@

package org.apache.shardingsphere.sql.parser.statement.firebird.dml;

import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.statement.firebird.FirebirdStatement;

import java.util.Optional;

/**
* Firebird insert statement.
*/
@Setter
public final class FirebirdInsertStatement extends InsertStatement implements FirebirdStatement {

private ReturningSegment returningSegment;

@Override
public Optional<ReturningSegment> getReturningSegment() {
return Optional.ofNullable(returningSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@

package org.apache.shardingsphere.sql.parser.statement.firebird.dml;

import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.statement.firebird.FirebirdStatement;

import java.util.Optional;

/**
* Firebird update statement.
*/
@Setter
public final class FirebirdUpdateStatement extends UpdateStatement implements FirebirdStatement {

private ReturningSegment returningSegment;

@Override
public Optional<ReturningSegment> getReturningSegment() {
return Optional.ofNullable(returningSegment);
}
}
59 changes: 59 additions & 0 deletions test/it/parser/src/main/resources/case/dml/update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2337,4 +2337,63 @@
</expr>
</where>
</update>

<update sql-case-id="update_returning_expressions" parameters="1, 2, 3">
<table start-index="7" stop-index="13">
<simple-table name="t_order" start-index="7" stop-index="13" />
</table>
<set start-index="15" stop-index="37" literal-stop-index="37">
<assignment start-index="19" stop-index="37" literal-stop-index="37">
<column name="status" start-index="19" stop-index="24" />
<assignment-value>
<binary-operation-expression start-index="28" stop-index="37" literal-start-index="28" literal-stop-index="37">
<left>
<column name="status" start-index="28" stop-index="33" literal-start-index="28" literal-stop-index="33" />
</left>
<operator>-</operator>
<right>
<literal-expression value="1" start-index="37" stop-index="37" />
<parameter-marker-expression parameter-index="0" start-index="37" stop-index="37" literal-start-index="37" literal-stop-index="37" />
</right>
</binary-operation-expression>
</assignment-value>
</assignment>
</set>
<where start-index="39" stop-index="72">
<expr>
<binary-operation-expression start-index="45" stop-index="72">
<left>
<binary-operation-expression start-index="45" stop-index="56">
<left>
<column name="order_id" start-index="45" stop-index="52" />
</left>
<operator>=</operator>
<right>
<literal-expression value="2" start-index="56" stop-index="56" />
<parameter-marker-expression parameter-index="1" start-index="56" stop-index="56" />
</right>
</binary-operation-expression>
</left>
<operator>AND</operator>
<right>
<binary-operation-expression start-index="62" stop-index="72">
<left>
<column name="user_id" start-index="62" stop-index="68" />
</left>
<operator>=</operator>
<right>
<literal-expression value="3" start-index="72" stop-index="72" />
<parameter-marker-expression parameter-index="2" start-index="72" stop-index="72" />
</right>
</binary-operation-expression>
</right>
</binary-operation-expression>
</expr>
</where>
<returning start-index="74" stop-index="89">
<projections start-index="84" stop-index="89">
<column-projection name="status" start-index="84" stop-index="89" />
</projections>
</returning>
</update>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
<sql-case id="delete_with_schema" value="DELETE FROM db1.t_order" />
<sql-case id="delete_with_simple_condition" value="DELETE FROM Q1_2000_sales WHERE amount_sold &lt; 0" db-types="Oracle" />
<sql-case id="delete_with_output_clause_with_compress_function" value="DELETE FROM player OUTPUT deleted.id,deleted.name, deleted.surname,deleted.datemodifier,COMPRESS(deleted.info) INTO dbo.inactivePlayers WHERE datemodified &lt; @startOfYear" db-types="SQLServer" />
<sql-case id="delete_returning_expressions" value="DELETE FROM t2 WHERE id = 2 RETURNING id,t&amp;t" db-types="MySQL" />
<sql-case id="delete_returning_expressions" value="DELETE FROM t2 WHERE id = 2 RETURNING id,t&amp;t" db-types="MySQL,Firebird" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@
<sql-case id="insert_on_duplicate_key_with_column_aliases" value="INSERT INTO t SET a=9,b=5 AS new(m,n) ON DUPLICATE KEY UPDATE a=m+n" db-types="MySQL"/>
<sql-case id="insert_with_aes_encrypt" value="INSERT INTO t_order_item(item_id,encrypt) VALUES (1,AES_ENCRYPT('text','key_str'))" db-types="MySQL" />
<sql-case id="insert_on_duplicate_key_update_with_values" value="INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);" db-types="MySQL" />
<sql-case id="insert_returning_expressions" value="INSERT INTO t2 (id) VALUES (2),(3) RETURNING id,t&amp;t" db-types="MySQL" />
<sql-case id="insert_returning_expressions" value="INSERT INTO t2 (id) VALUES (2),(3) RETURNING id,t&amp;t" db-types="MySQL,Firebird" />

</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@
<sql-case id="update_with_inner_join" value="UPDATE dbo.Table2 SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB FROM dbo.Table2 INNER JOIN dbo.Table1 ON (dbo.Table2.ColA = dbo.Table1.ColA)" db-types="SQLServer"/>
<sql-case id="update_with_current_of_abc" value="UPDATE dbo.Table1 SET c2 = c2 + d2 FROM dbo.Table2 WHERE CURRENT OF abc" db-types="SQLServer"/>
<sql-case id="update_with_location_setXY" value="UPDATE Cities SET Location.SetXY(23.5, 23.5) WHERE Name = 'Anchorage'" db-types="SQLServer"/>
<sql-case id="update_returning_expressions" value="UPDATE t_order SET status = status - ? WHERE order_id = ? AND user_id = ? RETURNING status" db-types="Firebird" />
</sql-cases>
Loading