SQLASTOutputVisitor add method printWhere & printHaving

This commit is contained in:
shaojin.wensj
2022-10-20 23:55:03 +08:00
parent 569f4a66ca
commit 9fc7829178
11 changed files with 62 additions and 60 deletions
@@ -193,9 +193,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
printExpr(where, parameterized);
printWhere(where);
}
printHierarchical(x);
@@ -448,17 +448,7 @@ public class OdpsOutputVisitor extends HiveOutputVisitor implements OdpsASTVisit
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
if (where.hasBeforeComment() && isPrettyFormat()) {
printlnComments(x.getWhere().getBeforeCommentsDirect());
}
where.accept(this);
if (where.hasAfterComment() && isPrettyFormat()) {
print(' ');
printlnComment(x.getWhere().getAfterCommentsDirect());
}
printWhere(where);
}
if (x.getGroupBy() != null) {
@@ -410,9 +410,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
if (x.getWhere() != null) {
println();
print0(ucase ? "WHERE " : "where ");
x.getWhere().accept(this);
printWhere(x.getWhere());
}
printHierarchical(x);
@@ -185,14 +185,7 @@ public class OscarOutputVisitor extends SQLASTOutputVisitor implements OscarASTV
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
where.accept(this);
if (where.hasAfterComment() && isPrettyFormat()) {
print(' ');
printlnComment(x.getWhere().getAfterCommentsDirect());
}
printWhere(where);
}
if (x.getGroupBy() != null) {
@@ -143,14 +143,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
where.accept(this);
if (where.hasAfterComment() && isPrettyFormat()) {
print(' ');
printlnComment(x.getWhere().getAfterCommentsDirect());
}
printWhere(where);
}
if (x.getGroupBy() != null) {
@@ -77,9 +77,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
printExpr(where);
printWhere(where);
}
SQLSelectGroupByClause groupBy = x.getGroupBy();
@@ -2391,10 +2391,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
this.indentCount--;
}
if (x.getHaving() != null) {
println();
print0(ucase ? "HAVING " : "having ");
x.getHaving().accept(this);
SQLExpr having = x.getHaving();
if (having != null) {
printHaving(having);
}
if (x.isWithRollUp() && !paren) {
@@ -2408,6 +2407,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return false;
}
protected void printHaving(SQLExpr having) {
println();
print0(ucase ? "HAVING " : "having ");
having.accept(this);
}
public boolean visit(SQLSelect x) {
SQLHint headHint = x.getHeadHint();
if (headHint != null) {
@@ -2492,14 +2497,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
printExpr(where, parameterized);
if (where.hasAfterComment() && isPrettyFormat()) {
print(' ');
printlnComment(x.getWhere().getAfterCommentsDirect());
}
printWhere(where);
}
printHierarchical(x);
@@ -2556,6 +2554,24 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return false;
}
protected void printWhere(SQLExpr where) {
println();
print0(ucase ? "WHERE " : "where ");
List<String> beforeComments = where.getBeforeCommentsDirect();
if (beforeComments != null && !beforeComments.isEmpty() && isPrettyFormat()) {
printlnComments(beforeComments);
}
printExpr(where, parameterized);
List<String> afterComments = where.getAfterCommentsDirect();
if (afterComments != null && !afterComments.isEmpty() && isPrettyFormat()) {
print(' ');
printlnComment(afterComments);
}
}
protected void printFetchFirst(SQLSelectQueryBlock x) {
SQLLimit limit = x.getLimit();
if (limit == null) {
@@ -47,7 +47,8 @@ public class MySqlSelectTest_243 extends MysqlTest {
"\tAND enable_status = 1 -- AND seller_id= 100154704\n" +
"\tAND buyer_full_name LIKE concat('%', 'Zaini Abd', '%')\n" +
"\tAND _features.wt = 'dropshipping'\n" +
"\tAND _features.ads = 'pending'\n" +
"\tAND _features.ads = 'pending' -- GROUP BY buyer_id,\n" +
"-- buyer_full_name \n" +
"LIMIT 0, 10", stmt.toString());
@@ -561,7 +561,8 @@ public class MySqlSelectTest_290 extends MysqlTest {
"\t\tWHERE document_status_code != 'FI0007004'\n" +
"\t\t\tAND (actual_amount_total > 0\n" +
"\t\t\t\tOR (actual_amount_total < 0\n" +
"\t\t\t\t\tAND deductible = 1))\n" +
"\t\t\t\t\tAND deductible = 1)) -- <projectId> AND octt.project_id in ( ?{projectId} )</projectId>\n" +
"\t\t-- <endDate1> AND rh.actual_received_date<= ?{endDate1}</endDate1>\n" +
"\t\tGROUP BY octt.id\n" +
"\t) actualReceiMoney\n" +
"\tON tran.id = actualReceiMoney.id\n" +
@@ -572,7 +573,8 @@ public class MySqlSelectTest_290 extends MysqlTest {
"\t\t\tLEFT JOIN midea_sd_actual_received_head rh ON ar.actual_received_head_id = rh.id\n" +
"\t\tWHERE rh.document_status_code != 'FI0007004'\n" +
"\t\t\tAND ar.fund_type_code IN ('FIFT01', 'FIFT02')\n" +
"\t\t\tAND ar.actual_amount_total > 0\n" +
"\t\t\tAND ar.actual_amount_total > 0 -- <projectId> AND octt.project_id in ( ?{projectId} )</projectId>\n" +
"\t\t-- <endDate1> AND rh.actual_received_date<= ?{endDate1}</endDate1>\n" +
"\t\tGROUP BY octt.id\n" +
"\t) act1_date\n" +
"\tON tran.id = act1_date.id\n" +
@@ -637,7 +639,7 @@ public class MySqlSelectTest_290 extends MysqlTest {
"\t\t\tEND) AS whkAmount\n" +
"\t\tFROM midea_sd_payment_plan pp\n" +
"\t\t\tJOIN midea_sd_order_contract_transaction ort ON ort.id = pp.transaction_id\n" +
"\t\tWHERE 1 = 1\n" +
"\t\tWHERE 1 = 1 -- <projectId> AND ort.project_id in ( ?{projectId} )</projectId>\n" +
"\t\tGROUP BY ort.id\n" +
"\t) paymentplan\n" +
"\tON tran.id = paymentplan.id\n" +
@@ -649,12 +651,12 @@ public class MySqlSelectTest_290 extends MysqlTest {
"\t\t\t\tSELECT r2.transaction_id, MAX(r1.exec_date) AS exec_date\n" +
"\t\t\t\tFROM midea_sd_reserve_area r1\n" +
"\t\t\t\t\tLEFT JOIN midea_sd_reserve_area_detail r2 ON r2.reserve_area_id = r1.id\n" +
"\t\t\t\tWHERE r1.status_code = 'SD050104'\n" +
"\t\t\t\tWHERE r1.status_code = 'SD050104' -- <projectId> AND r1.project_id in ( ?{projectId} )</projectId>\n" +
"\t\t\t\tGROUP BY r2.contract_id\n" +
"\t\t\t) ra3\n" +
"\t\t\tON ra3.transaction_id = ra2.transaction_id\n" +
"\t\t\t\tAND ra3.exec_date = ra1.exec_date\n" +
"\t\tWHERE ra1.status_code = 'SD050104'\n" +
"\t\tWHERE ra1.status_code = 'SD050104' -- <projectId> AND ra1.project_id in ( ?{projectId} )</projectId> \n" +
"\t) areaDetail\n" +
"\tON areaDetail.transaction_id = tran.id\n" +
"\tLEFT JOIN midea_sd_payment_method payment ON tran.payment_method_id = payment.id\n" +
@@ -668,13 +670,16 @@ public class MySqlSelectTest_290 extends MysqlTest {
"\t\t\tON tran.id = salesMemberItem.transaction_id\n" +
"\t\t\t\tAND salesMemberItem.modify_type != 1\n" +
"\t\t\tLEFT JOIN midea_sd_user_account ON salesMemberItem.sale_member_id = midea_sd_user_account.id\n" +
"\t\tWHERE 1 = 1\n" +
"\t\tWHERE 1 = 1 -- <projectId> AND tran.project_id in ( ?{projectId} )</projectId>\n" +
"\t\tGROUP BY tran.id\n" +
"\t) salesTeam\n" +
"\tON salesTeam.id = tran.id\n" +
"WHERE contract.status_code IN ('SD040501', 'SD040502') -- <orgId> AND org.id in (?{orgId}) </orgId>\n" +
"-- <projectId> AND tran.project_id in ( ?{projectId} )</projectId>\n" +
"-- <startDate2>AND contract.vesting_date >= ?{startDate2} </startDate2>\n" +
"-- <endDate2> AND contract.vesting_date <= ?{endDate2} </endDate2> -- <orgId> AND org.id in (?{orgId}) </orgId>\n" +
"-- <projectId> AND tran.project_id in ( ?{projectId} )</projectId>\n" +
"-- <startDate2>AND contract.vesting_date >= ?{startDate2} </startDate2>\n" +
"-- <endDate2> AND contract.vesting_date <= ?{endDate2} </endDate2>\n" +
"GROUP BY contract.id", stmt.toString());
@@ -209,17 +209,27 @@ public class MySqlSelectTest_plus_sub_comment extends MysqlTest {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("SELECT *\n" + "FROM mp_Sites\n" + "WHERE SiteID = -1\n" + "\tOR -1 = -1", stmt.toString());
assertEquals("SELECT *\n" +
"FROM mp_Sites\n" +
"WHERE SiteID = -1\n" +
"\tOR -1 = -1 -- ORDER BY SiteID LIMIT ", stmt.toString());
}
public void test_19() throws Exception {
String sql = "-- comments\n"
+ "SELECT * FROM mp_Sites WHERE SiteID = -1 OR -1 = -1 -- ORDER BY SiteID LIMIT 1";
String sql = "-- comments\n" +
"SELECT *\n" +
"FROM mp_Sites\n" +
"WHERE SiteID = -1\n" +
"\tOR -1 = -1 -- ORDER BY SiteID LIMIT ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("-- comments\nSELECT *\n" + "FROM mp_Sites\n" + "WHERE SiteID = -1\n" + "\tOR -1 = -1", stmt.toString());
assertEquals("-- comments\n" +
"SELECT *\n" +
"FROM mp_Sites\n" +
"WHERE SiteID = -1\n" +
"\tOR -1 = -1 -- ORDER BY SiteID LIMIT", stmt.toString());
}
public void test_20() throws Exception {
@@ -245,7 +245,7 @@ public class OracleCreateViewTest14 extends OracleTest {
"\tAND (tt.status = '3'\n" +
"\t\tOR tt.status = '4'\n" +
"\t\tOR tt.status = '5')\n" +
"\tAND tt.product_id = tb.team_id\n" +
"\tAND tt.product_id = tb.team_id -- 3n,4c,5c*6>\n" +
"UNION\n" +
"/* hU*/\n" +
"SELECT t.order_id AS order_id, 'GN' AS product_type, t.agent_id AS order_agent, t.all_price AS collect_money, t.olduserid AS customer_id\n" +