github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/sqlparser/sqlparser_test.go (about) 1 package sqlparser 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func checkErr(t *testing.T, err error) { 9 if err != nil { 10 t.Fatalf("%+v", err) 11 } 12 } 13 14 func checkEqual(t *testing.T, src interface{}, dst interface{}) { 15 if !reflect.DeepEqual(src, dst) { 16 t.Fatalf("not equal %v and %v", src, dst) 17 } 18 } 19 20 func TestDropTableParsing(t *testing.T) { 21 _, err := Parse(`DROP table if exists users`) 22 checkErr(t, err) 23 } 24 25 func TestSelectParsing(t *testing.T) { 26 _, err := Parse(`SELECT * from users where id = 1 order by created_at limit 1 offset 3`) 27 checkErr(t, err) 28 } 29 30 func TestCreateTableParsing(t *testing.T) { 31 _, err := Parse(` 32 CREATE TABLE users ( 33 id bigint(20) unsigned NOT NULL, 34 other_id bigint(20) unsigned NOT NULL, 35 enum_column enum('a','b','c','d') DEFAULT NULL, 36 int_column int(10) DEFAULT '0', 37 PRIMARY KEY (id) 38 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 39 `) 40 checkErr(t, err) 41 } 42 43 func TestCreateTableWithPartition(t *testing.T) { 44 _, err := Parse(` 45 CREATE TABLE histories ( 46 id bigint(20) unsigned NOT NULL AUTO_INCREMENT, 47 user_id bigint(20) unsigned NOT NULL, 48 note text NOT NULL, 49 created_at datetime NOT NULL, 50 updated_at datetime NOT NULL, 51 PRIMARY KEY (id,created_at) 52 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 53 /*!50500 PARTITION BY RANGE COLUMNS(created_at) 54 (PARTITION p201812 VALUES LESS THAN ('2019-01-01') ENGINE = InnoDB, 55 PARTITION p201901 VALUES LESS THAN ('2019-02-01') ENGINE = InnoDB, 56 PARTITION p201902 VALUES LESS THAN ('2019-03-01') ENGINE = InnoDB, 57 PARTITION p201903 VALUES LESS THAN ('2019-04-01') ENGINE = InnoDB) */; 58 `) 59 checkErr(t, err) 60 } 61 62 func TestShowCreateTableParsing(t *testing.T) { 63 ast, err := Parse(`SHOW CREATE TABLE users`) 64 checkErr(t, err) 65 switch stmt := ast.(type) { 66 case *Show: 67 checkEqual(t, "users", stmt.TableName) 68 default: 69 t.Fatalf("%+v", "type mismatch") 70 } 71 }