Simplify foreign key discovery

This commit is contained in:
Jakub Vrana 2011-04-08 00:50:33 -06:00
parent 3ebfdd30c1
commit 91aae7332f

View file

@ -133,20 +133,16 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
function rowDescriptions($rows, $foreignKeys) { function rowDescriptions($rows, $foreignKeys) {
$return = $rows; $return = $rows;
foreach ($rows[0] as $key => $val) { foreach ($rows[0] as $key => $val) {
foreach ((array) $foreignKeys[$key] as $foreignKey) { if (list($table, $id, $name) = $this->_foreignColumn($foreignKeys, $key)) {
if (count($foreignKey["source"]) == 1) {
$id = idf_escape($foreignKey["target"][0]);
$name = $this->rowDescription($foreignKey["table"]);
if ($name != "") {
// find all used ids // find all used ids
$ids = array(); $ids = array();
foreach ($rows as $row) { foreach ($rows as $row) {
$ids[$row[$key]] = exact_value($row[$key]); $ids[$row[$key]] = exact_value($row[$key]);
} }
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
$descriptions = $this->_values[$foreignKey["table"]]; $descriptions = $this->_values[$table];
if (!$descriptions) { if (!$descriptions) {
$descriptions = get_key_vals("SELECT $id, $name FROM " . table($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")"); $descriptions = get_key_vals("SELECT $id, $name FROM " . table($table) . " WHERE $id IN (" . implode(", ", $ids) . ")");
} }
// use the descriptions // use the descriptions
foreach ($rows as $n => $row) { foreach ($rows as $n => $row) {
@ -154,9 +150,6 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
$return[$n][$key] = (string) $descriptions[$row[$key]]; $return[$n][$key] = (string) $descriptions[$row[$key]];
} }
} }
break;
}
}
} }
} }
return $return; return $return;
@ -556,23 +549,28 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
} }
} }
function _foreignKeyOptions($table, $column) { function _foreignColumn($foreignKeys, $column) {
$foreignKeys = column_foreign_keys($table);
foreach ((array) $foreignKeys[$column] as $foreignKey) { foreach ((array) $foreignKeys[$column] as $foreignKey) {
if (count($foreignKey["source"]) == 1) { if (count($foreignKey["source"]) == 1) {
$id = idf_escape($foreignKey["target"][0]);
$name = $this->rowDescription($foreignKey["table"]); $name = $this->rowDescription($foreignKey["table"]);
if ($name != "") { if ($name != "") {
$return = &$this->_values[$foreignKey["table"]]; $id = idf_escape($foreignKey["target"][0]);
return array($foreignKey["table"], $id, $name);
}
}
}
}
function _foreignKeyOptions($table, $column) {
if (list($table, $id, $name) = $this->_foreignColumn(column_foreign_keys($table), $column)) {
$return = &$this->_values[$table];
if (!isset($return)) { if (!isset($return)) {
$table_status = table_status($foreignKey["table"]); $table_status = table_status($table);
$return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($foreignKey["table"]) . " ORDER BY 2")); $return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($table) . " ORDER BY 2"));
} }
return $return; return $return;
} }
} }
}
}
} }