PMA\libraries\Table::isValidName PHP 메소드

isValidName() 정적인 공개 메소드

checks if given name is a valid table name, currently if not empty, trailing spaces, '.', '/' and '\'
또한 보기: https://dev.mysql.com/doc/refman/5.0/en/legal-names.html
static public isValidName ( string $table_name, boolean $is_backquoted = false ) : boolean
$table_name string name to check
$is_backquoted boolean whether this name is used inside backquotes or not
리턴 boolean whether the string is valid or not
    static function isValidName($table_name, $is_backquoted = false)
    {
        if ($table_name !== rtrim($table_name)) {
            // trailing spaces not allowed even in backquotes
            return false;
        }
        if (strlen($table_name) === 0) {
            // zero length
            return false;
        }
        if (!$is_backquoted && $table_name !== trim($table_name)) {
            // spaces at the start or in between only allowed inside backquotes
            return false;
        }
        if (!$is_backquoted && preg_match('/^[a-zA-Z0-9_$]+$/', $table_name)) {
            // only allow the above regex in unquoted identifiers
            // see : https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
            return true;
        } else {
            if ($is_backquoted) {
                // If backquoted, all characters should be allowed (except w/ trailing spaces)
                return true;
            }
        }
        // If not backquoted and doesn't follow the above regex
        return false;
    }

Usage Example

예제 #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;
 }
All Usage Examples Of PMA\libraries\Table::isValidName