Skip to content

Commit ec94235

Browse files
committed
Passing SQLite comments to the generated struct
This is a partial solution for #3430 for sqlite, which may or may not be adaptable for other engines. It basically looks for single-line comments before the CREATE command and in-between column definitions.
1 parent 86c1d77 commit ec94235

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

internal/engine/sqlite/convert.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,59 @@ func (c *cc) convertAttach_stmtContext(n *parser.Attach_stmtContext) ast.Node {
107107
}
108108

109109
func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext) ast.Node {
110+
tokenStream := n.GetParser().GetTokenStream().(*antlr.CommonTokenStream)
111+
110112
stmt := &ast.CreateTableStmt{
111113
Name: parseTableName(n),
112114
IfNotExists: n.EXISTS_() != nil,
115+
Comment: comment(tokenStream, n, false),
113116
}
117+
114118
for _, idef := range n.AllColumn_def() {
115119
if def, ok := idef.(*parser.Column_defContext); ok {
116120
typeName := "any"
117121
if def.Type_name() != nil {
118122
typeName = def.Type_name().GetText()
119123
}
124+
120125
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
121126
Colname: identifier(def.Column_name().GetText()),
122127
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
123128
TypeName: &ast.TypeName{Name: typeName},
129+
Comment: comment(tokenStream, def, true),
124130
})
131+
125132
}
126133
}
127134
return stmt
128135
}
129136

137+
// comment returns the comment associated with the given context. The parameter right indicates whether the comment is
138+
// to the right or to the left of the context.
139+
func comment(tokenStream *antlr.CommonTokenStream, ctx antlr.ParserRuleContext, right bool) string {
140+
var (
141+
hiddenTokens []antlr.Token
142+
comment string
143+
)
144+
145+
if right {
146+
hiddenTokens = tokenStream.GetHiddenTokensToRight(ctx.GetStop().GetTokenIndex()+1, antlr.TokenHiddenChannel)
147+
} else {
148+
hiddenTokens = tokenStream.GetHiddenTokensToLeft(ctx.GetStart().GetTokenIndex(), antlr.TokenHiddenChannel)
149+
}
150+
151+
for _, token := range hiddenTokens {
152+
// Filter for single-line comments
153+
if token.GetTokenType() == parser.SQLiteLexerSINGLE_LINE_COMMENT {
154+
// Remove "--" and leading/trailing whitespaces
155+
comment = strings.TrimSpace(strings.TrimPrefix(token.GetText(), "--"))
156+
return comment
157+
}
158+
}
159+
160+
return ""
161+
}
162+
130163
func (c *cc) convertCreate_virtual_table_stmtContext(n *parser.Create_virtual_table_stmtContext) ast.Node {
131164
switch moduleName := n.Module_name().GetText(); moduleName {
132165
case "fts5":

internal/engine/sqlite/parse.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
5050
pp.AddErrorListener(el)
5151
// pp.BuildParseTrees = true
5252
tree := pp.Parse()
53+
5354
if el.err != "" {
5455
return nil, errors.New(el.err)
5556
}
@@ -83,6 +84,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
8384
loc = stmt.GetStop().GetStop() + 2
8485
}
8586
}
87+
8688
return stmts, nil
8789
}
8890

0 commit comments

Comments
 (0)