Skip to content

Commit

Permalink
fmt: add support for comments inside sql db { ... } blocks (fix vla…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 21, 2024
1 parent 9579c1f commit cf03ecd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,8 @@ fn (t Tree) sql_stmt_line(node ast.SqlStmtLine) &Node {
obj.add_terse('updated_columns', t.array_node_string(node.updated_columns))
obj.add_terse('update_exprs', t.array_node_expr(node.update_exprs))
obj.add('pos', t.pos(node.pos))
obj.add('pre_comments', t.array_node_comment(node.pre_comments))
obj.add('end_comments', t.array_node_comment(node.end_comments))

sub_struct_map := new_object()
for key, val in node.sub_structs {
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,8 @@ pub mut:
sub_structs map[int]SqlStmtLine
where_expr Expr
update_exprs []Expr // for `update`
pre_comments []Comment
end_comments []Comment
}

pub struct SqlExpr {
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,9 @@ pub fn (mut f Fmt) sql_stmt(node ast.SqlStmt) {
f.writeln(' {')

for line in node.lines {
f.comments(line.pre_comments, level: .indent)
f.sql_stmt_line(line)
f.comments(line.end_comments, level: .indent)
}
f.write('}')
f.or_expr(node.or_expr)
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/fmt/tests/sqlstmt_comments_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import db.sqlite

fn main() {
dblite := sqlite.connect(':memory:')!
sql dblite {
// some comment
insert address into Address
// some comment2
}!

sql dblite {
// some comment
drop table Address
// some comment2
}!

sql dblite {
// some comment
create table Address
// some comment2
}!
}
10 changes: 10 additions & 0 deletions vlib/v/parser/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fn (mut p Parser) parse_sql_or_block() ast.OrExpr {
}

fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
pre_comments := p.eat_comments()
mut n := p.check_name() // insert
pos := p.tok.pos()
mut kind := ast.SqlStmtKind.insert
Expand All @@ -215,6 +216,7 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
}
typ := p.parse_type()
typ_pos := p.tok.pos()
end_comments := p.eat_comments()
return ast.SqlStmtLine{
kind: kind
pos: pos.extend(p.prev_tok.pos())
Expand All @@ -224,6 +226,8 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
}
scope: p.scope
is_generated: false
pre_comments: pre_comments
end_comments: end_comments
}
} else if n == 'drop' {
kind = .drop
Expand All @@ -234,6 +238,7 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
}
typ := p.parse_type()
typ_pos := p.tok.pos()
end_comments := p.eat_comments()
return ast.SqlStmtLine{
kind: kind
pos: pos.extend(p.prev_tok.pos())
Expand All @@ -243,6 +248,8 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
}
is_generated: false
scope: p.scope
pre_comments: pre_comments
end_comments: end_comments
}
}
mut inserted_var := ''
Expand Down Expand Up @@ -313,6 +320,7 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
return ast.SqlStmtLine{}
}
}
end_comments := p.eat_comments()
return ast.SqlStmtLine{
table_expr: ast.TypeNode{
typ: table_type
Expand All @@ -326,6 +334,8 @@ fn (mut p Parser) parse_sql_stmt_line() ast.SqlStmtLine {
where_expr: where_expr
is_generated: false
scope: p.scope
pre_comments: pre_comments
end_comments: end_comments
}
}

Expand Down

0 comments on commit cf03ecd

Please sign in to comment.