Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Take only comment inside CREATE query
  • Loading branch information
oxisto committed Dec 27, 2024
commit 847fd6cc3c9d024012a9aa9ed2125bbc3e531825
15 changes: 5 additions & 10 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext)
stmt := &ast.CreateTableStmt{
Name: parseTableName(n),
IfNotExists: n.EXISTS_() != nil,
Comment: comment(tokenStream, n, false),
Comment: comment(tokenStream, n.OPEN_PAR().GetSymbol()),
}

for _, idef := range n.AllColumn_def() {
Expand All @@ -126,27 +126,22 @@ func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext)
Colname: identifier(def.Column_name().GetText()),
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
TypeName: &ast.TypeName{Name: typeName},
Comment: comment(tokenStream, def, true),
Comment: comment(tokenStream, def.GetStop()),
})

}
}
return stmt
}

// comment returns the comment associated with the given context. The parameter right indicates whether the comment is
// to the right or to the left of the context.
func comment(tokenStream *antlr.CommonTokenStream, ctx antlr.ParserRuleContext, right bool) string {
// comment returns the comment associated with the given context.
func comment(tokenStream *antlr.CommonTokenStream, from antlr.Token) string {
var (
hiddenTokens []antlr.Token
comment string
)

if right {
hiddenTokens = tokenStream.GetHiddenTokensToRight(ctx.GetStop().GetTokenIndex()+1, antlr.TokenHiddenChannel)
} else {
hiddenTokens = tokenStream.GetHiddenTokensToLeft(ctx.GetStart().GetTokenIndex(), antlr.TokenHiddenChannel)
}
hiddenTokens = tokenStream.GetHiddenTokensToRight(from.GetTokenIndex()+1, antlr.TokenHiddenChannel)

for _, token := range hiddenTokens {
// Filter for single-line comments
Expand Down
2 changes: 0 additions & 2 deletions internal/engine/sqlite/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
pp.AddErrorListener(el)
// pp.BuildParseTrees = true
tree := pp.Parse()

if el.err != "" {
return nil, errors.New(el.err)
}
Expand Down Expand Up @@ -84,7 +83,6 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
loc = stmt.GetStop().GetStop() + 2
}
}

return stmts, nil
}

Expand Down
Loading