From 3db7d1995699f168e3533f43bfe409323e871d21 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Thu, 19 Jun 2003 19:42:58 +0000 Subject: [PATCH] innodb and PMA_getForeigners --- ChangeLog | 3 ++ libraries/display_tbl.lib.php3 | 44 +++++++++----------- libraries/relation.lib.php3 | 73 ++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index eca801ae9..5b0736a81 100755 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,9 @@ $Source$ 2003-06-19 Marc Delisle * libraries/sqlparser.lib.php3: analyzer for FOREIGN KEYS * libraries/sqlparser.lib.php3: bug 756861, DEFAULT 0 becomes DEFAULT0 + * libraries/display_tbl.lib.php3: use PMA_getForeigners() + * libraries/relation.lib.php3: now PMA_getForeigners() gets also + foreign keys defined in innnodb 2003-06-17 Michal Cihar * config.inc.php3: Fixed typo (s/'PMA_pdf_page'/'PMA_pdf_pages'/). diff --git a/libraries/display_tbl.lib.php3 b/libraries/display_tbl.lib.php3 index e20893067..3a9c450bb 100644 --- a/libraries/display_tbl.lib.php3 +++ b/libraries/display_tbl.lib.php3 @@ -1615,34 +1615,28 @@ if (!defined('PMA_DISPLAY_TBL_LIB_INCLUDED')) { // init map $map = array(); - if ($cfgRelation['relwork']) { - // find tables - //$pattern = '`?[[:space:]]+(((ON|on)[[:space:]]+[^,]+)?,|((NATURAL|natural)[[:space:]]+)?(INNER|inner|LEFT|left|RIGHT|right)([[:space:]]+(OUTER|outer))?[[:space:]]+(JOIN|join))[[:space:]]*`?'; - //$target = eregi_replace('^.*[[:space:]]+FROM[[:space:]]+`?|`?[[:space:]]*(ON[[:space:]]+[^,]+)?(WHERE[[:space:]]+.*)?$', '', $sql_query); - //$target = eregi_replace('`?[[:space:]]ORDER BY[[:space:]](.*)','',$target); - //$tabs = '(\'' . join('\',\'', split($pattern, $target)) . '\')'; - $target=array(); - reset($analyzed_sql[0]['table_ref']); - while (list ($table_ref_position, $table_ref) = each ($analyzed_sql[0]['table_ref'])) { - $target[] = $analyzed_sql[0]['table_ref'][$table_ref_position]['table_true_name']; - } - $tabs = '(\'' . join('\',\'', $target) . '\')'; + // find tables - $local_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field' - . ' FROM ' . PMA_backquote($cfgRelation['relation']) - . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\'' - . ' AND master_table IN ' . $tabs; - $result = @PMA_query_as_cu($local_query, FALSE); - if ($result) { - while ($rel = PMA_mysql_fetch_row($result)) { - // check for display field? - if ($cfgRelation['displaywork']) { - $display_field = PMA_getDisplayField($rel[1], $rel[2]); - $map[$rel[0]] = array($rel[2], $rel[3], $display_field, $rel[1]); - } // end if + $target=array(); + reset($analyzed_sql[0]['table_ref']); + while (list ($table_ref_position, $table_ref) = each ($analyzed_sql[0]['table_ref'])) { + $target[] = $analyzed_sql[0]['table_ref'][$table_ref_position]['table_true_name']; + } + $tabs = '(\'' . join('\',\'', $target) . '\')'; + + if ($cfgRelation['displaywork']) { + $exist_rel = PMA_getForeigners($db, $table, '', 'both'); + if ($exist_rel) { + while (list($master_field,$rel) = each($exist_rel)) { + $display_field = PMA_getDisplayField($rel['foreign_db'],$rel['foreign_table']); + $map[$master_field] = array($rel['foreign_table'], + $rel['foreign_field'], + $display_field, + $rel['foreign_db']); } // end while } // end if - } // end 2b + } // end if + // end 2b // 3. ----- Displays the results table ----- echo '' . "\n" diff --git a/libraries/relation.lib.php3 b/libraries/relation.lib.php3 index 433e389cd..2904c12fc 100644 --- a/libraries/relation.lib.php3 +++ b/libraries/relation.lib.php3 @@ -272,6 +272,7 @@ if (!defined('PMA_RELATION_LIB_INCLUDED')){ * @param string the name of the db to check for * @param string the name of the table to check for * @param string the name of the column to check for + * @param string the source for foreign key information * * @return array db,table,column * @@ -280,33 +281,61 @@ if (!defined('PMA_RELATION_LIB_INCLUDED')){ * * @access public * - * @author Mike Beck + * @author Mike Beck and Marc Delisle */ - function PMA_getForeigners($db, $table, $column = '') { + function PMA_getForeigners($db, $table, $column = '', $source = 'both') { global $cfgRelation, $err_url_0; - $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field' - . ' FROM ' . PMA_backquote($cfgRelation['relation']) - . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' ' - . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' '; - if (!empty($column)) { - $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\''; + if ($source == 'both' || $source == 'internal') { + $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field' + . ' FROM ' . PMA_backquote($cfgRelation['relation']) + . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' ' + . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' '; + if (!empty($column)) { + $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\''; + } + $relations = PMA_query_as_cu($rel_query); + $i = 0; + while ($relrow = @PMA_mysql_fetch_array($relations)) { + $field = $relrow['master_field']; + $foreign[$field]['foreign_db'] = $relrow['foreign_db']; + $foreign[$field]['foreign_table'] = $relrow['foreign_table']; + $foreign[$field]['foreign_field'] = $relrow['foreign_field']; + $i++; + } // end while } - $relations = PMA_query_as_cu($rel_query); - $i = 0; - while ($relrow = @PMA_mysql_fetch_array($relations)) { - $field = $relrow['master_field']; - $foreign[$field]['foreign_db'] = $relrow['foreign_db']; - $foreign[$field]['foreign_table'] = $relrow['foreign_table']; - $foreign[$field]['foreign_field'] = $relrow['foreign_field']; - $i++; - } // end while - if (isset($foreign) && is_array($foreign)) { - return $foreign; - } else { - return FALSE; - } + if ($source == 'both' || $source == 'innodb') { + $show_create_table_query = 'SHOW CREATE TABLE ' + . PMA_backquote($db) . '.' . PMA_backquote($table); + $show_create_table_res = PMA_mysql_query($show_create_table_query); + list(,$show_create_table) = PMA_mysql_fetch_row($show_create_table_res); + $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table)); + + while (list(,$one_key) = each ($analyzed_sql[0]['foreign_keys'])) { + + // TODO: the analyzer may return more than one column name in the + // index list or the ref_index_list but for now we take the first + $field = $one_key['index_list'][0]; + + // TODO: SHOW CREATE TABLE does not return the db name in + // the REFERENCES, so we assume the same db as master + + // If a foreign key is defined in the 'internal' source (pmadb) + // and in 'innodb', we won't get it twice if $source='both' + // because we use $field as key + + $foreign[$field]['foreign_db'] = $db; + $foreign[$field]['foreign_table'] = $one_key['ref_table_name']; + $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][0]; + } + } + + if (isset($foreign) && is_array($foreign)) { + return $foreign; + } else { + return FALSE; + } } // end of the 'PMA_getForeigners()' function