Skip to content

Commit 868918b

Browse files
authored
fixed broken INSERT statement
1 parent f48caf9 commit 868918b

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

MiniSqlParser/Visitors/BeautifulStringifier.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,23 @@ private void AppendNewLine() {
228228
}
229229
}
230230

231-
private void DeleteString(int length) {
232-
if(length > _sql.Length) {
231+
// 削除対象のSQL文字列を末尾から走査し指定文字cがあれば
232+
// 末尾からcまでの空白/改行文字も含め削除する
233+
private void RemoveTailCharIf(char c) {
234+
if(_sql.Length == 0) {
233235
return;
234236
}
235-
//// 削除対象の文字列に改行が含まれている場合は削除処理を中断する
236-
//for(var i = 0; i < length; ++i) {
237-
// var index = _sql.Length - i - 1;
238-
// if(_newline.Contains(_sql[index].ToString())) {
239-
// return;
240-
// }
241-
//}
242-
_sql.Remove(_sql.Length - length - 1, length);
237+
238+
int i = _sql.Length -1;
239+
int j = 0;
240+
while(char.IsWhiteSpace(_sql[i])) {
241+
--i;
242+
++j;
243+
}
244+
245+
if(_sql[i] == c) {
246+
_sql.Remove(i, j + 1);
247+
}
243248
}
244249

245250
public override void VisitOnSeparator(Node node, int offset, int i) {
@@ -1479,7 +1484,7 @@ public override void VisitOnInsert(InsertStmt insertStmt) {
14791484
public override void VisitOnValues(InsertValuesStmt insertValuesStmt, int offset) {
14801485
if(insertValuesStmt.HasTableColumns) {
14811486
// INSERT-VALUES文のVALUESの両端に括弧を配置する
1482-
this.DeleteString(2);
1487+
this.RemoveTailCharIf(')');
14831488
this.AppendNewLine();
14841489
this.AppendSymbol(")");
14851490
} else {

0 commit comments

Comments
 (0)