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

Fix insert ValuesClause clone() issue #6333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -93,7 +93,12 @@ public ValuesClause() {
public ValuesClause clone() {
ValuesClause x = new ValuesClause(new ArrayList<SQLExpr>(this.values.size()));
for (Object v : values) {
x.addValue(v);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句导致value的parent变成了clone后的的ValuesClause

if (v instanceof SQLObject) {
SQLObject clone = ((SQLExpr) v).clone();
x.addValue(clone);
} else {
x.addValue(v);
}
}
return x;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.alibaba.druid.bvt.sql.ast;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;

import static org.junit.Assert.assertEquals;

public class SQLInsertStatementTest {
@Test
public void test_0() throws Exception {
SQLInsertStatement stmt = new SQLInsertStatement();
stmt.setTableSource(new SQLExprTableSource("my_table"));

// Add columns
stmt.addColumn(new SQLIdentifierExpr("id"));
stmt.addColumn(new SQLIdentifierExpr("name"));

// Add values
List<SQLExpr> values = new ArrayList<>();
SQLExpr value1 = new SQLIdentifierExpr("1");
SQLExpr value2 = new SQLIdentifierExpr("'abc'");
values.add(value1);
values.add(value2);
SQLInsertStatement.ValuesClause valuesClause = new SQLInsertStatement.ValuesClause(values);
stmt.addValueCause(valuesClause);
assertEquals(value1.getParent(), valuesClause);
assertEquals(value2.getParent(), valuesClause);

// clone
SQLInsertStatement clone = stmt.clone();
assertEquals(value1.getParent(), valuesClause);
assertEquals(value2.getParent(), valuesClause);

assertEquals(stmt.toString(), clone.toString());
}
}
Loading