Simplify foreign key discovery
This commit is contained in:
parent
3ebfdd30c1
commit
91aae7332f
|
@ -133,28 +133,21 @@ 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) {
|
// find all used ids
|
||||||
$id = idf_escape($foreignKey["target"][0]);
|
$ids = array();
|
||||||
$name = $this->rowDescription($foreignKey["table"]);
|
foreach ($rows as $row) {
|
||||||
if ($name != "") {
|
$ids[$row[$key]] = exact_value($row[$key]);
|
||||||
// find all used ids
|
}
|
||||||
$ids = array();
|
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
|
||||||
foreach ($rows as $row) {
|
$descriptions = $this->_values[$table];
|
||||||
$ids[$row[$key]] = exact_value($row[$key]);
|
if (!$descriptions) {
|
||||||
}
|
$descriptions = get_key_vals("SELECT $id, $name FROM " . table($table) . " WHERE $id IN (" . implode(", ", $ids) . ")");
|
||||||
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
|
}
|
||||||
$descriptions = $this->_values[$foreignKey["table"]];
|
// use the descriptions
|
||||||
if (!$descriptions) {
|
foreach ($rows as $n => $row) {
|
||||||
$descriptions = get_key_vals("SELECT $id, $name FROM " . table($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")");
|
if (isset($row[$key])) {
|
||||||
}
|
$return[$n][$key] = (string) $descriptions[$row[$key]];
|
||||||
// use the descriptions
|
|
||||||
foreach ($rows as $n => $row) {
|
|
||||||
if (isset($row[$key])) {
|
|
||||||
$return[$n][$key] = (string) $descriptions[$row[$key]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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]);
|
||||||
if (!isset($return)) {
|
return array($foreignKey["table"], $id, $name);
|
||||||
$table_status = table_status($foreignKey["table"]);
|
|
||||||
$return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($foreignKey["table"]) . " ORDER BY 2"));
|
|
||||||
}
|
|
||||||
return $return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _foreignKeyOptions($table, $column) {
|
||||||
|
if (list($table, $id, $name) = $this->_foreignColumn(column_foreign_keys($table), $column)) {
|
||||||
|
$return = &$this->_values[$table];
|
||||||
|
if (!isset($return)) {
|
||||||
|
$table_status = table_status($table);
|
||||||
|
$return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($table) . " ORDER BY 2"));
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue