printTrail('table'); $misc->printTitle($lang['straddfk'],'pg.constraint.foreign_key'); $misc->printMsg($msg); // Unserialize target and fetch appropriate table. This is a bit messy // because the table could be in another schema. $data->setSchema($_REQUEST['target']['schemaname']); $attrs = $data->getTableAttributes($_REQUEST['target']['tablename']); $data->setSchema($_REQUEST['schema']); $selColumns = new XHTML_select('TableColumnList', true, 10); $selColumns->set_style('width: 15em;'); if ($attrs->recordCount() > 0) { while (!$attrs->EOF) { $selColumns->add(new XHTML_Option($attrs->fields['attname'])); $attrs->moveNext(); } } $selIndex = new XHTML_select('IndexColumnList[]', true, 10); $selIndex->set_style('width: 15em;'); $selIndex->set_attribute('id', 'IndexColumnList'); $buttonAdd = new XHTML_Button('add', '>>'); $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); $buttonAdd->set_attribute('type', 'button'); $buttonRemove = new XHTML_Button('remove', '<<'); $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); $buttonRemove->set_attribute('type', 'button'); echo "
\n"; } break; case 3: // Unserialize target $_POST['target'] = unserialize($_POST['target']); // Check that they've given at least one column if (isset($_POST['SourceColumnList'])) $temp = unserialize($_POST['SourceColumnList']); if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || sizeof($_POST['IndexColumnList']) == 0 || !isset($temp) || !is_array($temp) || sizeof($temp) == 0) addForeignKey(2, $lang['strfkneedscols']); else { $status = $data->addForeignKey($_POST['table'], $_POST['target']['schemaname'], $_POST['target']['tablename'], unserialize($_POST['SourceColumnList']), $_POST['IndexColumnList'], $_POST['upd_action'], $_POST['del_action'], $_POST['match'], $_POST['deferrable'], $_POST['initially'], $_POST['name']); if ($status == 0) doDefault($lang['strfkadded']); else addForeignKey(2, $lang['strfkaddedbad']); } break; default: $misc->printTrail('table'); $misc->printTitle($lang['straddfk'],'pg.constraint.foreign_key'); $misc->printMsg($msg); $attrs = $data->getTableAttributes($_REQUEST['table']); $tables = $data->getTables(true); $selColumns = new XHTML_select('TableColumnList', true, 10); $selColumns->set_style('width: 15em;'); if ($attrs->recordCount() > 0) { while (!$attrs->EOF) { $selColumns->add(new XHTML_Option($attrs->fields['attname'])); $attrs->moveNext(); } } $selIndex = new XHTML_select('IndexColumnList[]', true, 10); $selIndex->set_style('width: 15em;'); $selIndex->set_attribute('id', 'IndexColumnList'); $buttonAdd = new XHTML_Button('add', '>>'); $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); $buttonAdd->set_attribute('type', 'button'); $buttonRemove = new XHTML_Button('remove', '<<'); $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); $buttonRemove->set_attribute('type', 'button'); echo "\n"; break; } } /** * Confirm and then actually add a PRIMARY KEY or UNIQUE constraint */ function addPrimaryOrUniqueKey($type, $confirm, $msg = '') { global $data, $misc; global $lang; if (!isset($_POST['name'])) $_POST['name'] = ''; if ($confirm) { if (!isset($_POST['name'])) $_POST['name'] = ''; if (!isset($_POST['tablespace'])) $_POST['tablespace'] = ''; $misc->printTrail('table'); switch ($type) { case 'primary': $misc->printTitle($lang['straddpk'],'pg.constraint.primary_key'); break; case 'unique': $misc->printTitle($lang['stradduniq'],'pg.constraint.unique_key'); break; default: doDefault($lang['strinvalidparam']); return; } $misc->printMsg($msg); $attrs = $data->getTableAttributes($_REQUEST['table']); // Fetch all tablespaces from the database if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces(); $selColumns = new XHTML_select('TableColumnList', true, 10); $selColumns->set_style('width: 15em;'); if ($attrs->recordCount() > 0) { while (!$attrs->EOF) { $selColumns->add(new XHTML_Option($attrs->fields['attname'])); $attrs->moveNext(); } } $selIndex = new XHTML_select('IndexColumnList[]', true, 10); $selIndex->set_style('width: 15em;'); $selIndex->set_attribute('id', 'IndexColumnList'); $buttonAdd = new XHTML_Button('add', '>>'); $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); $buttonAdd->set_attribute('type', 'button'); $buttonRemove = new XHTML_Button('remove', '<<'); $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); $buttonRemove->set_attribute('type', 'button'); echo "\n"; } else { // Default tablespace to empty if it isn't set if (!isset($_POST['tablespace'])) $_POST['tablespace'] = ''; if ($_POST['type'] == 'primary') { // Check that they've given at least one column if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || sizeof($_POST['IndexColumnList']) == 0) addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkneedscols']); else { $status = $data->addPrimaryKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']); if ($status == 0) doDefault($lang['strpkadded']); else addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkaddedbad']); } } elseif ($_POST['type'] == 'unique') { // Check that they've given at least one column if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || sizeof($_POST['IndexColumnList']) == 0) addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqneedscols']); else { $status = $data->addUniqueKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']); if ($status == 0) doDefault($lang['struniqadded']); else addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqaddedbad']); } } else doDefault($lang['strinvalidparam']); } } /** * Confirm and then actually add a CHECK constraint */ function addCheck($confirm, $msg = '') { global $data, $misc; global $lang; if (!isset($_POST['name'])) $_POST['name'] = ''; if (!isset($_POST['definition'])) $_POST['definition'] = ''; if ($confirm) { $misc->printTrail('table'); $misc->printTitle($lang['straddcheck'],'pg.constraint.check'); $misc->printMsg($msg); echo "\n"; } else { if (trim($_POST['definition']) == '') addCheck(true, $lang['strcheckneedsdefinition']); else { $status = $data->addCheckConstraint($_POST['table'], $_POST['definition'], $_POST['name']); if ($status == 0) doDefault($lang['strcheckadded']); else addCheck(true, $lang['strcheckaddedbad']); } } } /** * Show confirmation of drop and perform actual drop */ function doDrop($confirm) { global $data, $misc; global $lang; if ($confirm) { $misc->printTrail('constraint'); $misc->printTitle($lang['strdrop'],'pg.constraint.drop'); echo "", sprintf($lang['strconfdropconstraint'], $misc->printVal($_REQUEST['constraint']), $misc->printVal($_REQUEST['table'])), "
\n"; echo "\n"; } else { $status = $data->dropConstraint($_POST['constraint'], $_POST['table'], $_POST['type'], isset($_POST['cascade'])); if ($status == 0) doDefault($lang['strconstraintdropped']); else doDefault($lang['strconstraintdroppedbad']); } } /** * List all the constraints on the table */ function doDefault($msg = '') { global $data, $misc, $lang; function cnPre(&$rowdata) { global $data; if (is_null($rowdata->fields['consrc'])) { $atts = $data->getAttributeNames($_REQUEST['table'], explode(' ', $rowdata->fields['indkey'])); $rowdata->fields['+definition'] = ($rowdata->fields['contype'] == 'u' ? "UNIQUE (" : "PRIMARY KEY (") . join(',', $atts) . ')'; } else { $rowdata->fields['+definition'] = $rowdata->fields['consrc']; } } $misc->printTrail('table'); $misc->printTabs('table','constraints'); $misc->printMsg($msg); $constraints = $data->getConstraints($_REQUEST['table']); $columns = array( 'constraint' => array( 'title' => $lang['strname'], 'field' => field('conname'), ), 'definition' => array( 'title' => $lang['strdefinition'], 'field' => field('+definition'), 'type' => 'pre', ), 'actions' => array( 'title' => $lang['stractions'], ), 'comment' => array( 'title' => $lang['strcomment'], 'field' => field('constcomment'), ), ); $actions = array( 'drop' => array( 'title' => $lang['strdrop'], 'url' => "constraints.php?action=confirm_drop&{$misc->href}&table=".urlencode($_REQUEST['table'])."&", 'vars' => array('constraint' => 'conname', 'type' => 'contype'), ), ); $misc->printTable($constraints, $columns, $actions, $lang['strnoconstraints'], 'cnPre'); echo "