mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge pull request #3247 from LeonemZhang/v5-dev
Fix: issue#2845 SqlUtil.formatSql 格式化的sql换行异常
This commit is contained in:
commit
7f814b1fb3
@ -609,6 +609,15 @@ public class SqlBuilder implements Builder<String> {
|
|||||||
return this.build();
|
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
|
// --------------------------------------------------------------- private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,6 +110,12 @@ public class SqlFormatter {
|
|||||||
t = this.tokens.nextToken();
|
t = this.tokens.nextToken();
|
||||||
this.token += t;
|
this.token += t;
|
||||||
} while (!"\"".equals(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))) {
|
if ((this.afterByOrSetOrFromOrSelect) && (",".equals(this.token))) {
|
||||||
|
@ -11,4 +11,18 @@ public class SqlFormatterTest {
|
|||||||
String sql = "(select 1 from dual) union all (select 1 from dual)";
|
String sql = "(select 1 from dual) union all (select 1 from dual)";
|
||||||
SqlFormatter.format(sql);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user