github.com/XiaoMi/Gaea@v1.2.5/parser/parser_example_test.go (about)

     1  // Copyright 2018 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package parser_test
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/XiaoMi/Gaea/parser"
    20  	_ "github.com/XiaoMi/Gaea/parser/tidb-types/parser_driver"
    21  )
    22  
    23  // This example show how to parse a text sql into ast.
    24  func Example_parseSQL() {
    25  
    26  	// 0. make sure import parser_driver implemented by TiDB(user also can implement own driver by self).
    27  	// and add `import _ "github.com/pingcap/tidb/types/parser_driver"` in the head of file.
    28  
    29  	// 1. Create a parser. The parser is NOT goroutine safe and should
    30  	// not be shared among multiple goroutines. However, parser is also
    31  	// heavy, so each goroutine should reuse its own local instance if
    32  	// possible.
    33  	p := parser.New()
    34  
    35  	// 2. Parse a text SQL into AST([]ast.StmtNode).
    36  	stmtNodes, _, err := p.Parse("select * from tbl where id = 1", "", "")
    37  
    38  	// 3. Use AST to do cool things.
    39  	fmt.Println(stmtNodes[0], err)
    40  }