Description:
The output of getTableName may vary depending on the query and data. It may either return the table name or null/empty.
How to repeat:
Table structure and data:
CREATE TABLE test1(
a int not null auto_increment primary key,
b int not null,
c int not null
);
CREATE TABLE test2(
d int not null auto_increment primary key,
e int not null,
f int not null
);
CREATE TABLE test3(
g int not null auto_increment primary key,
h int not null,
i int not null
);
INSERT INTO test1 VALUES (1,1,1);
INSERT INTO test2 VALUES (1,1,1);
INSERT INTO test3 VALUES (1,1,1);
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class PerconaTest {
public static void main(String[] args) throws Exception {
String password = "msandbox";
String jdbc = "jdbc:mysql://127.0.0.1:5740/test?useSSL=false&user=root&password=" + password;
Connection conn = DriverManager.getConnection(jdbc);
test(conn, "SELECT test2.*, test1.b, CONCAT(test1.c), GROUP_CONCAT(DISTINCT(test3.g)) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d ORDER BY test3.h LIMIT 1");
test(conn, "SELECT test2.*, test1.b, CONCAT(test1.c) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d ORDER BY test3.h LIMIT 1");
test(conn, "SELECT test2.*, test1.b, CONCAT(test1.c), GROUP_CONCAT(DISTINCT(test3.g)) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d");
}
private static void test(Connection conn, String query) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
System.out.println("Query: " + query);
System.out.println(" All column metadata getTableName; getColumnName");
for (int i = 1; i <= md.getColumnCount(); i++) {
System.out.println("Table Name: " + md.getTableName(i));
System.out.println(" Col " + i + ": " + md.getTableName(i) + "." + md.getColumnName(i));
}
stmt.close();
}
}
Output:
Query: SELECT test2.*, test1.b, CONCAT(test1.c), GROUP_CONCAT(DISTINCT(test3.g)) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d ORDER BY test3.h LIMIT 1
All column metadata getTableName; getColumnName
Table Name:
Col 1: .d
Table Name:
Col 2: .e
Table Name:
Col 3: .f
Table Name:
Col 4: .b
Table Name:
Col 5: .CONCAT(test1.c)
Table Name:
Col 6: .GROUP_CONCAT(DISTINCT(test3.g))
Query: SELECT test2.*, test1.b, CONCAT(test1.c) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d ORDER BY test3.h LIMIT 1
All column metadata getTableName; getColumnName
Table Name: test2
Col 1: test2.d
Table Name: test2
Col 2: test2.e
Table Name: test2
Col 3: test2.f
Table Name: test1
Col 4: test1.b
Table Name:
Col 5: .CONCAT(test1.c)
Query: SELECT test2.*, test1.b, CONCAT(test1.c), GROUP_CONCAT(DISTINCT(test3.g)) FROM test1 JOIN test2 ON (test1.b=test2.d AND test1.c = test2.e) JOIN test3 ON (test1.b=test3.h AND test1.c=test3.i) GROUP BY test2.d
All column metadata getTableName; getColumnName
Table Name: test2
Col 1: test2.d
Table Name: test2
Col 2: test2.e
Table Name: test2
Col 3: test2.f
Table Name: test1
Col 4: test1.b
Table Name:
Col 5: .CONCAT(test1.c)
Table Name:
Col 6: .GROUP_CONCAT(DISTINCT(test3.g))
Suggested fix:
It should show every table name.