2006-12-28 15:27:44 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload('Doctrine_Import');
|
|
|
|
/**
|
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
|
|
|
* @version $Revision$
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Import_Mysql extends Doctrine_Import
|
|
|
|
{
|
2006-12-30 13:58:58 +03:00
|
|
|
protected $sql = array(
|
|
|
|
'showDatabases' => 'SHOW DATABASES',
|
|
|
|
'listTableFields' => 'DESCRIBE %s',
|
|
|
|
'listSequences' => 'SHOW TABLES',
|
|
|
|
'listTables' => 'SHOW TABLES',
|
|
|
|
'listUsers' => 'SELECT DISTINCT USER FROM USER',
|
|
|
|
'listViews' => "SHOW FULL TABLES %sWHERE Table_type = 'VIEW'",
|
|
|
|
);
|
2006-12-28 15:27:44 +03:00
|
|
|
/**
|
|
|
|
* lists all database sequences
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listSequences($database = null)
|
|
|
|
{
|
2006-12-29 14:03:19 +03:00
|
|
|
$query = "SHOW TABLES";
|
|
|
|
if (!is_null($database)) {
|
|
|
|
$query .= " FROM $database";
|
|
|
|
}
|
2006-12-30 13:58:58 +03:00
|
|
|
$tableNames = $this->conn->fetchColumn($query);
|
2006-12-29 14:03:19 +03:00
|
|
|
|
2007-01-16 01:44:24 +03:00
|
|
|
return array_map(array($this->conn, 'fixSequenceName'), $tableNames);
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableConstraints($table)
|
|
|
|
{
|
2007-01-16 01:44:24 +03:00
|
|
|
$keyName = 'Key_name';
|
|
|
|
$nonUnique = 'Non_unique';
|
|
|
|
if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
|
2007-01-06 01:06:52 +03:00
|
|
|
if ($this->conn->options['field_case'] == CASE_LOWER) {
|
2007-01-16 01:44:24 +03:00
|
|
|
$keyName = strtolower($keyName);
|
|
|
|
$nonUnique = strtolower($nonUnique);
|
2006-12-29 14:03:19 +03:00
|
|
|
} else {
|
2007-01-16 01:44:24 +03:00
|
|
|
$keyName = strtoupper($keyName);
|
|
|
|
$nonUnique = strtoupper($nonUnique);
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-06 01:06:52 +03:00
|
|
|
$table = $this->conn->quoteIdentifier($table, true);
|
2007-01-16 01:44:24 +03:00
|
|
|
$query = 'SHOW INDEX FROM ' . $table;
|
2007-01-06 01:06:52 +03:00
|
|
|
$indexes = $this->conn->fetchAssoc($query);
|
2006-12-29 14:03:19 +03:00
|
|
|
|
|
|
|
$result = array();
|
2007-01-16 01:44:24 +03:00
|
|
|
foreach ($indexes as $indexData) {
|
|
|
|
if (!$indexData[$nonUnique]) {
|
|
|
|
if ($indexData[$keyName] !== 'PRIMARY') {
|
|
|
|
$index = $this->conn->fixIndexName($indexData[$keyName]);
|
2006-12-29 14:03:19 +03:00
|
|
|
} else {
|
|
|
|
$index = 'PRIMARY';
|
|
|
|
}
|
2007-01-16 01:44:24 +03:00
|
|
|
if ( ! empty($index)) {
|
|
|
|
$result[] = $index;
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-16 01:44:24 +03:00
|
|
|
return $result;
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableColumns($table)
|
|
|
|
{
|
2006-12-30 13:58:58 +03:00
|
|
|
$sql = 'DESCRIBE ' . $table;
|
|
|
|
$result = $this->conn->fetchAssoc($sql);
|
2006-12-28 15:27:44 +03:00
|
|
|
$description = array();
|
2006-12-30 13:58:58 +03:00
|
|
|
foreach ($result as $key => $val) {
|
|
|
|
|
|
|
|
array_change_key_case($val, CASE_LOWER);
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
$description = array(
|
|
|
|
'name' => $val['field'],
|
|
|
|
'type' => $val['type'],
|
|
|
|
'primary' => (strtolower($val['key']) == 'pri'),
|
|
|
|
'default' => $val['default'],
|
|
|
|
'notnull' => (bool) ($val['null'] != 'YES'),
|
2006-12-30 13:58:58 +03:00
|
|
|
'autoinc' => (bool) (strpos($val['extra'], 'auto_increment') !== false),
|
2006-12-28 15:27:44 +03:00
|
|
|
);
|
2006-12-30 13:58:58 +03:00
|
|
|
$columns[$val['field']] = $description;
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableIndexes($table)
|
2006-12-29 14:03:19 +03:00
|
|
|
{
|
2007-01-16 01:44:24 +03:00
|
|
|
$keyName = 'Key_name';
|
|
|
|
$nonUnique = 'Non_unique';
|
2007-01-06 01:06:52 +03:00
|
|
|
if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
|
|
|
|
if ($this->conn->options['field_case'] == CASE_LOWER) {
|
2007-01-16 01:44:24 +03:00
|
|
|
$keyName = strtolower($keyName);
|
|
|
|
$nonUnique = strtolower($nonUnique);
|
2006-12-29 14:03:19 +03:00
|
|
|
} else {
|
2007-01-27 03:07:59 +03:00
|
|
|
$keyName = strtoupper($keyName);
|
2007-01-16 01:44:24 +03:00
|
|
|
$nonUnique = strtoupper($nonUnique);
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-06 01:06:52 +03:00
|
|
|
$table = $this->conn->quoteIdentifier($table, true);
|
2006-12-29 14:03:19 +03:00
|
|
|
$query = "SHOW INDEX FROM $table";
|
2007-01-06 01:06:52 +03:00
|
|
|
$indexes = $this->conn->fetchAssoc($query);
|
2006-12-29 14:03:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
$result = array();
|
2007-01-16 01:44:24 +03:00
|
|
|
foreach ($indexes as $indexData) {
|
|
|
|
if ($indexData[$nonUnique] && ($index = $this->conn->fixIndexName($indexData[$keyName]))) {
|
|
|
|
$result[] = $index;
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
|
|
|
}
|
2007-01-16 01:44:24 +03:00
|
|
|
return $result;
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists tables
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTables($database = null)
|
2006-12-29 14:03:19 +03:00
|
|
|
{
|
2006-12-30 13:58:58 +03:00
|
|
|
return $this->conn->fetchColumn($this->sql['listTables']);
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists database views
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listViews($database = null)
|
2006-12-29 14:03:19 +03:00
|
|
|
{
|
|
|
|
if (!is_null($database)) {
|
2006-12-30 13:58:58 +03:00
|
|
|
$query = sprintf($this->sql['listViews'], ' FROM ' . $database);
|
2007-01-06 01:06:52 +03:00
|
|
|
}
|
2006-12-29 14:03:19 +03:00
|
|
|
|
2006-12-30 13:58:58 +03:00
|
|
|
return $this->conn->fetchColumn($query);
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
}
|