Skip to content

Commit

Permalink
终于优化完括号解析和输出逻辑
Browse files Browse the repository at this point in the history
终于优化完括号解析和输出逻辑
  • Loading branch information
lizongbo committed May 3, 2024
1 parent 5808e65 commit 0c1332a
Show file tree
Hide file tree
Showing 10 changed files with 1,675 additions and 1,659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Deprecated
public class ConfigTools {
@Deprecated
private static final String DEFAULT_PRIVATE_KEY_STRING = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAocbCrurZGbC5GArEHKlAfDSZi7gFBnd4yxOt0rwTqKBFzGyhtQLu5PRKjEiOXVa95aeIIBJ6OhC2f8FjqFUpawIDAQABAkAPejKaBYHrwUqUEEOe8lpnB6lBAsQIUFnQI/vXU4MV+MhIzW0BLVZCiarIQqUXeOhThVWXKFt8GxCykrrUsQ6BAiEA4vMVxEHBovz1di3aozzFvSMdsjTcYRRo82hS5Ru2/OECIQC2fAPoXixVTVY7bNMeuxCP4954ZkXp7fEPDINCjcQDywIgcc8XLkkPcs3Jxk7uYofaXaPbg39wuJpEmzPIxi3k0OECIGubmdpOnin3HuCP/bbjbJLNNoUdGiEmFL5hDI4UdwAdAiEAtcAwbm08bKN7pwwvyqaCBC//VnEWaq39DCzxr+Z2EIk=";
@Deprecated
public static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ==";

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public SQLExpr expr() {
parenthesized = false;
}
}
if (parenthesized && expr instanceof SQLQueryExpr) {
parenthesized = false;
((SQLQueryExpr) expr).setParenthesized(true);
}
if (parenthesized && expr instanceof SQLIdentifierExpr) {
parenthesized = false;
((SQLIdentifierExpr) expr).setParenthesized(true);
Expand Down Expand Up @@ -1385,7 +1389,10 @@ public SQLExpr primary() {
if (beforeComments != null) {
expr.addBeforeComment(beforeComments);
}

if (lexer.hasComment() && lexer.isKeepComments()) {
// @todo 是否保留注释,暂时待定,因为保留的话,有20来个测试用例会失败 by lizongbo
// expr.addAfterComment(lexer.readAndResetComments());
}
return expr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,7 @@ public boolean visit(SQLBinaryOpExprGroup x) {
} else {
//bracket = !parameterized && !((SQLBinaryOpExpr) item).isParenthesized();
}
if (bracket) {
print('(');
visit(binaryOpExpr);
print(')');
} else {
visit(binaryOpExpr);
}
visit(binaryOpExpr);
//
// if (item.hasAfterComment() && !parameterized) {
// print(' ');
Expand Down Expand Up @@ -1794,15 +1788,20 @@ public boolean visit(SQLContainsExpr x) {

public boolean visit(SQLIntegerExpr x) {
boolean parameterized = this.parameterized;
if (x.isParenthesized() && !parameterized) {
print('(');
}
printInteger(x, parameterized);
if (x.isParenthesized() && !parameterized) {
print(')');
}
return false;
}

private static final Integer ONE = Integer.valueOf(1);

protected void printInteger(SQLIntegerExpr x, boolean parameterized) {
Number number = x.getNumber();

if (number.equals(ONE)) {
if (DbType.oracle.equals(dbType)) {
SQLObject parent = x.getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void test_0() throws Exception {
System.out.println(stmt.toString());
assertEquals(1, statementList.size());

assertEquals("SELECT (~43), ((tinyint_1bit_test % integer_test % bigint_test) NOT IN (1, 2, 'a', (BINARY 'a' = 'a ')))\n" +
assertEquals("SELECT (~(43)), ((tinyint_1bit_test % integer_test % bigint_test) NOT IN (1, 2, 'a', (BINARY 'a' = 'a ')))\n" +
"FROM select_base_two_one_db_multi_tb", stmt.toString());

assertEquals("SELECT (~?), ((tinyint_1bit_test % integer_test % bigint_test) NOT IN (?, ?, ?, (BINARY ? = ?)))\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,49 +90,49 @@ public void test_0() throws Exception {

assertEquals(1, statementList.size());

assertEquals("/*+engine=MPP*/\n" +
"WITH year_total AS (\n" +
"\t\tSELECT c_customer_id AS customer_id, c_first_name AS customer_first_name, c_last_name AS customer_last_name, c_preferred_cust_flag AS customer_preferred_cust_flag, c_birth_country AS customer_birth_country\n" +
"\t\t\t, c_login AS customer_login, c_email_address AS customer_email_address, d_year AS dyear\n" +
"\t\t\t, sum(ss_ext_list_price - ss_ext_discount_amt) AS year_total, 's' AS sale_type\n" +
"\t\tFROM customer, store_sales, date_dim\n" +
"\t\tWHERE c_customer_sk = ss_customer_sk\n" +
"\t\t\tAND ss_sold_date_sk = d_date_sk\n" +
"\t\tGROUP BY c_customer_id, c_first_name, c_last_name, c_preferred_cust_flag, c_birth_country, c_login, c_email_address, d_year\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT c_customer_id AS customer_id, c_first_name AS customer_first_name, c_last_name AS customer_last_name, c_preferred_cust_flag AS customer_preferred_cust_flag, c_birth_country AS customer_birth_country\n" +
"\t\t\t, c_login AS customer_login, c_email_address AS customer_email_address, d_year AS dyear\n" +
"\t\t\t, sum(ws_ext_list_price - ws_ext_discount_amt) AS year_total, 'w' AS sale_type\n" +
"\t\tFROM customer, web_sales, date_dim\n" +
"\t\tWHERE c_customer_sk = ws_bill_customer_sk\n" +
"\t\t\tAND ws_sold_date_sk = d_date_sk\n" +
"\t\tGROUP BY c_customer_id, c_first_name, c_last_name, c_preferred_cust_flag, c_birth_country, c_login, c_email_address, d_year\n" +
"\t)\n" +
"SELECT t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name, t_s_secyear.customer_preferred_cust_flag, t_s_secyear.customer_birth_country\n" +
"\t, t_s_secyear.customer_login\n" +
"FROM year_total t_s_firstyear, year_total t_s_secyear, year_total t_w_firstyear, year_total t_w_secyear\n" +
"WHERE t_s_secyear.customer_id = t_s_firstyear.customer_id\n" +
"\tAND t_s_firstyear.customer_id = t_w_secyear.customer_id\n" +
"\tAND t_s_firstyear.customer_id = t_w_firstyear.customer_id\n" +
"\tAND t_s_firstyear.sale_type = 's'\n" +
"\tAND t_w_firstyear.sale_type = 'w'\n" +
"\tAND t_s_secyear.sale_type = 's'\n" +
"\tAND t_w_secyear.sale_type = 'w'\n" +
"\tAND t_s_firstyear.dyear = 2001\n" +
"\tAND t_s_secyear.dyear = 2001 + 1\n" +
"\tAND t_w_firstyear.dyear = 2001\n" +
"\tAND t_w_secyear.dyear = 2001 + 1\n" +
"\tAND t_s_firstyear.year_total > 0\n" +
"\tAND t_w_firstyear.year_total > 0\n" +
"\tAND CASE \n" +
"\t\tWHEN t_w_firstyear.year_total > 0 THEN t_w_secyear.year_total / t_w_firstyear.year_total\n" +
"\t\tELSE DECIMAL '0.0'\n" +
"\tEND > CASE \n" +
"\t\tWHEN t_s_firstyear.year_total > 0 THEN t_s_secyear.year_total / t_s_firstyear.year_total\n" +
"\t\tELSE DECIMAL '0.0'\n" +
"\tEND\n" +
"ORDER BY t_s_secyear.customer_id ASC, t_s_secyear.customer_first_name ASC, t_s_secyear.customer_last_name ASC, t_s_secyear.customer_preferred_cust_flag ASC\n" +
"LIMIT 100", stmt.toString());
assertEquals("/*+engine=MPP*/\n"
+ "WITH year_total AS (\n"
+ "\t\tSELECT c_customer_id AS customer_id, c_first_name AS customer_first_name, c_last_name AS customer_last_name, c_preferred_cust_flag AS customer_preferred_cust_flag, c_birth_country AS customer_birth_country\n"
+ "\t\t\t, c_login AS customer_login, c_email_address AS customer_email_address, d_year AS dyear\n"
+ "\t\t\t, sum((ss_ext_list_price - ss_ext_discount_amt)) AS year_total, 's' AS sale_type\n"
+ "\t\tFROM customer, store_sales, date_dim\n"
+ "\t\tWHERE (c_customer_sk = ss_customer_sk)\n"
+ "\t\t\tAND (ss_sold_date_sk = d_date_sk)\n"
+ "\t\tGROUP BY c_customer_id, c_first_name, c_last_name, c_preferred_cust_flag, c_birth_country, c_login, c_email_address, d_year\n"
+ "\t\tUNION ALL\n"
+ "\t\tSELECT c_customer_id AS customer_id, c_first_name AS customer_first_name, c_last_name AS customer_last_name, c_preferred_cust_flag AS customer_preferred_cust_flag, c_birth_country AS customer_birth_country\n"
+ "\t\t\t, c_login AS customer_login, c_email_address AS customer_email_address, d_year AS dyear\n"
+ "\t\t\t, sum((ws_ext_list_price - ws_ext_discount_amt)) AS year_total, 'w' AS sale_type\n"
+ "\t\tFROM customer, web_sales, date_dim\n"
+ "\t\tWHERE (c_customer_sk = ws_bill_customer_sk)\n"
+ "\t\t\tAND (ws_sold_date_sk = d_date_sk)\n"
+ "\t\tGROUP BY c_customer_id, c_first_name, c_last_name, c_preferred_cust_flag, c_birth_country, c_login, c_email_address, d_year\n"
+ "\t)\n"
+ "SELECT t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name, t_s_secyear.customer_preferred_cust_flag, t_s_secyear.customer_birth_country\n"
+ "\t, t_s_secyear.customer_login\n"
+ "FROM year_total t_s_firstyear, year_total t_s_secyear, year_total t_w_firstyear, year_total t_w_secyear\n"
+ "WHERE (t_s_secyear.customer_id = t_s_firstyear.customer_id)\n"
+ "\tAND (t_s_firstyear.customer_id = t_w_secyear.customer_id)\n"
+ "\tAND (t_s_firstyear.customer_id = t_w_firstyear.customer_id)\n"
+ "\tAND (t_s_firstyear.sale_type = 's')\n"
+ "\tAND (t_w_firstyear.sale_type = 'w')\n"
+ "\tAND (t_s_secyear.sale_type = 's')\n"
+ "\tAND (t_w_secyear.sale_type = 'w')\n"
+ "\tAND (t_s_firstyear.dyear = 2001)\n"
+ "\tAND (t_s_secyear.dyear = (2001 + 1))\n"
+ "\tAND (t_w_firstyear.dyear = 2001)\n"
+ "\tAND (t_w_secyear.dyear = (2001 + 1))\n"
+ "\tAND (t_s_firstyear.year_total > 0)\n"
+ "\tAND (t_w_firstyear.year_total > 0)\n"
+ "\tAND ((CASE \n"
+ "\t\tWHEN (t_w_firstyear.year_total > 0) THEN (t_w_secyear.year_total / t_w_firstyear.year_total)\n"
+ "\t\tELSE DECIMAL '0.0'\n"
+ "\tEND) > (CASE \n"
+ "\t\tWHEN (t_s_firstyear.year_total > 0) THEN (t_s_secyear.year_total / t_s_firstyear.year_total)\n"
+ "\t\tELSE DECIMAL '0.0'\n"
+ "\tEND))\n"
+ "ORDER BY t_s_secyear.customer_id ASC, t_s_secyear.customer_first_name ASC, t_s_secyear.customer_last_name ASC, t_s_secyear.customer_preferred_cust_flag ASC\n"
+ "LIMIT 100", stmt.toString());


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,35 @@ public void test_6() throws Exception {

System.out.println(stmt.toString());
}
public void test_9999() throws Exception {
String sql = "SELECT NULLIF ( 5, + 589999 * MIN( 67 ) + COUNT( * ) * 8 ) * 47 + CAST( NULL AS SIGNED ) from aaa";

SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);
System.out.println(stmt.toString());
}
public void test_7() throws Exception {
String sql = "SELECT + CASE WHEN 33 NOT BETWEEN - + 16 AND ( + COUNT( * ) "
+ "+ COUNT( * ) / - COALESCE ( - 27, ( - MAX( ALL 41 ) ) / 24 * - - 95 - - 80 + - COUNT( * ) * CAST( NULL AS DECIMAL ) / + 76 - - + 74 * - 49 + - - 25 ) * 89 * - "
+ "NULLIF ( - - SUM( DISTINCT + 57 ), COUNT( * ) ) - 29 - + MAX( - - 43 ) - + + MAX( DISTINCT + 90 ) + CASE - + 26 "
+ "WHEN NULLIF ( 5, + 58 * MIN( 67 ) + COUNT( * ) * 8 ) * 47 + CAST( NULL AS SIGNED ) "
+ "WHEN NULLIF ( 5, + 589999 * MIN( 67 ) + COUNT( * ) * 8 ) * 47 + CAST( NULL AS SIGNED ) "
+ "THEN 57 WHEN + 45 THEN NULL ELSE CAST( 35 AS SIGNED ) * 56 END * CAST( NULL AS SIGNED ) ) "
+ "THEN NULL WHEN NOT + ( - 10 ) / 42 IS NULL THEN + 93 ELSE 54 END * + 36";

SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT (+CASE \n" +
"\t\tWHEN 33 NOT BETWEEN (-(+16)) AND (+COUNT(*) + COUNT(*) / -COALESCE(-27, (-MAX(ALL 41)) / 24 * --95 - -80 + (-COUNT(*)) * CAST(NULL AS DECIMAL) / +76 - (-(+74)) * -49 + --25) * 89 * -NULLIF(-(-SUM(DISTINCT +57)), COUNT(*)) - 29 - +MAX(--43) - +(+MAX(DISTINCT +90)) + CASE -(+26)\n" +
"\t\t\t\t\tWHEN NULLIF(5, (+58) * MIN(67) + COUNT(*) * 8) * 47 + CAST(NULL AS SIGNED) THEN 57\n" +
assertEquals("SELECT +CASE \n" +
"\t\tWHEN 33 NOT BETWEEN -+16 AND (+COUNT(*) + COUNT(*) / -COALESCE(-27, (-MAX(ALL 41)) / 24 * --95 - -80 + -COUNT(*) * CAST(NULL AS DECIMAL) / +76 - -+74 * -49 + --25) * 89 * -"
+ "NULLIF(--SUM(DISTINCT +57), COUNT(*)) - 29 - +MAX(--43) - ++MAX(DISTINCT +90) + CASE -+26\n" +
"\t\t\t\t\tWHEN NULLIF(5, +589999 * MIN(67) + COUNT(*) * 8) * 47 + CAST(NULL AS SIGNED) THEN 57\n" +
"\t\t\t\t\tWHEN +45 THEN NULL\n" +
"\t\t\t\t\tELSE CAST(35 AS SIGNED) * 56\n" +
"\t\t\t\tEND * CAST(NULL AS SIGNED))\n" +
"\t\tTHEN NULL\n" +
"\t\tWHEN NOT (+-10) / 42 IS NULL THEN +93\n" +
"\t\tWHEN NOT +(-10) / 42 IS NULL THEN +93\n" +
"\t\tELSE 54\n" +
"\tEND) * +36", stmt.toString());
"\tEND * +36", stmt.toString());

System.out.println(stmt.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@ public void test_0() throws Exception {
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT (0 IN (20 = ANY (\n" +
assertEquals("SELECT 0 IN (20 = ANY (\n" +
"\t\tSELECT col1\n" +
"\t\tFROM t1\n" +
"\t)) IS NOT NULL) IS NOT unknown AS t;", //
"\t)) IS NOT NULL IS NOT unknown AS t;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select (0 in (20 = any (\n" +
assertEquals("select 0 in (20 = any (\n" +
"\t\tselect col1\n" +
"\t\tfrom t1\n" +
"\t)) is not null) is not unknown as t;", //
"\t)) is not null is not unknown as t;", //
output);
}

{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT (? IN (? = ANY (\n" +
assertEquals("SELECT ? IN (? = ANY (\n" +
"\t\tSELECT col1\n" +
"\t\tFROM t1\n" +
"\t)) IS NOT NULL) IS NOT unknown AS t;", //
"\t)) IS NOT NULL IS NOT unknown AS t;", //
output);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void test_0() throws Exception {
"\t\"SQFAS\" NUMBER,\n" +
"\t\"TQFAS\" NUMBER,\n" +
"\t\"TJSJ\" DATE DEFAULT SYSDATE,\n" +
"\t\"FATB\" NUMBER GENERATED ALWAYS AS ROUND((\"FAS\" - \"TQFAS\") / DECODE(\"TQFAS\", 0, 1, \"TQFAS\"), 2) * 100 VIRTUAL VISIBLE,\n" +
"\t\"FAHB\" NUMBER GENERATED ALWAYS AS ROUND((\"FAS\" - \"SQFAS\") / DECODE(\"SQFAS\", 0, 1, \"SQFAS\"), 2) * 100 VIRTUAL VISIBLE\n" +
"\t\"FATB\" NUMBER GENERATED ALWAYS AS (ROUND((\"FAS\" - \"TQFAS\") / DECODE(\"TQFAS\", 0, 1, \"TQFAS\"), 2) * 100) VIRTUAL VISIBLE,\n" +
"\t\"FAHB\" NUMBER GENERATED ALWAYS AS (ROUND((\"FAS\" - \"SQFAS\") / DECODE(\"SQFAS\", 0, 1, \"SQFAS\"), 2) * 100) VIRTUAL VISIBLE\n" +
")\n" +
"PCTFREE 10\n" +
"PCTUSED 40\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public void test_0() throws Exception {

{
String text = SQLUtils.toOracleString(stmt);

//@todo 这里的注释信息断言暂时去掉
assertEquals("SELECT e.area_name AS 区域, e.department_user_id AS 店铺代码, e.department_name AS 店铺名称, a.card_id AS 会员卡号, a.vip_name AS 姓名\n" +
"\t, CASE \n" +
"\t, (CASE \n" +
"\t\tWHEN a.vip_sex = '1' THEN '男'\n" +
"\t\tWHEN a.vip_sex = '2' THEN '女'\n" +
"\t\tELSE '保密'\n" +
"\tEND AS 性别\n" +
"\tEND) AS 性别\n" +
"\t, a.vip_birthday_year || '-' || a.vip_birthday_month || '-' || a.vip_birthday_day AS 出生日期\n" +
"\t, a.vip_create_date AS 会员注册日期, a.vip_mobile AS 手机号, a.vip_job AS 职业, a.wechat AS 微信号, a.vip_email AS 邮箱\n" +
"\t, d.viptype_name AS 会员等级\n" +
Expand All @@ -96,7 +96,7 @@ public void test_0() throws Exception {
"JOIN D0169 d ON d.viptype_id = c.viptype_id\n" +
"\tAND d.language_id = 'zh-cn' \n" +
"\tJOIN area_store_hn e ON a.department_id = e.department_id \n" +
"WHERE a.vip_create_date BETWEEN TRUNC(SYSDATE) - 4 - 10 / 24 AND TRUNC(SYSDATE) - 10 / 24 ----注册日期\n" +
"WHERE a.vip_create_date BETWEEN TRUNC(SYSDATE) - 4 - 10 / 24 AND TRUNC(SYSDATE) - 10 / 24\n" +
"\tAND (a.vip_state = '0'\n" +
"\t\tOR a.vip_state = '1')\n" +
"\tAND e.department_user_id IN (\n" +
Expand Down

0 comments on commit 0c1332a

Please sign in to comment.