| 
 | 1 | +package main  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"bytes"  | 
 | 5 | +	"github.com/actiontech/dtle/helper/u"  | 
 | 6 | +	tparser "github.com/pingcap/parser"  | 
 | 7 | +	tast "github.com/pingcap/parser/ast"  | 
 | 8 | +	"github.com/pingcap/parser/format"  | 
 | 9 | + | 
 | 10 | +	"fmt"  | 
 | 11 | +	_ "github.com/pingcap/tidb/types/parser_driver"  | 
 | 12 | +	"log"  | 
 | 13 | +)  | 
 | 14 | + | 
 | 15 | +func main() {  | 
 | 16 | +	fmt.Printf("")  | 
 | 17 | +	log.Println("hello")  | 
 | 18 | + | 
 | 19 | +	p := tparser.New()  | 
 | 20 | +	sql := `CREATE TABLE t7 (  | 
 | 21 | +id int(11) DEFAULT NULL,  | 
 | 22 | +name varchar(35) DEFAULT NULL  | 
 | 23 | +) ENGINE=InnoDB DEFAULT CHARSET=latin1  | 
 | 24 | +/*!50100 PARTITION BY LIST (id)  | 
 | 25 | +(PARTITION r0 VALUES IN (1,5,9,13,17,21) ENGINE = InnoDB,  | 
 | 26 | + PARTITION r1 VALUES IN (2,6,10,14,18,22) ENGINE = InnoDB,  | 
 | 27 | + PARTITION r2 VALUES IN (3,7,11,15,19,23) ENGINE = InnoDB,  | 
 | 28 | + PARTITION r3 VALUES IN (4,8,12,16,20,24) ENGINE = InnoDB) */`  | 
 | 29 | +	ast, err := p.ParseOneStmt(sql, "", "")  | 
 | 30 | + | 
 | 31 | +	u.PanicIfErr(err)  | 
 | 32 | + | 
 | 33 | +	buf := bytes.NewBuffer(nil)  | 
 | 34 | +	rCtx := &format.RestoreCtx{  | 
 | 35 | +		//Flags:     format.RestoreKeyWordUppercase | format.RestoreNameBackQuotes | format.RestoreSpacesAroundBinaryOperation,  | 
 | 36 | +		Flags:     format.RestoreKeyWordUppercase | format.RestoreSpacesAroundBinaryOperation,  | 
 | 37 | +		In:        buf,  | 
 | 38 | +		JoinLevel: 0,  | 
 | 39 | +	}  | 
 | 40 | +	switch v := ast.(type) {  | 
 | 41 | +	case *tast.CreateTableStmt:  | 
 | 42 | +		v.Table.Schema.O = "hello"  | 
 | 43 | +	case *tast.DropTableStmt:  | 
 | 44 | +		for i := range v.Tables {  | 
 | 45 | +			if v.Tables[i].Schema.O == "b" {  | 
 | 46 | +				v.Tables[i].Schema.O = "hello"  | 
 | 47 | +			}  | 
 | 48 | +		}  | 
 | 49 | +	case *tast.RenameTableStmt:  | 
 | 50 | +		println(v.OldTable.Name.O)  | 
 | 51 | +		println(v.NewTable.Name.O)  | 
 | 52 | +		println("---")  | 
 | 53 | +		for i := range v.TableToTables {  | 
 | 54 | +			println(v.TableToTables[i].OldTable.Name.O)  | 
 | 55 | +			println(v.TableToTables[i].NewTable.Name.O)  | 
 | 56 | +		}  | 
 | 57 | +	case *tast.GrantStmt:  | 
 | 58 | +		println("is grant")  | 
 | 59 | +	default:  | 
 | 60 | +		println("unknown ast")  | 
 | 61 | +	}  | 
 | 62 | + | 
 | 63 | +	err = ast.Restore(rCtx)  | 
 | 64 | +	u.PanicIfErr(err)  | 
 | 65 | +	fmt.Printf("after %v\n", buf.String())  | 
 | 66 | +}  | 
0 commit comments