Merge pull request #3247 from LeonemZhang/v5-dev

Fix: issue#2845 SqlUtil.formatSql 格式化的sql换行异常
This commit is contained in:
Golden Looly 2023-08-04 15:39:12 +08:00 committed by GitHub
commit 7f814b1fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -609,6 +609,15 @@ public class SqlBuilder implements Builder<String> {
return this.build();
}
/**
* 格式化SQL语句
* @return SqlBuilder
*/
public SqlBuilder format() {
this.sql.replace(0, this.sql.length(), SqlFormatter.format(this.sql.toString()));
return this;
}
// --------------------------------------------------------------- private method start
/**

View File

@ -110,6 +110,12 @@ public class SqlFormatter {
t = this.tokens.nextToken();
this.token += t;
} while (!"\"".equals(t));
} else if ("`".equals(this.token)) {
String t;
do {
t = this.tokens.nextToken();
this.token += t;
} while (!"`".equals(t));
}
if ((this.afterByOrSetOrFromOrSelect) && (",".equals(this.token))) {

View File

@ -11,4 +11,18 @@ public class SqlFormatterTest {
String sql = "(select 1 from dual) union all (select 1 from dual)";
SqlFormatter.format(sql);
}
@Test
public void testKeyword() {
String sql = "select * from `order`";
String format = SqlFormatter.format(sql);
System.out.println(format);
}
@Test
public void testSqlBuilderFormat() {
String sql = "SELECT `link_table_a`.`value_a` AS `link_table_a.value_a`,`link_table_a`.`id` AS `link_table_a.id`,`link_table_b`.`value_b` AS `link_table_b.value_b`,`link_table_c`.`id` AS `link_table_c.id`,`link_table_b`.`id` AS `link_table_b.id`,`link_table_c`.`value_c` AS `link_table_c.value_c` FROM `link_table_a` INNER JOIN `link_table_b` ON `link_table_a`.`table_b_id` = `link_table_b`.`id` INNER JOIN `link_table_c` ON `link_table_b`.`table_c_id` = `link_table_c`.`id`";
String format = SqlBuilder.of(sql).format().build();
System.out.println(format);
}
}