PMA\libraries\DatabaseInterface::getTriggers PHP Method

getTriggers() public method

returns details about the TRIGGERs for a specific table or database
public getTriggers ( string $db, string $table = '', string $delimiter = '//' ) : array
$db string db name
$table string table name
$delimiter string the delimiter to use (may be empty)
return array information about triggers (may be empty)
    public function getTriggers($db, $table = '', $delimiter = '//')
    {
        $result = array();
        if (!$GLOBALS['cfg']['Server']['DisableIS']) {
            $query = 'SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION' . ', EVENT_OBJECT_TABLE, ACTION_TIMING, ACTION_STATEMENT' . ', EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE, DEFINER' . ' FROM information_schema.TRIGGERS' . ' WHERE EVENT_OBJECT_SCHEMA ' . Util::getCollateForIS() . '=' . ' \'' . $GLOBALS['dbi']->escapeString($db) . '\'';
            if (!empty($table)) {
                $query .= " AND EVENT_OBJECT_TABLE " . Util::getCollateForIS() . " = '" . $GLOBALS['dbi']->escapeString($table) . "';";
            }
        } else {
            $query = "SHOW TRIGGERS FROM " . Util::backquote($db);
            if (!empty($table)) {
                $query .= " LIKE '" . $GLOBALS['dbi']->escapeString($table) . "';";
            }
        }
        if ($triggers = $this->fetchResult($query)) {
            foreach ($triggers as $trigger) {
                if ($GLOBALS['cfg']['Server']['DisableIS']) {
                    $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
                    $trigger['ACTION_TIMING'] = $trigger['Timing'];
                    $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
                    $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
                    $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
                    $trigger['DEFINER'] = $trigger['Definer'];
                }
                $one_result = array();
                $one_result['name'] = $trigger['TRIGGER_NAME'];
                $one_result['table'] = $trigger['EVENT_OBJECT_TABLE'];
                $one_result['action_timing'] = $trigger['ACTION_TIMING'];
                $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
                $one_result['definition'] = $trigger['ACTION_STATEMENT'];
                $one_result['definer'] = $trigger['DEFINER'];
                // do not prepend the schema name; this way, importing the
                // definition into another schema will work
                $one_result['full_trigger_name'] = Util::backquote($trigger['TRIGGER_NAME']);
                $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
                $one_result['create'] = 'CREATE TRIGGER ' . $one_result['full_trigger_name'] . ' ' . $trigger['ACTION_TIMING'] . ' ' . $trigger['EVENT_MANIPULATION'] . ' ON ' . Util::backquote($trigger['EVENT_OBJECT_TABLE']) . "\n" . ' FOR EACH ROW ' . $trigger['ACTION_STATEMENT'] . "\n" . $delimiter . "\n";
                $result[] = $one_result;
            }
        }
        // Sort results by name
        $name = array();
        foreach ($result as $value) {
            $name[] = $value['name'];
        }
        array_multisort($name, SORT_ASC, $result);
        return $result;
    }

Usage Example

Esempio n. 1
0
 /**
  * renames table
  *
  * @param string $new_name new table name
  * @param string $new_db   new database name
  *
  * @return bool success
  */
 function rename($new_name, $new_db = null)
 {
     $lowerCaseTableNames = Util::cacheGet('lower_case_table_names', function () {
         return $GLOBALS['dbi']->fetchValue("SELECT @@lower_case_table_names");
     });
     if ($lowerCaseTableNames) {
         $new_name = strtolower($new_name);
     }
     if (null !== $new_db && $new_db !== $this->getDbName()) {
         // Ensure the target is valid
         if (!$GLOBALS['pma']->databases->exists($new_db)) {
             $this->errors[] = __('Invalid database:') . ' ' . $new_db;
             return false;
         }
     } else {
         $new_db = $this->getDbName();
     }
     $new_table = new Table($new_name, $new_db);
     if ($this->getFullName() === $new_table->getFullName()) {
         return true;
     }
     if (!Table::isValidName($new_name)) {
         $this->errors[] = __('Invalid table name:') . ' ' . $new_table->getFullName();
         return false;
     }
     // If the table is moved to a different database drop its triggers first
     $triggers = $this->_dbi->getTriggers($this->getDbName(), $this->getName(), '');
     $handle_triggers = $this->getDbName() != $new_db && $triggers;
     if ($handle_triggers) {
         foreach ($triggers as $trigger) {
             $sql = 'DROP TRIGGER IF EXISTS ' . Util::backquote($this->getDbName()) . '.' . Util::backquote($trigger['name']) . ';';
             $this->_dbi->query($sql);
         }
     }
     /*
      * tested also for a view, in MySQL 5.0.92, 5.1.55 and 5.5.13
      */
     $GLOBALS['sql_query'] = '
         RENAME TABLE ' . $this->getFullName(true) . '
               TO ' . $new_table->getFullName(true) . ';';
     // I don't think a specific error message for views is necessary
     if (!$this->_dbi->query($GLOBALS['sql_query'])) {
         // Restore triggers in the old database
         if ($handle_triggers) {
             $this->_dbi->selectDb($this->getDbName());
             foreach ($triggers as $trigger) {
                 $this->_dbi->query($trigger['create']);
             }
         }
         $this->errors[] = sprintf(__('Failed to rename table %1$s to %2$s!'), $this->getFullName(), $new_table->getFullName());
         return false;
     }
     $old_name = $this->getName();
     $old_db = $this->getDbName();
     $this->_name = $new_name;
     $this->_db_name = $new_db;
     // Renable table in configuration storage
     PMA_REL_renameTable($old_db, $new_db, $old_name, $new_name);
     $this->messages[] = sprintf(__('Table %1$s has been renamed to %2$s.'), htmlspecialchars($old_name), htmlspecialchars($new_name));
     return true;
 }