提交 1d09ee21 编写于 作者: martianzhang's avatar martianzhang

🐛 fix commna separate join tables info

  select * from t1, t2 limit 1
上级 ac82e632
...@@ -6,6 +6,8 @@ syntax error case ...@@ -6,6 +6,8 @@ syntax error case
[] []
select * from ta join tb using (id) select * from ta join tb using (id)
[`sakila`.`ta` `sakila`.`tb`] [`sakila`.`ta` `sakila`.`tb`]
select * from ta, tb limit 1
[`sakila`.`ta` `sakila`.`tb`]
SELECT * FROM film WHERE length = 86; SELECT * FROM film WHERE length = 86;
[`sakila`.`film`] [`sakila`.`film`]
SELECT * FROM film WHERE length IS NULL; SELECT * FROM film WHERE length IS NULL;
...@@ -117,9 +119,9 @@ DELETE FROM film WHERE length > 100; ...@@ -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; 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`] [`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; 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; 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; UPDATE film SET length = 10 WHERE language_id = 20;
[`sakila`.`film`] [`sakila`.`film`]
INSERT INTO city (country_id) SELECT country_id FROM country; INSERT INTO city (country_id) SELECT country_id FROM country;
......
...@@ -86,32 +86,20 @@ func SchemaMetaInfo(sql string, defaultDatabase string) []string { ...@@ -86,32 +86,20 @@ func SchemaMetaInfo(sql string, defaultDatabase string) []string {
tables = append(tables, fmt.Sprintf("`%s`.`dual`", n.DBName)) tables = append(tables, fmt.Sprintf("`%s`.`dual`", n.DBName))
case *ast.InsertStmt, *ast.SelectStmt, *ast.UnionStmt, *ast.UpdateStmt, *ast.DeleteStmt: case *ast.InsertStmt, *ast.SelectStmt, *ast.UnionStmt, *ast.UpdateStmt, *ast.DeleteStmt:
// DML/DQL: INSERT, SELECT, UPDATE, DELETE // DML/DQL: INSERT, SELECT, UPDATE, DELETE
tableRefs := common.JSONFind(jsonString, "TableRefs") for _, tableRef := range common.JSONFind(jsonString, "TableRefs") {
for _, table := range tableRefs { for _, source := range common.JSONFind(tableRef, "Source") {
leftDatabase := gjson.Get(table, "Left.Source.Schema.O") database := gjson.Get(source, "Schema.O")
leftTable := gjson.Get(table, "Left.Source.Name.O") table := gjson.Get(source, "Name.O")
if leftDatabase.String() == "" { if database.String() == "" {
if leftTable.String() != "" { if table.String() != "" {
tables = append(tables, fmt.Sprintf("`%s`.`%s`", defaultDatabase, leftTable)) tables = append(tables, fmt.Sprintf("`%s`.`%s`", defaultDatabase, table))
} }
} 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))
} else { } 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))
}
} }
} }
} }
......
...@@ -64,6 +64,7 @@ func TestSchemaMetaInfo(t *testing.T) { ...@@ -64,6 +64,7 @@ func TestSchemaMetaInfo(t *testing.T) {
"select 1;", "select 1;",
"syntax error case", "syntax error case",
"select * from ta join tb using (id)", "select * from ta join tb using (id)",
"select * from ta, tb limit 1",
} }
err := common.GoldenDiff(func() { err := common.GoldenDiff(func() {
for _, sql := range append(sqls, common.TestSQLs...) { for _, sql := range append(sqls, common.TestSQLs...) {
......
...@@ -204,7 +204,9 @@ ...@@ -204,7 +204,9 @@
"`hello`.`t`" "`hello`.`t`"
], ],
"C15BDF2C73B5B7ED": [ "C15BDF2C73B5B7ED": [
"`unknown`.`address`" "`unknown`.`address`",
"`unknown`.`city`",
"`unknown`.`country`"
], ],
"C315BC4EE0F4E523": [ "C315BC4EE0F4E523": [
"`unknown`.inventory`" "`unknown`.inventory`"
...@@ -262,6 +264,7 @@ ...@@ -262,6 +264,7 @@
"`unknown`.`country`" "`unknown`.`country`"
], ],
"FCD1ABF36F8CDAD7": [ "FCD1ABF36F8CDAD7": [
"`unknown`.`city`",
"`unknown`.`country`" "`unknown`.`country`"
], ],
"FE409EB794EE91CF": [ "FE409EB794EE91CF": [
...@@ -474,7 +477,9 @@ ...@@ -474,7 +477,9 @@
"`hello`.`t`" "`hello`.`t`"
], ],
"C15BDF2C73B5B7ED": [ "C15BDF2C73B5B7ED": [
"`sakila`.`address`" "`sakila`.`address`",
"`sakila`.`city`",
"`sakila`.`country`"
], ],
"C315BC4EE0F4E523": [ "C315BC4EE0F4E523": [
"`sakila`.inventory`" "`sakila`.inventory`"
...@@ -532,6 +537,7 @@ ...@@ -532,6 +537,7 @@
"`sakila`.`country`" "`sakila`.`country`"
], ],
"FCD1ABF36F8CDAD7": [ "FCD1ABF36F8CDAD7": [
"`sakila`.`city`",
"`sakila`.`country`" "`sakila`.`country`"
], ],
"FE409EB794EE91CF": [ "FE409EB794EE91CF": [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册