Skip to content

Commit

Permalink
Merge pull request #15 from knocknote/feature/add_show_parser
Browse files Browse the repository at this point in the history
add show statement parser
  • Loading branch information
goccy authored Jul 13, 2019
2 parents f2c6b32 + 049139c commit b2331f5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/juju/errors v0.0.0-20170703010042-c7d06af17c68 // indirect
github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
github.com/juju/testing v0.0.0-20190415054131-a282c42ba059 // indirect
github.com/knocknote/vitess-sqlparser v0.0.0-20181121014348-1003c43917a3
github.com/knocknote/vitess-sqlparser v0.0.0-20190712090058-385243f72d33
github.com/mattn/go-colorable v0.0.0-20160220075935-9cbef7c35391 // indirect
github.com/mattn/go-isatty v0.0.0-20151211000621-56b76bdf51f7 // indirect
github.com/mattn/go-sqlite3 v0.0.0-20170407154627-cf7286f069c3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/juju/testing v0.0.0-20190415054131-a282c42ba059 h1:vsqkD58dFysYgUZ8XH
github.com/juju/testing v0.0.0-20190415054131-a282c42ba059/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
github.com/knocknote/vitess-sqlparser v0.0.0-20181121014348-1003c43917a3 h1:Nk/bqRokAz+B4j+18CKyAYhkf/scbbY7anWdSZFHjqI=
github.com/knocknote/vitess-sqlparser v0.0.0-20181121014348-1003c43917a3/go.mod h1:bF2oGXw2Ex/jIPGFPaFzEf8BtNRSBWc81ni+N9UaW5Q=
github.com/knocknote/vitess-sqlparser v0.0.0-20190712090058-385243f72d33 h1:3//yyn1X4pIytNgAA3a1a7CQJm2w3WNVNCGLBQrPcPA=
github.com/knocknote/vitess-sqlparser v0.0.0-20190712090058-385243f72d33/go.mod h1:bF2oGXw2Ex/jIPGFPaFzEf8BtNRSBWc81ni+N9UaW5Q=
github.com/mattn/go-colorable v0.0.0-20160220075935-9cbef7c35391 h1:x4vT4RoTH2BNkPx0LgrBKeFuPQPviK1aSAIWG6ruc+Y=
github.com/mattn/go-colorable v0.0.0-20160220075935-9cbef7c35391/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.0-20151211000621-56b76bdf51f7 h1:owMyzMR4QR+jSdlfkX9jPU3rsby4++j99BfbtgVr6ZY=
Expand Down
3 changes: 3 additions & 0 deletions sqlparser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
CreateTable
// TruncateTable 'TRUNCATE TABLE' query type
TruncateTable
// Show 'SHOW' query type
Show

)

func (t QueryType) IsWriteQuery() bool {
Expand Down
12 changes: 12 additions & 0 deletions sqlparser/sqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ func (p *Parser) parseDDLStmt(stmt *vtparser.DDL, queryBase *QueryBase) (Query,
return queryBase, nil
}

func (p *Parser) parseShowStmt(stmt *vtparser.Show, queryBase *QueryBase) (Query, error) {
queryBase.Type = Show
queryBase.TableName = stmt.TableName
return queryBase, nil
}

func (p *Parser) formatQuery(query string) string {
formattedQuery := replaceDoubleQuote.ReplaceAllString(query, "`")
formattedQuery = removeSemiColon.ReplaceAllString(formattedQuery, "")
Expand Down Expand Up @@ -445,6 +451,12 @@ func (p *Parser) Parse(queryText string, args ...interface{}) (Query, error) {
return nil, errors.WithStack(err)
}
return query, nil
case *vtparser.Show:
query, err := p.parseShowStmt(stmt, queryBase)
if err != nil {
return nil, errors.WithStack(err)
}
return query, nil
default:
}
return nil, errors.Errorf("unsupported query type %s", reflect.TypeOf(ast))
Expand Down
18 changes: 17 additions & 1 deletion sqlparser/sqlparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func TestDDL(t *testing.T) {
})
}

func TestSHOW(t *testing.T) {
parser, err := New()
checkErr(t, err)
t.Run("show create table", func(t *testing.T) {
query, err := parser.Parse("show create table users")
fmt.Printf("show.table:%v\n", query.Table())
checkErr(t, err)
if query.QueryType() != Show {
t.Fatal("cannot parse 'show' query")
}
if query.Table() != "users" {
t.Fatal("cannot parse 'show' query")
}
})
}

func validateSelectQuery(t *testing.T, query Query) {
if query.QueryType() != Select {
t.Fatal("cannot parse 'select' query")
Expand Down Expand Up @@ -542,7 +558,7 @@ func TestERROR(t *testing.T) {
log.Println(err)
})
t.Run("unsupport query", func(t *testing.T) {
query, err := parser.Parse("show create table users")
query, err := parser.Parse("show slave status")
if query != nil {
t.Fatal("invalid query value")
}
Expand Down

0 comments on commit b2331f5

Please sign in to comment.