From 1d09ee21babaf0260a518a0ebb10fdbe30e7d093 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 23 Jan 2019 16:33:16 +0800 Subject: [PATCH] :bug: fix commna separate join tables info select * from t1, t2 limit 1 --- ast/testdata/TestSchemaMetaInfo.golden | 6 ++- ast/tidb.go | 38 +++++++------------ ast/tidb_test.go | 1 + .../test_Check_get_tables_from_SQL.golden | 10 ++++- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/ast/testdata/TestSchemaMetaInfo.golden b/ast/testdata/TestSchemaMetaInfo.golden index 7b6e6cd..fd38226 100644 --- a/ast/testdata/TestSchemaMetaInfo.golden +++ b/ast/testdata/TestSchemaMetaInfo.golden @@ -6,6 +6,8 @@ syntax error case [] select * from ta join tb using (id) [`sakila`.`ta` `sakila`.`tb`] +select * from ta, tb limit 1 +[`sakila`.`ta` `sakila`.`tb`] SELECT * FROM film WHERE length = 86; [`sakila`.`film`] SELECT * FROM film WHERE length IS NULL; @@ -117,9 +119,9 @@ DELETE FROM film WHERE length > 100; UPDATE city INNER JOIN country USING(country_id) SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10; [`sakila`.`city` `sakila`.`country`] UPDATE city INNER JOIN country ON city.country_id = country.country_id INNER JOIN address ON city.city_id = address.city_id SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10; -[`sakila`.`address`] +[`sakila`.`address` `sakila`.`city` `sakila`.`country`] UPDATE city, country SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.country_id = country.country_id AND city.city_id=10; -[`sakila`.`country`] +[`sakila`.`city` `sakila`.`country`] UPDATE film SET length = 10 WHERE language_id = 20; [`sakila`.`film`] INSERT INTO city (country_id) SELECT country_id FROM country; diff --git a/ast/tidb.go b/ast/tidb.go index 26722cf..d608570 100644 --- a/ast/tidb.go +++ b/ast/tidb.go @@ -86,32 +86,20 @@ func SchemaMetaInfo(sql string, defaultDatabase string) []string { tables = append(tables, fmt.Sprintf("`%s`.`dual`", n.DBName)) case *ast.InsertStmt, *ast.SelectStmt, *ast.UnionStmt, *ast.UpdateStmt, *ast.DeleteStmt: // DML/DQL: INSERT, SELECT, UPDATE, DELETE - tableRefs := common.JSONFind(jsonString, "TableRefs") - for _, table := range tableRefs { - leftDatabase := gjson.Get(table, "Left.Source.Schema.O") - leftTable := gjson.Get(table, "Left.Source.Name.O") - if leftDatabase.String() == "" { - if leftTable.String() != "" { - tables = append(tables, fmt.Sprintf("`%s`.`%s`", defaultDatabase, leftTable)) - } - } else { - if leftTable.String() != "" { - tables = append(tables, fmt.Sprintf("`%s`.`%s`", leftDatabase, leftTable)) - } else { - tables = append(tables, fmt.Sprintf("`%s`.`dual`", leftDatabase)) - } - } - rightDatabase := gjson.Get(table, "Right.Source.Schema.O") - rightTable := gjson.Get(table, "Right.Source.Name.O") - if rightDatabase.String() == "" { - if rightTable.String() != "" { - tables = append(tables, fmt.Sprintf("`%s`.`%s`", defaultDatabase, rightTable)) - } - } else { - if rightTable.String() != "" { - tables = append(tables, fmt.Sprintf("`%s`.`%s`", rightDatabase, rightTable)) + for _, tableRef := range common.JSONFind(jsonString, "TableRefs") { + for _, source := range common.JSONFind(tableRef, "Source") { + database := gjson.Get(source, "Schema.O") + table := gjson.Get(source, "Name.O") + if database.String() == "" { + if table.String() != "" { + tables = append(tables, fmt.Sprintf("`%s`.`%s`", defaultDatabase, table)) + } } else { - tables = append(tables, fmt.Sprintf("`%s`.`dual`", rightDatabase)) + if table.String() != "" { + tables = append(tables, fmt.Sprintf("`%s`.`%s`", database, table)) + } else { + tables = append(tables, fmt.Sprintf("`%s`.`dual`", database)) + } } } } diff --git a/ast/tidb_test.go b/ast/tidb_test.go index 93a5b25..85892ad 100644 --- a/ast/tidb_test.go +++ b/ast/tidb_test.go @@ -64,6 +64,7 @@ func TestSchemaMetaInfo(t *testing.T) { "select 1;", "syntax error case", "select * from ta join tb using (id)", + "select * from ta, tb limit 1", } err := common.GoldenDiff(func() { for _, sql := range append(sqls, common.TestSQLs...) { diff --git a/test/fixture/test_Check_get_tables_from_SQL.golden b/test/fixture/test_Check_get_tables_from_SQL.golden index 98be1f6..c39cd86 100644 --- a/test/fixture/test_Check_get_tables_from_SQL.golden +++ b/test/fixture/test_Check_get_tables_from_SQL.golden @@ -204,7 +204,9 @@ "`hello`.`t`" ], "C15BDF2C73B5B7ED": [ - "`unknown`.`address`" + "`unknown`.`address`", + "`unknown`.`city`", + "`unknown`.`country`" ], "C315BC4EE0F4E523": [ "`unknown`.inventory`" @@ -262,6 +264,7 @@ "`unknown`.`country`" ], "FCD1ABF36F8CDAD7": [ + "`unknown`.`city`", "`unknown`.`country`" ], "FE409EB794EE91CF": [ @@ -474,7 +477,9 @@ "`hello`.`t`" ], "C15BDF2C73B5B7ED": [ - "`sakila`.`address`" + "`sakila`.`address`", + "`sakila`.`city`", + "`sakila`.`country`" ], "C315BC4EE0F4E523": [ "`sakila`.inventory`" @@ -532,6 +537,7 @@ "`sakila`.`country`" ], "FCD1ABF36F8CDAD7": [ + "`sakila`.`city`", "`sakila`.`country`" ], "FE409EB794EE91CF": [ -- GitLab