alecpl
2010-01-26 2273d4117fd50ee44dcdaa28fd6444383dc403a0
program/lib/MDB2/Driver/Reverse/mysqli.php
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org>                           |
// +----------------------------------------------------------------------+
//
// $Id: mysqli.php,v 1.69 2007/11/25 13:38:29 quipo Exp $
// $Id: mysqli.php 292715 2009-12-28 14:06:34Z quipo $
//
require_once 'MDB2/Driver/Reverse/Common.php';
@@ -167,7 +167,7 @@
                $default = false;
                if (array_key_exists('default', $column)) {
                    $default = $column['default'];
                    if (is_null($default) && $notnull) {
                    if ((null === $default) && $notnull) {
                        $default = '';
                    }
                }
@@ -185,13 +185,13 @@
                    'notnull' => $notnull,
                    'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
                );
                if (!is_null($length)) {
                if (null !== $length) {
                    $definition[0]['length'] = $length;
                }
                if (!is_null($unsigned)) {
                if (null !== $unsigned) {
                    $definition[0]['unsigned'] = $unsigned;
                }
                if (!is_null($fixed)) {
                if (null !== $fixed) {
                    $definition[0]['fixed'] = $fixed;
                }
                if ($default !== false) {
@@ -200,7 +200,7 @@
                if ($autoincrement !== false) {
                    $definition[0]['autoincrement'] = $autoincrement;
                }
                if (!is_null($collate)) {
                if (null !== $collate) {
                    $definition[0]['collate'] = $collate;
                    $definition[0]['charset'] = $charset;
                }
@@ -246,7 +246,7 @@
        $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
        $index_name_mdb2 = $db->getIndexName($index_name);
        $result = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2)));
        if (!PEAR::isError($result) && !is_null($result)) {
        if (!PEAR::isError($result) && (null !== $result)) {
            // apply 'idxname_format' only if the query succeeded, otherwise
            // fallback to the given $index_name, without transformation
            $index_name = $index_name_mdb2;
@@ -323,7 +323,7 @@
        if (strtolower($constraint_name) != 'primary') {
            $constraint_name_mdb2 = $db->getIndexName($constraint_name);
            $result = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2)));
            if (!PEAR::isError($result) && !is_null($result)) {
            if (!PEAR::isError($result) && (null !== $result)) {
                // apply 'idxname_format' only if the query succeeded, otherwise
                // fallback to the given $index_name, without transformation
                $constraint_name = $constraint_name_mdb2;
@@ -364,6 +364,56 @@
            if ($constraint_name == $key_name) {
                if ($row['non_unique']) {
                    //FOREIGN KEY?
                    return $this->_getTableFKConstraintDefinition($table, $constraint_name_original, $definition);
                }
                if ($row['key_name'] == 'PRIMARY') {
                    $definition['primary'] = true;
                } elseif (!$row['non_unique']) {
                    $definition['unique'] = true;
                }
                $column_name = $row['column_name'];
                if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
                    if ($db->options['field_case'] == CASE_LOWER) {
                        $column_name = strtolower($column_name);
                    } else {
                        $column_name = strtoupper($column_name);
                    }
                }
                $definition['fields'][$column_name] = array(
                    'position' => $colpos++
                );
                if (!empty($row['collation'])) {
                    $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
                        ? 'ascending' : 'descending');
                }
            }
        }
        $result->free();
        if (empty($definition['fields'])) {
            return $this->_getTableFKConstraintDefinition($table, $constraint_name_original, $definition);
        }
        return $definition;
    }
    // }}}
    // {{{ _getTableFKConstraintDefinition()
    /**
     * Get the FK definition from the CREATE TABLE statement
     *
     * @param string $table           table name
     * @param string $constraint_name constraint name
     * @param array  $definition      default values for constraint definition
     *
     * @return array|PEAR_Error
     * @access private
     */
    function _getTableFKConstraintDefinition($table, $constraint_name, $definition)
    {
        $db =& $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
                    $query = 'SHOW CREATE TABLE '. $db->escape($table);
                    $constraint = $db->queryOne($query, 'text', 1);
                    if (!PEAR::isError($constraint) && !empty($constraint)) {
@@ -374,6 +424,8 @@
                                $constraint = strtoupper($constraint);
                            }
                        }
            $constraint_name_original = $constraint_name;
            $constraint_name = $db->getIndexName($constraint_name);
                        $pattern = '/\bCONSTRAINT\s+'.$constraint_name.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^ ]+) \(([^\)]+)\)/i';
                        if (!preg_match($pattern, str_replace('`', '', $constraint), $matches)) {
                            //fallback to original constraint name
@@ -405,38 +457,8 @@
                            return $definition;
                        }
                    }
                    return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                        $constraint_name . ' is not an existing table constraint', __FUNCTION__);
                }
                if ($row['key_name'] == 'PRIMARY') {
                    $definition['primary'] = true;
                } elseif (!$row['non_unique']) {
                    $definition['unique'] = true;
                }
                $column_name = $row['column_name'];
                if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
                    if ($db->options['field_case'] == CASE_LOWER) {
                        $column_name = strtolower($column_name);
                    } else {
                        $column_name = strtoupper($column_name);
                    }
                }
                $definition['fields'][$column_name] = array(
                    'position' => $colpos++
                );
                if (!empty($row['collation'])) {
                    $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
                        ? 'ascending' : 'descending');
                }
            }
        }
        $result->free();
        if (empty($definition['fields'])) {
            return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                $constraint_name . ' is not an existing table constraint', __FUNCTION__);
        }
        return $definition;
    }
    // }}}