diff --git a/ast/testdata/TestSchemaMetaInfo.golden b/ast/testdata/TestSchemaMetaInfo.golden index 7b6e6cdc59694442e9242473bc3d1425d87b8e2e..fd3822673f54c1f94df2ff030e6013927a7ad223 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 26722cfc859b076689551cb1e1fac7a152d032d0..d6085704a610fd67636d26d080c525876e1b431c 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 93a5b25596f8c6de9d65f64323bd1bbe1653b0d1..85892ad04449b2aa6b796da3de4ff07113de8e24 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 98be1f62ee62068dba5a1b13cb17634765de775d..c39cd8682f6d72398d7e5e677d13f714ec05d980 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": [