Skip to content

Commit

Permalink
feat(spanner/spansql): support for parsing a DML file (#6349)
Browse files Browse the repository at this point in the history
* feat(spanner/spansql): support for parsing a DML file

* fix(spanner/spansql): add comment to spansql.ParseDML

Co-authored-by: rahul2393 <rahulyadavsep92@gmail.com>
  • Loading branch information
110y and rahul2393 committed Jul 23, 2022
1 parent 0df3ed8 commit 267a9bb
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 138 deletions.
55 changes: 43 additions & 12 deletions spanner/spansql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Package spansql contains types and a parser for the Cloud Spanner SQL dialect.
To parse, use one of the Parse functions (ParseDDL, ParseDDLStmt, ParseQuery, etc.).
Sources:
https://cloud.google.com/spanner/docs/lexical
https://cloud.google.com/spanner/docs/query-syntax
https://cloud.google.com/spanner/docs/data-definition-language
Expand Down Expand Up @@ -67,37 +68,67 @@ func debugf(format string, args ...interface{}) {
// The provided filename is used for error reporting and will
// appear in the returned structure.
func ParseDDL(filename, s string) (*DDL, error) {
p := newParser(filename, s)
ddl := &DDL{}
if err := parseStatements(ddl, filename, s); err != nil {
return nil, err
}

ddl := &DDL{
Filename: filename,
return ddl, nil
}

// ParseDML parses a DML file.
//
// The provided filename is used for error reporting and will
// appear in the returned structure.
func ParseDML(filename, s string) (*DML, error) {
dml := &DML{}
if err := parseStatements(dml, filename, s); err != nil {
return nil, err
}

return dml, nil
}

func parseStatements(stmts statements, filename string, s string) error {
p := newParser(filename, s)

stmts.setFilename(filename)

for {
p.skipSpace()
if p.done {
break
}

stmt, err := p.parseDDLStmt()
if err != nil {
return nil, err
switch v := stmts.(type) {
case *DDL:
stmt, err := p.parseDDLStmt()
if err != nil {
return err
}
v.List = append(v.List, stmt)
case *DML:
stmt, err := p.parseDMLStmt()
if err != nil {
return err
}
v.List = append(v.List, stmt)
}
ddl.List = append(ddl.List, stmt)

tok := p.next()
if tok.err == eof {
break
} else if tok.err != nil {
return nil, tok.err
return tok.err
}
if tok.value == ";" {
continue
} else {
return nil, p.errorf("unexpected token %q", tok.value)
return p.errorf("unexpected token %q", tok.value)
}
}
if p.Rem() != "" {
return nil, fmt.Errorf("unexpected trailing contents %q", p.Rem())
return fmt.Errorf("unexpected trailing contents %q", p.Rem())
}

// Handle comments.
Expand Down Expand Up @@ -136,10 +167,10 @@ func ParseDDL(filename, s string) (*DDL, error) {
}
}

ddl.Comments = append(ddl.Comments, c)
stmts.addComment(c)
}

return ddl, nil
return nil
}

// ParseDDLStmt parses a single DDL statement.
Expand Down
Loading

0 comments on commit 267a9bb

Please sign in to comment.