Skip to content

Commit

Permalink
Fix some parent-child relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengguanLi committed Nov 22, 2024
1 parent 7752ee5 commit 558ea7d
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public boolean equals(Object obj) {

public SQLListExpr clone() {
SQLListExpr x = new SQLListExpr();
if (isParenthesized()) {
x.setParenthesized(true);
}
for (SQLExpr item : items) {
SQLExpr item2 = item.clone();
item2.setParent(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ public SQLTableSource getUsing() {
}

public void setUsing(SQLTableSource using) {
if (using != null) {
using.setParent(this);
}
this.using = using;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public SQLExpr getOn() {
}

public void setOn(SQLExpr on) {
if (on != null) {
on.setParent(this);
}
this.on = on;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected void parseCreateTableRest(SQLCreateTableStatement createTable) {
for (; ; ) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
lexer.nextToken();
this.exprParser.exprList(list.getItems(), list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ protected void parseOptions(MySqlCreateTableStatement stmt) {

accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprParser.exprList(list.getItems(), list);
stmt.addOption("UNION", list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,9 @@ public SQLExpr primary() {
if (lexer.token() != Token.LPAREN) {
SQLExpr expr = primary();
SQLValuesQuery values = new SQLValuesQuery();
values.addValue(new SQLListExpr(expr));
SQLListExpr sqlListExpr = new SQLListExpr(expr);
sqlListExpr.setParenthesized(true);
values.addValue(sqlListExpr);
return new SQLQueryExpr(new SQLSelect(values));
}
return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
Expand Down Expand Up @@ -2542,6 +2544,7 @@ public boolean parseTableOptions(List<SQLAssignItem> assignItems, SQLDDLStatemen

accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
exprList(list.getItems(), list);
accept(Token.RPAREN);
assignItem = new SQLAssignItem(new SQLIdentifierExpr("UNION"), list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected void parseAssignItemNcToBeExecuted() {
protected boolean parseAssignItemTblProperties(SQLAssignItem item) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprList(list.getItems(), list);
item.setTarget(new SQLIdentifierExpr("tblproperties"));
item.setValue(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public OracleReturningClause getReturning() {
}

public void setReturning(OracleReturningClause returning) {
if (returning != null) {
returning.setParent(this);
}
this.returning = returning;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public OracleReturningClause getReturning() {
}

public void setReturning(OracleReturningClause returning) {
if (returning != null) {
returning.setParent(this);
}
this.returning = returning;
}

Expand All @@ -72,6 +75,9 @@ public SQLErrorLoggingClause getErrorLogging() {
}

public void setErrorLogging(SQLErrorLoggingClause errorLogging) {
if (errorLogging != null) {
errorLogging.setParent(this);
}
this.errorLogging = errorLogging;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public boolean visit(OracleAnalyticWindowing x) {
public boolean visit(OracleDeleteStatement x) {
print0(ucase ? "DELETE " : "delete ");

SQLTableSource tableSource = x.getTableSource();
if (x.getHints().size() > 0) {
printAndAccept(x.getHints(), ", ");
print(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public SQLExpr primary() {
for (; ; ) {
accept(Token.LPAREN);
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ public boolean visit(SQLSetStatement x) {
@Override
public boolean visit(SQLAssignItem x) {
if (!(x.getParent() instanceof SQLSetStatement)) {
return super.visit(x);
x.getTarget().accept(this);
print0(" := ");
x.getValue().accept(this);
return false;
}

x.getTarget().accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public SQLCreateTableStatement parseCreateTable() {
for (; ; ) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
lexer.nextToken();
this.exprParser.exprList(list.getItems(), list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,7 @@ public SQLExpr primaryRest(SQLExpr expr) {
}

SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprList(listExpr.getItems(), listExpr);
item = listExpr;

Expand Down Expand Up @@ -3084,6 +3085,7 @@ public SQLUpdateSetItem parseUpdateSetItem() {
if (lexer.token == (Token.LPAREN)) {
lexer.nextToken();
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprList(list.getItems(), list);
accept(Token.RPAREN);
item.setColumn(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ protected SQLSelectQuery valuesQuery(boolean acceptUnion) {
if (lexer.token == Token.LPAREN) {
lexer.nextToken();
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprParser.exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);
valuesQuery.addValue(listExpr);
Expand Down Expand Up @@ -1042,7 +1043,9 @@ protected SQLExpr parseGroupByItem() {

if (lexer.token == Token.RPAREN) {
lexer.nextToken();
return new SQLListExpr();
SQLListExpr sqlListExpr = new SQLListExpr();
sqlListExpr.setParenthesized(true);
return sqlListExpr;
}

lexer.reset(mark);
Expand All @@ -1062,6 +1065,7 @@ protected SQLExpr parseGroupByItem() {
if (lexer.token == Token.LPAREN) {
accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
if (lexer.token == Token.COMMA) {
lexer.nextToken();
}
Expand Down Expand Up @@ -1250,6 +1254,7 @@ public SQLTableSource parseTableSource(boolean forFrom) {
for (; ; ) {
accept(Token.LPAREN);
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprParser.exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);

Expand Down Expand Up @@ -2258,6 +2263,7 @@ public SQLValuesTableSource parseValues() {
}

SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);

if (isSingleValue) {
SQLExpr expr = expr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,13 @@ static void resolve(SchemaResolveVisitor visitor, SQLInsertStatement x) {
visitor.visit(query);
}

if (x instanceof OracleInsertStatement) {
SQLObject returning = ((OracleInsertStatement) x).getReturning();
if (returning != null) {
returning.accept(visitor);
}
}

visitor.popContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4806,9 +4806,13 @@ public boolean visit(SQLInSubQueryExpr x) {

@Override
public boolean visit(SQLListExpr x) {
print('(');
if (x.isParenthesized()) {
print('(');
}
printAndAccept(x.getItems(), ", ");
print(')');
if (x.isParenthesized()) {
print(')');
}

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleInsertStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitorAdapter;
import com.alibaba.druid.sql.repository.SchemaObject;
Expand Down Expand Up @@ -1184,6 +1185,9 @@ public boolean visit(SQLInsertStatement x) {

accept(x.getColumns());
accept(x.getQuery());
if (x instanceof OracleInsertStatement) {
accept(((OracleInsertStatement) x).getReturning());
}

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,36 @@ public void test_0() throws Exception {
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
}

public void test_1() throws Exception {
String sql = "BEGIN\n" +
"\tINSERT INTO employees (first_name, last_name, job_title)\n" +
"\tVALUES (?, ?, ?)\n" +
"\tRETURNING employee_id INTO ?;\n" +
"\tCOMMIT;\n" +
"END;";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statemen.accept(visitor);

System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));

Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(4, visitor.getColumns().size());

Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "first_name")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "last_name")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "job_title")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "employee_id")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void test_0() throws Exception {
+ "\t, in_ProductNo, in_Order_Payment, in_OrderCnt, in_Commision, in_RebateFee\n"
+ "\t, in_ProdDiscount, in_CreditCardFee, in_ServiceFee, in_ActivityNo, in_ProductShelfNo\n"
+ "\t, IN_PayOrganID, IN_CorrelationID\n"
+ "INTO (:b0, :b1, :b2:b3, :b4:b5, :b6:b7, :b8:b9, :b10:b11, :b12:b13, :b14:b15, :b16:b17, :b18:b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30:b31, :b32:b33, :b34:b35, :b36:b37, :b38:b39, :b40:b41, :b42:b43, :b44:b45, :b46:b47, :b48:b49, :b50:b51, :b52:b53, :b54:b55, :b56:b57, :b58:b59, :b60:b61, :b62:b63, :b64:b65, :b66:b67, :b68:b69, :b70:b71, :b72:b73, :b74:b75, :b76:b77, :b78:b79, :b80:b81, :b82:b83, :b84:b85, :b86:b87, :b88:b89, :b90:b91, :b92:b93, :b94:b95, :b96:b97, :b98:b99, :b100:b101, :b102:b103, :b104:b105)\n"
+ "INTO :b0, :b1, :b2:b3, :b4:b5, :b6:b7, :b8:b9, :b10:b11, :b12:b13, :b14:b15, :b16:b17, :b18:b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30:b31, :b32:b33, :b34:b35, :b36:b37, :b38:b39, :b40:b41, :b42:b43, :b44:b45, :b46:b47, :b48:b49, :b50:b51, :b52:b53, :b54:b55, :b56:b57, :b58:b59, :b60:b61, :b62:b63, :b64:b65, :b66:b67, :b68:b69, :b70:b71, :b72:b73, :b74:b75, :b76:b77, :b78:b79, :b80:b81, :b82:b83, :b84:b85, :b86:b87, :b88:b89, :b90:b91, :b92:b93, :b94:b95, :b96:b97, :b98:b99, :b100:b101, :b102:b103, :b104:b105\n"
+ "FROM b2b_payment_ReconDetail\n" + "WHERE (Recon_seq_id = :b106\n"
+ "\tAND transaction_id = :b107)", text);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public void test_2() throws Exception {
+ "\t, ACCOUNT_ID, SUBTOTAL_DETAILBILL_ID, NVL(ACCT_HOME_COUNTY, -1)\n"
+ "\t, TO_CHAR(NVL(BILLCYCLE_INURE_DATE, TO_DATE(TO_CHAR(GEN_TIME, 'yyyymm'), 'yyyymm')), 'yyyymmdd')\n"
+ "\t, NVL(BILLCYCLE_MONTH, -1)\n" + "\t, NVL(Detailbill_Flag, 0)\n"
+ "INTO (:b0, :b1, :b2, :b3, :b4, :b5, :b6, :b7, :b8, :b9, :b10, :b11, :b12, :b13, :b14, :b15, :b16, :b17, :b18, :b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30, :b31, :b32, :b33)\n"
+ "INTO :b0, :b1, :b2, :b3, :b4, :b5, :b6, :b7, :b8, :b9, :b10, :b11, :b12, :b13, :b14, :b15, :b16, :b17, :b18, :b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30, :b31, :b32, :b33\n"
+ "FROM DebtBill_Item\n" + "WHERE ROWID = :b34", text);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/bvt/parser/postgresql/17.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ INSERT INTO profile_group_execution
, status, count, error_msg, error_code)
VALUES (now(), now(), ?, ?, ?
, ?, ?, ?, ?)
RETURNING (id, gmt_create, gmt_modified, group_id, mode, sql, status, count, error_msg, error_code)
RETURNING id, gmt_create, gmt_modified, group_id, mode, sql, status, count, error_msg, error_code
------------------------------------------------------------------------------------------------------------------------
insert into test1 as xx (tid,tid1,tvalue)
values (1,1,1),(2,2,2)
Expand Down

0 comments on commit 558ea7d

Please sign in to comment.