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( 'content' => $lang['strdrop'], 'attr'=> array ( 'href' => array ( 'url' => 'constraints.php', 'urlvars' => array ( 'action' => 'confirm_drop', 'table' => $_REQUEST['table'], 'constraint' => field('conname'), 'type' => field('contype') ) ) ) ) ); $misc->printTable($constraints, $columns, $actions, 'constraints-constraints', $lang['strnoconstraints'], 'cnPre'); $navlinks = array ( 'addcheck' => array ( 'attr'=> array ( 'href' => array ( 'url' => 'constraints.php', 'urlvars' => array ( 'action' => 'add_check', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'table' => $_REQUEST['table'] ) ) ), 'content' => $lang['straddcheck'], ), 'adduniq' => array ( 'attr'=> array ( 'href' => array ( 'url' => 'constraints.php', 'urlvars' => array ( 'action' => 'add_unique_key', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'table' => $_REQUEST['table'] ) ) ), 'content' => $lang['stradduniq'], ), 'addpk' => array ( 'attr'=> array ( 'href' => array ( 'url' => 'constraints.php', 'urlvars' => array ( 'action' => 'add_primary_key', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'table' => $_REQUEST['table'] ) ) ), 'content' => $lang['straddpk'], ), 'addfk' => array ( 'attr'=> array ( 'href' => array ( 'url' => 'constraints.php', 'urlvars' => array ( 'action' => 'add_foreign_key', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'table' => $_REQUEST['table'] ) ) ), 'content' => $lang['straddfk'] ) ); $misc->printNavLinks($navlinks, 'constraints-constraints', get_defined_vars()); } function doTree() { global $misc, $data; $constraints = $data->getConstraints($_REQUEST['table']); $reqvars = $misc->getRequestVars('schema'); function getIcon($f) { switch($f['contype']) { case 'u': return 'UniqueConstraint'; case 'c': return 'CheckConstraint'; case 'f': return 'ForeignKey'; case 'p': return 'PrimaryKey'; } } $attrs = array( 'text' => field('conname'), 'icon' => callback('getIcon'), ); $misc->printTree($constraints, $attrs, 'constraints'); exit; } if ($action == 'tree') doTree(); $misc->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strconstraints'], ""); if ($action == 'add_unique_key' || $action == 'save_add_unique_key' || $action == 'add_primary_key' || $action == 'save_add_primary_key' || $action == 'add_foreign_key' || $action == 'save_add_foreign_key') echo ""; else $misc->printBody(); switch ($action) { case 'add_foreign_key': addForeignKey(1); break; case 'save_add_foreign_key': if (isset($_POST['cancel'])) doDefault(); else addForeignKey($_REQUEST['stage']); break; case 'add_unique_key': addPrimaryOrUniqueKey('unique', true); break; case 'save_add_unique_key': if (isset($_POST['cancel'])) doDefault(); else addPrimaryOrUniqueKey('unique', false); break; case 'add_primary_key': addPrimaryOrUniqueKey('primary', true); break; case 'save_add_primary_key': if (isset($_POST['cancel'])) doDefault(); else addPrimaryOrUniqueKey('primary', false); break; case 'add_check': addCheck(true); break; case 'save_add_check': if (isset($_POST['cancel'])) doDefault(); else addCheck(false); break; case 'save_create': doSaveCreate(); break; case 'create': doCreate(); break; case 'drop': if (isset($_POST['drop'])) doDrop(false); else doDefault(); break; case 'confirm_drop': doDrop(true); break; default: doDefault(); break; } $misc->printFooter(); ?>