alecpl
2011-08-16 5c2ac5f528ef39c3e4ae6aab81e4ff93eabc6ac5
program/lib/MDB2/Driver/Reverse/mysql.php
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org>                           |
// +----------------------------------------------------------------------+
//
// $Id: mysql.php 292715 2009-12-28 14:06:34Z quipo $
// $Id: mysql.php 295587 2010-02-28 17:16:38Z quipo $
//
require_once 'MDB2/Driver/Reverse/Common.php';
@@ -69,7 +69,7 @@
     */
    function getTableFieldDefinition($table_name, $field_name)
    {
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
@@ -117,9 +117,17 @@
                        $default = '';
                    }
                }
                $definition[0] = array(
                    'notnull' => $notnull,
                    'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
                );
                $autoincrement = false;
                if (!empty($column['extra']) && $column['extra'] == 'auto_increment') {
                    $autoincrement = true;
                if (!empty($column['extra'])) {
                    if ($column['extra'] == 'auto_increment') {
                        $autoincrement = true;
                    } else {
                        $definition[0]['extra'] = $column['extra'];
                    }
                }
                $collate = null;
                if (!empty($column['collation'])) {
@@ -127,10 +135,6 @@
                    $charset = preg_replace('/(.+?)(_.+)?/', '$1', $collate);
                }
                $definition[0] = array(
                    'notnull' => $notnull,
                    'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
                );
                if (null !== $length) {
                    $definition[0]['length'] = $length;
                }
@@ -181,7 +185,7 @@
     */
    function getTableIndexDefinition($table_name, $index_name)
    {
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
@@ -256,7 +260,7 @@
     */
    function getTableConstraintDefinition($table_name, $constraint_name)
    {
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
@@ -356,10 +360,16 @@
     */
    function _getTableFKConstraintDefinition($table, $constraint_name, $definition)
    {
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
        //Use INFORMATION_SCHEMA instead?
        //SELECT *
        //  FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
        // WHERE CONSTRAINT_SCHEMA = '$dbname'
        //   AND TABLE_NAME = '$table'
        //   AND CONSTRAINT_NAME = '$constraint_name';
        $query = 'SHOW CREATE TABLE '. $db->escape($table);
        $constraint = $db->queryOne($query, 'text', 1);
        if (!PEAR::isError($constraint) && !empty($constraint)) {
@@ -372,10 +382,10 @@
            }
            $constraint_name_original = $constraint_name;
            $constraint_name = $db->getIndexName($constraint_name);
            $pattern = '/\bCONSTRAINT\s+'.$constraint_name.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^ ]+) \(([^\)]+)\)/i';
            $pattern = '/\bCONSTRAINT\s+'.$constraint_name.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^\s]+) \(([^\)]+)\)(?: ON DELETE ([^\s]+))?(?: ON UPDATE ([^\s]+))?/i';
            if (!preg_match($pattern, str_replace('`', '', $constraint), $matches)) {
                //fallback to original constraint name
                $pattern = '/\bCONSTRAINT\s+'.$constraint_name_original.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^ ]+) \(([^\)]+)\)/i';
                $pattern = '/\bCONSTRAINT\s+'.$constraint_name_original.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^\s]+) \(([^\)]+)\)(?: ON DELETE ([^\s]+))?(?: ON UPDATE ([^\s]+))?/i';
            }
            if (preg_match($pattern, str_replace('`', '', $constraint), $matches)) {
                $definition['foreign'] = true;
@@ -397,8 +407,8 @@
                        'position' => $colpos++
                    );
                }
                $definition['onupdate'] = 'NO ACTION';
                $definition['ondelete'] = 'NO ACTION';
                $definition['ondelete'] = empty($matches[4]) ? 'RESTRICT' : strtoupper($matches[4]);
                $definition['onupdate'] = empty($matches[5]) ? 'RESTRICT' : strtoupper($matches[5]);
                $definition['match']    = 'SIMPLE';
                return $definition;
            }
@@ -424,7 +434,7 @@
     */
    function getTriggerDefinition($trigger)
    {
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
@@ -476,7 +486,7 @@
           return parent::tableInfo($result, $mode);
        }
        $db =& $this->getDBInstance();
        $db = $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
@@ -506,11 +516,11 @@
        $db->loadModule('Datatype', null, true);
        for ($i = 0; $i < $count; $i++) {
            $res[$i] = array(
                'table' => $case_func(@mysql_field_table($resource, $i)),
                'name'  => $case_func(@mysql_field_name($resource, $i)),
                'type'  => @mysql_field_type($resource, $i),
                'length'   => @mysql_field_len($resource, $i),
                'flags' => @mysql_field_flags($resource, $i),
                'table'  => $case_func(@mysql_field_table($resource, $i)),
                'name'   => $case_func(@mysql_field_name($resource, $i)),
                'type'   => @mysql_field_type($resource, $i),
                'length' => @mysql_field_len($resource, $i),
                'flags'  => @mysql_field_flags($resource, $i),
            );
            if ($res[$i]['type'] == 'string') {
                $res[$i]['type'] = 'char';