diff options
author | ales-bulko <44088923+ales-bulko@users.noreply.github.com> | 2018-11-27 01:34:09 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauriciofauth@gmail.com> | 2018-11-27 01:34:09 +0300 |
commit | 23ce7cec521f830736cb6ee177ec4c3d03516df2 (patch) | |
tree | ef795e9627a17aa75ba244041d21f796a8cc0089 /libraries | |
parent | 416bf940736efa918241c31554d14a4bcd9bf1ed (diff) |
Fixes #14658 - Regression on multi table query (#14681)
Added functionality to generate SELECT query with JOINs of requested tables. It was mentioned that it's just a bug and it was working in previous versions, but I did not see any code fragments that would make it possible. Please, feel free to suggest any better solution if you don't like that one.
Fixes #14658
Signed-off-by: Aleš Buľko <ales.bulko@student.tuke.sk>
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/classes/DatabaseInterface.php | 37 | ||||
-rw-r--r-- | libraries/classes/Dbi/DbiDummy.php | 12 |
2 files changed, 49 insertions, 0 deletions
diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 41305971a2..8dd64c9ba0 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -390,6 +390,43 @@ class DatabaseInterface return $tables; } + + /** + * returns + * + * @param string $database name of database + * @param array $tables list of tables to search for for relations + * @param int $link mysql link resource|object + * + * @return array array of found foreign keys + */ + public function getForeignKeyConstrains(string $database, array $tables, $link = DatabaseInterface::CONNECT_USER): array + { + $tablesListForQuery = ''; + foreach($tables as $table){ + $tablesListForQuery .= "'" . $this->escapeString($table) . "',"; + } + $tablesListForQuery = rtrim($tablesListForQuery, ','); + + $foreignKeyConstrains = $this->fetchResult( + "SELECT" + . " TABLE_NAME," + . " COLUMN_NAME," + . " REFERENCED_TABLE_NAME," + . " REFERENCED_COLUMN_NAME" + . " FROM information_schema.key_column_usage" + . " WHERE referenced_table_name IS NOT NULL" + . " AND TABLE_SCHEMA = '" . $this->escapeString($database) . "'" + . " AND TABLE_NAME IN (" . $tablesListForQuery . ")" + . " AND REFERENCED_TABLE_NAME IN (" . $tablesListForQuery . ");", + null, + null, + $link, + self::QUERY_STORE + ); + return $foreignKeyConstrains; + } + /** * returns a segment of the SQL WHERE clause regarding table name and type * diff --git a/libraries/classes/Dbi/DbiDummy.php b/libraries/classes/Dbi/DbiDummy.php index b36c680dc1..446efbf91d 100644 --- a/libraries/classes/Dbi/DbiDummy.php +++ b/libraries/classes/Dbi/DbiDummy.php @@ -1481,6 +1481,18 @@ class DbiDummy implements DbiExtension 'query' => "SELECT *, `COLUMN_NAME` AS `Field`, `COLUMN_TYPE` AS `Type`, `COLLATION_NAME` AS `Collation`, `IS_NULLABLE` AS `Null`, `COLUMN_KEY` AS `Key`, `COLUMN_DEFAULT` AS `Default`, `EXTRA` AS `Extra`, `PRIVILEGES` AS `Privileges`, `COLUMN_COMMENT` AS `Comment` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'information_schema' AND `TABLE_NAME` = 'PMA'", 'result' => [], ], + [ + 'query' => "SELECT TABLE_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.key_column_usage WHERE referenced_table_name IS NOT NULL AND TABLE_SCHEMA = 'test' AND TABLE_NAME IN ('table1','table2') AND REFERENCED_TABLE_NAME IN ('table1','table2');", + 'result' => [ + [ + 'TABLE_NAME' => 'table2', + 'COLUMN_NAME' => 'idtable2', + 'REFERENCED_TABLE_NAME' => 'table1', + 'REFERENCED_COLUMN_NAME' => 'idtable1', + ] + ], + ], + ]; /** * Current database. |