Skip to content

Commit

Permalink
Fix multi bug in auto-complete value on insert/update
Browse files Browse the repository at this point in the history
* explicitly not support multi FK constr, only deal
  with the first one. marked as FIXME
* some bad escaped vars were failing the auto-complete
  on very weird table/field names
* some cleanup
  • Loading branch information
ioguix committed Sep 28, 2010
1 parent ea73ca2 commit 32888a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
28 changes: 20 additions & 8 deletions ajax-ac-insert.php
Expand Up @@ -7,13 +7,25 @@
$_POST['offset'] = 0;
$offset = " OFFSET 0";
}
$keyspos = array_combine($_POST['fkeynames'], $_POST['keys']);
$keysnames = array_combine($_POST['fkeynames'], $_POST['keynames']);

$keynames = array();
foreach ($_POST['fkeynames'] as $k => $v) {
$fkeynames[$k] = html_entity_decode($v, ENT_QUOTES);
}

$keyspos = array_combine($fkeynames, $_POST['keys']);

$f_schema = html_entity_decode($_POST['f_schema'], ENT_QUOTES);
$data->fieldClean($f_schema);
$f_table = html_entity_decode($_POST['f_table'], ENT_QUOTES);
$data->fieldClean($f_table);
$f_attname = $fkeynames[$_POST['fattpos'][0]];
$data->fieldClean($f_attname);

$q = "SELECT *
FROM \"{$_POST['f_schema']}\".\"{$_POST['f_table']}\"
WHERE \"{$_POST['fkeynames'][$_POST['fattpos']]}\"::text LIKE '{$_POST['fvalue']}%'
ORDER BY \"{$_POST['fkeynames'][$_POST['fattpos']]}\" LIMIT 12 {$offset};";
FROM \"{$f_schema}\".\"{$f_table}\"
WHERE \"{$f_attname}\"::text LIKE '{$_POST['fvalue']}%'
ORDER BY \"{$f_attname}\" LIMIT 12 {$offset};";

$res = $data->selectSet($q);

Expand All @@ -23,7 +35,7 @@
foreach (array_keys($res->fields) as $h) {
echo '<th>';

if (in_array($h,$_POST['fkeynames']))
if (in_array($h, $fkeynames))
echo '<img src="'. $misc->icon('ForeignKey') .'" alt="[referenced key]" />';

echo htmlentities($h), '</th>';
Expand All @@ -34,7 +46,7 @@
while ((!$res->EOF) && ($i < 11)) {
echo "<tr class=\"acline\">";
foreach ($res->fields as $n => $v) {
if (in_array($n,$_POST['fkeynames']))
if (in_array($n, $fkeynames))
echo "<td><a href=\"javascript:void(0)\" class=\"fkval\" name=\"{$keyspos[$n]}\">",htmlentities($v), "</a></td>";
else
echo "<td><a href=\"javascript:void(0)\">", htmlentities($v), "</a></td>";
Expand Down Expand Up @@ -66,7 +78,7 @@
echo $js ."</script>";
}
else {
printf("<p>{$lang['strnofkref']}</p>", "\"{$_POST['f_schema']}\".\"{$_POST['f_table']}\".\"{$_POST['fkeynames'][$_POST['fattpos']]}\"");
printf("<p>{$lang['strnofkref']}</p>", "\"{$_POST['f_schema']}\".\"{$_POST['f_table']}\".\"{$fkeynames[$_POST['fattpos']]}\"");

if ($_POST['offset'])
echo "<a href=\"javascript:void(0)\" class=\"fkprev\">Prev &lt;&lt;</a>";
Expand Down
34 changes: 28 additions & 6 deletions classes/Misc.php
Expand Up @@ -2154,6 +2154,28 @@ function printConnection($onchange) {
echo "</td></tr></table>\n";
}

/**
* returns an array representing FKs definition for a table, sorted by fields
* or by constraint.
* @param $table The table to retrieve FK contraints from
* @returns the array of FK definition:
* array(
* 'byconstr' => array(
* constrain id => array(
* confrelid => foreign relation oid
* f_schema => foreign schema name
* f_table => foreign table name
* pattnums => array of parent's fields nums
* pattnames => array of parent's fields names
* fattnames => array of foreign attributes names
* )
* ),
* 'byfield' => array(
* attribute num => array (constraint id, ...)
* ),
* 'code' => HTML/js code to include in the page for auto-completion
* )
**/
function getAutocompleteFKProperties($table) {
global $data;

Expand Down Expand Up @@ -2186,7 +2208,7 @@ function getAutocompleteFKProperties($table) {

if (!isset($fksprops['byfield'][$constrs->fields['p_attnum']]))
$fksprops['byfield'][$constrs->fields['p_attnum']] = array();
$fksprops['byfield'][$constrs->fields['p_attnum']] = $constrs->fields['conid'];
$fksprops['byfield'][$constrs->fields['p_attnum']][] = $constrs->fields['conid'];
}
$constrs->moveNext();
}
Expand All @@ -2196,8 +2218,8 @@ function getAutocompleteFKProperties($table) {
foreach ($fksprops['byconstr'] as $conid => $props) {
$fksprops['code'] .= "constrs.constr_{$conid} = {\n";
$fksprops['code'] .= 'pattnums: ['. implode(',',$props['pattnums']) ."],\n";
$fksprops['code'] .= "f_table:\"". htmlentities($props['f_table']) ."\",\n";
$fksprops['code'] .= "f_schema:\"". htmlentities($props['f_schema']) ."\",\n";
$fksprops['code'] .= "f_table:'". addslashes(htmlentities($props['f_table'], ENT_QUOTES)) ."',\n";
$fksprops['code'] .= "f_schema:'". addslashes(htmlentities($props['f_schema'], ENT_QUOTES)) ."',\n";
$_ = '';
foreach ($props['pattnames'] as $n) {
$_ .= ",'". htmlentities($n, ENT_QUOTES) ."'";
Expand All @@ -2215,12 +2237,12 @@ function getAutocompleteFKProperties($table) {

$fksprops['code'] .= "var attrs = {};\n";
foreach ($fksprops['byfield'] as $attnum => $cstrs ) {
$fksprops['code'] .= "attrs.attr_{$attnum} = {$fksprops['byfield'][$attnum]};\n";
$fksprops['code'] .= "attrs.attr_{$attnum} = [". implode(',', $fksprops['byfield'][$attnum]) ."];\n";
}

$fksprops['code'] .= "var table='". htmlentities($_REQUEST['table']) ."';";
$fksprops['code'] .= "var table='". addslashes(htmlentities($table, ENT_QUOTES)) ."';";
$fksprops['code'] .= "var server='". htmlentities($_REQUEST['server']) ."';";
$fksprops['code'] .= "var database='". htmlentities($_REQUEST['database']) ."';";
$fksprops['code'] .= "var database='". addslashes(htmlentities($_REQUEST['database'], ENT_QUOTES)) ."';";
$fksprops['code'] .= "</script>\n";

$fksprops['code'] .= '<div id="fkbg"></div>';
Expand Down
3 changes: 2 additions & 1 deletion js/ac_insert_row.js
Expand Up @@ -53,7 +53,8 @@ function selectVal(index) {
function openlist(e) {
var elt = jQuery(e);
var attnum = elt.attr('id').match(/\d+/)[0];
var conid = attrs['attr_'+attnum];
/* FIXME we only support the first FK constraint of the field */
var conid = attrs['attr_'+attnum][0];

var constr = constrs["constr_" + conid];

Expand Down

0 comments on commit 32888a8

Please sign in to comment.