2009-11-27 00:56:08 +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.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\DBAL\Schema;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
2009-11-28 01:00:43 +03:00
|
|
|
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
2010-02-20 01:33:54 +03:00
|
|
|
use Doctrine\DBAL\DBALException;
|
2009-11-27 00:56:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object Representation of a table
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
*/
|
|
|
|
class Table extends AbstractAsset
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const ID_NONE = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const ID_SEQUENCE = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const ID_IDENTITY = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_name = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_columns = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_indexes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_primaryKeyName = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-11-29 18:51:14 +03:00
|
|
|
protected $_fkConstraints = array();
|
2009-11-27 00:56:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_options = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_idGeneratorType = self::ID_NONE;
|
|
|
|
|
2009-12-11 02:55:47 +03:00
|
|
|
/**
|
|
|
|
* @var SchemaConfig
|
|
|
|
*/
|
|
|
|
protected $_schemaConfig = null;
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $indexes
|
2009-11-29 18:51:14 +03:00
|
|
|
* @param array $fkConstraints
|
2009-11-27 00:56:08 +03:00
|
|
|
* @param int $idGeneratorType
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2009-11-29 18:51:14 +03:00
|
|
|
public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType=self::ID_NONE, array $options=array())
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2010-02-20 01:33:54 +03:00
|
|
|
if (strlen($tableName) == 0) {
|
|
|
|
throw DBALException::invalidTableName($tableName);
|
|
|
|
}
|
|
|
|
|
2009-12-04 01:10:13 +03:00
|
|
|
$this->_setName($tableName);
|
2009-11-27 00:56:08 +03:00
|
|
|
$this->_idGeneratorType = $idGeneratorType;
|
|
|
|
|
|
|
|
foreach ($columns AS $column) {
|
|
|
|
$this->_addColumn($column);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($indexes AS $idx) {
|
|
|
|
$this->_addIndex($idx);
|
|
|
|
}
|
|
|
|
|
2009-11-29 18:51:14 +03:00
|
|
|
foreach ($fkConstraints AS $constraint) {
|
|
|
|
$this->_addForeignKeyConstraint($constraint);
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_options = $options;
|
|
|
|
}
|
|
|
|
|
2009-12-11 02:55:47 +03:00
|
|
|
/**
|
|
|
|
* @param SchemaConfig $schemaConfig
|
|
|
|
*/
|
|
|
|
public function setSchemaConfig(SchemaConfig $schemaConfig)
|
|
|
|
{
|
|
|
|
$this->_schemaConfig = $schemaConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function _getMaxIdentifierLength()
|
|
|
|
{
|
|
|
|
if ($this->_schemaConfig instanceof SchemaConfig) {
|
|
|
|
return $this->_schemaConfig->getMaxIdentifierLength();
|
|
|
|
} else {
|
|
|
|
return 63;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
/**
|
|
|
|
* Set Primary Key
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @param string $indexName
|
|
|
|
* @return Table
|
|
|
|
*/
|
2010-01-06 16:23:56 +03:00
|
|
|
public function setPrimaryKey(array $columns, $indexName = false)
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2010-02-04 21:23:38 +03:00
|
|
|
$primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true);
|
|
|
|
|
|
|
|
foreach ($columns AS $columnName) {
|
|
|
|
$column = $this->getColumn($columnName);
|
|
|
|
$column->setNotnull(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $primaryKey;
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function setIdGeneratorType($type)
|
|
|
|
{
|
|
|
|
$this->_idGeneratorType = $type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $columnNames
|
|
|
|
* @param string $indexName
|
|
|
|
* @return Table
|
|
|
|
*/
|
2010-01-06 16:23:56 +03:00
|
|
|
public function addIndex(array $columnNames, $indexName = null)
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2009-11-29 18:51:14 +03:00
|
|
|
if($indexName == null) {
|
2009-12-06 21:36:46 +03:00
|
|
|
$indexName = $this->_generateIdentifierName(
|
2009-12-11 02:55:47 +03:00
|
|
|
array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
|
2009-12-06 21:36:46 +03:00
|
|
|
);
|
2009-11-29 18:51:14 +03:00
|
|
|
}
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
return $this->_createIndex($columnNames, $indexName, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param array $columnNames
|
|
|
|
* @param string $indexName
|
|
|
|
* @return Table
|
|
|
|
*/
|
2010-01-06 16:23:56 +03:00
|
|
|
public function addUniqueIndex(array $columnNames, $indexName = null)
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2009-11-29 18:51:14 +03:00
|
|
|
if ($indexName == null) {
|
2009-12-06 21:36:46 +03:00
|
|
|
$indexName = $this->_generateIdentifierName(
|
2009-12-11 02:55:47 +03:00
|
|
|
array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
|
2009-12-06 21:36:46 +03:00
|
|
|
);
|
2009-11-29 18:51:14 +03:00
|
|
|
}
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
return $this->_createIndex($columnNames, $indexName, true, false);
|
|
|
|
}
|
|
|
|
|
2010-02-07 15:36:30 +03:00
|
|
|
/**
|
|
|
|
* Check if an index begins in the order of the given columns.
|
|
|
|
*
|
|
|
|
* @param array $columnsNames
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function columnsAreIndexed(array $columnsNames)
|
|
|
|
{
|
|
|
|
foreach ($this->getIndexes() AS $index) {
|
|
|
|
$indexColumns = $index->getColumns();
|
|
|
|
$areIndexed = true;
|
|
|
|
for ($i = 0; $i < count($columnsNames); $i++) {
|
|
|
|
if ($columnsNames[$i] != $indexColumns[$i]) {
|
|
|
|
$areIndexed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($areIndexed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param array $columnNames
|
|
|
|
* @param string $indexName
|
|
|
|
* @param bool $isUnique
|
|
|
|
* @param bool $isPrimary
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary)
|
|
|
|
{
|
|
|
|
if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) {
|
|
|
|
throw SchemaException::indexNameInvalid($indexName);
|
|
|
|
}
|
|
|
|
|
2009-12-01 01:33:08 +03:00
|
|
|
foreach ($columnNames AS $columnName => $indexColOptions) {
|
|
|
|
if (is_numeric($columnName) && is_string($indexColOptions)) {
|
|
|
|
$columnName = $indexColOptions;
|
|
|
|
}
|
|
|
|
|
2009-12-07 19:21:29 +03:00
|
|
|
if ( ! $this->hasColumn($columnName)) {
|
2009-11-27 00:56:08 +03:00
|
|
|
throw SchemaException::columnDoesNotExist($columnName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_addIndex(new Index($indexName, $columnNames, $isUnique, $isPrimary));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $columnName
|
|
|
|
* @param string $columnType
|
|
|
|
* @param array $options
|
2009-12-05 02:03:08 +03:00
|
|
|
* @return Column
|
2009-11-27 00:56:08 +03:00
|
|
|
*/
|
2010-02-26 00:51:30 +03:00
|
|
|
public function addColumn($columnName, $typeName, array $options=array())
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
|
|
|
$column = new Column($columnName, Type::getType($typeName), $options);
|
|
|
|
|
|
|
|
$this->_addColumn($column);
|
2009-12-05 02:03:08 +03:00
|
|
|
return $column;
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename Column
|
|
|
|
*
|
|
|
|
* @param string $oldColumnName
|
|
|
|
* @param string $newColumnName
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function renameColumn($oldColumnName, $newColumnName)
|
|
|
|
{
|
|
|
|
$column = $this->getColumn($oldColumnName);
|
|
|
|
$this->dropColumn($oldColumnName);
|
|
|
|
|
|
|
|
$column->_setName($newColumnName);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change Column Details
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @param array $options
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function changeColumn($columnName, array $options)
|
|
|
|
{
|
|
|
|
$column = $this->getColumn($columnName);
|
|
|
|
$column->setOptions($options);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop Column from Table
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function dropColumn($columnName)
|
|
|
|
{
|
2009-12-01 02:12:26 +03:00
|
|
|
$columnName = strtolower($columnName);
|
2009-11-27 00:56:08 +03:00
|
|
|
$column = $this->getColumn($columnName);
|
|
|
|
unset($this->_columns[$columnName]);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a foreign key constraint
|
|
|
|
*
|
2009-11-29 18:51:14 +03:00
|
|
|
* Name is inferred from the local columns
|
|
|
|
*
|
2009-11-27 00:56:08 +03:00
|
|
|
* @param Table $foreignTable
|
|
|
|
* @param array $localColumns
|
|
|
|
* @param array $foreignColumns
|
|
|
|
* @param array $options
|
|
|
|
* @return Table
|
|
|
|
*/
|
2009-11-29 18:51:14 +03:00
|
|
|
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
|
|
|
|
{
|
2009-12-11 02:55:47 +03:00
|
|
|
$name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
|
2009-11-29 18:51:14 +03:00
|
|
|
return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
|
|
|
|
}
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
/**
|
|
|
|
* Add a foreign key constraint
|
|
|
|
*
|
|
|
|
* Name is to be generated by the database itsself.
|
|
|
|
*
|
|
|
|
* @param Table $foreignTable
|
|
|
|
* @param array $localColumns
|
|
|
|
* @param array $foreignColumns
|
|
|
|
* @param array $options
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
|
|
|
|
{
|
|
|
|
return $this->addNamedForeignKeyConstraint(null, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
|
|
|
|
}
|
|
|
|
|
2009-11-29 18:51:14 +03:00
|
|
|
/**
|
|
|
|
* Add a foreign key constraint with a given name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param Table $foreignTable
|
|
|
|
* @param array $localColumns
|
|
|
|
* @param array $foreignColumns
|
|
|
|
* @param array $options
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2009-11-28 04:22:21 +03:00
|
|
|
if ($foreignTable instanceof Table) {
|
|
|
|
$foreignTableName = $foreignTable->getName();
|
|
|
|
|
|
|
|
foreach ($foreignColumnNames AS $columnName) {
|
2009-12-07 19:21:29 +03:00
|
|
|
if ( ! $foreignTable->hasColumn($columnName)) {
|
2009-11-28 04:22:21 +03:00
|
|
|
throw SchemaException::columnDoesNotExist($columnName);
|
|
|
|
}
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
} else {
|
|
|
|
$foreignTableName = $foreignTable;
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
|
|
|
foreach ($localColumnNames AS $columnName) {
|
2009-12-07 19:21:29 +03:00
|
|
|
if ( ! $this->hasColumn($columnName)) {
|
2009-11-27 00:56:08 +03:00
|
|
|
throw SchemaException::columnDoesNotExist($columnName);
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 21:36:46 +03:00
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
$constraint = new ForeignKeyConstraint(
|
2009-11-28 04:22:21 +03:00
|
|
|
$localColumnNames, $foreignTableName, $foreignColumnNames, $name, $options
|
2009-11-27 00:56:08 +03:00
|
|
|
);
|
2009-11-29 18:51:14 +03:00
|
|
|
$this->_addForeignKeyConstraint($constraint);
|
2009-12-06 21:36:46 +03:00
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function addOption($name, $value)
|
|
|
|
{
|
|
|
|
$this->_options[$name] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Column $column
|
|
|
|
*/
|
|
|
|
protected function _addColumn(Column $column)
|
|
|
|
{
|
2009-12-04 01:10:13 +03:00
|
|
|
$columnName = $column->getName();
|
|
|
|
$columnName = strtolower($columnName);
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
if (isset($this->_columns[$columnName])) {
|
2009-12-04 01:10:13 +03:00
|
|
|
throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_columns[$columnName] = $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add index to table
|
|
|
|
*
|
|
|
|
* @param Index $index
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
protected function _addIndex(Index $index)
|
|
|
|
{
|
2009-12-06 21:36:46 +03:00
|
|
|
// check for duplicates
|
|
|
|
$c = new Comparator();
|
|
|
|
foreach ($this->_indexes AS $existingIndex) {
|
|
|
|
if ($c->diffIndex($index, $existingIndex) == false) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-27 00:56:08 +03:00
|
|
|
$indexName = $index->getName();
|
2009-12-04 01:10:13 +03:00
|
|
|
$indexName = strtolower($indexName);
|
2009-11-27 00:56:08 +03:00
|
|
|
|
|
|
|
if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $index->isPrimary())) {
|
|
|
|
throw SchemaException::indexAlreadyExists($indexName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($index->isPrimary()) {
|
|
|
|
$this->_primaryKeyName = $indexName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_indexes[$indexName] = $index;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-02 21:52:21 +03:00
|
|
|
* @param ForeignKeyConstraint $constraint
|
2009-11-27 00:56:08 +03:00
|
|
|
*/
|
2009-12-02 21:52:21 +03:00
|
|
|
protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2009-12-06 21:36:46 +03:00
|
|
|
$constraint->setLocalTable($this);
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
if(strlen($constraint->getName())) {
|
|
|
|
$name = $constraint->getName();
|
|
|
|
} else {
|
|
|
|
$name = $this->_generateIdentifierName(
|
2009-12-11 02:55:47 +03:00
|
|
|
array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
|
2009-12-02 21:52:21 +03:00
|
|
|
);
|
|
|
|
}
|
2009-12-04 01:10:13 +03:00
|
|
|
$name = strtolower($name);
|
2009-12-02 21:52:21 +03:00
|
|
|
|
|
|
|
$this->_fkConstraints[$name] = $constraint;
|
2009-11-29 18:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does Table have a foreign key constraint with the given name?
|
|
|
|
* *
|
|
|
|
* @param string $constraintName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasForeignKey($constraintName)
|
|
|
|
{
|
2009-12-04 01:10:13 +03:00
|
|
|
$constraintName = strtolower($constraintName);
|
2009-11-29 18:51:14 +03:00
|
|
|
return isset($this->_fkConstraints[$constraintName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $constraintName
|
|
|
|
* @return ForeignKeyConstraint
|
|
|
|
*/
|
|
|
|
public function getForeignKey($constraintName)
|
|
|
|
{
|
2009-12-04 01:10:13 +03:00
|
|
|
$constraintName = strtolower($constraintName);
|
2009-11-29 18:51:14 +03:00
|
|
|
if(!$this->hasForeignKey($constraintName)) {
|
|
|
|
throw SchemaException::foreignKeyDoesNotExist($constraintName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_fkConstraints[$constraintName];
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isIdGeneratorIdentity()
|
|
|
|
{
|
|
|
|
return ($this->_idGeneratorType==self::ID_IDENTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function isIdGeneratorSequence()
|
|
|
|
{
|
|
|
|
return ($this->_idGeneratorType==self::ID_SEQUENCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Column[]
|
|
|
|
*/
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return $this->_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this table have a column with the given name?
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasColumn($columnName)
|
|
|
|
{
|
2009-12-01 02:12:26 +03:00
|
|
|
$columnName = strtolower($columnName);
|
2009-11-27 00:56:08 +03:00
|
|
|
return isset($this->_columns[$columnName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a column instance
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @return Column
|
|
|
|
*/
|
|
|
|
public function getColumn($columnName)
|
|
|
|
{
|
2009-12-01 02:12:26 +03:00
|
|
|
$columnName = strtolower($columnName);
|
2009-11-27 00:56:08 +03:00
|
|
|
if (!$this->hasColumn($columnName)) {
|
|
|
|
throw SchemaException::columnDoesNotExist($columnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_columns[$columnName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Index
|
|
|
|
*/
|
|
|
|
public function getPrimaryKey()
|
|
|
|
{
|
|
|
|
return $this->getIndex($this->_primaryKeyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $indexName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasIndex($indexName)
|
|
|
|
{
|
2009-12-04 01:10:13 +03:00
|
|
|
$indexName = strtolower($indexName);
|
2009-11-27 00:56:08 +03:00
|
|
|
return (isset($this->_indexes[$indexName]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $indexName
|
|
|
|
* @return Index
|
|
|
|
*/
|
|
|
|
public function getIndex($indexName)
|
|
|
|
{
|
2009-12-04 01:10:13 +03:00
|
|
|
$indexName = strtolower($indexName);
|
2009-11-27 00:56:08 +03:00
|
|
|
if (!$this->hasIndex($indexName)) {
|
|
|
|
throw SchemaException::indexDoesNotExist($indexName);
|
|
|
|
}
|
|
|
|
return $this->_indexes[$indexName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIndexes()
|
|
|
|
{
|
|
|
|
return $this->_indexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Constraints
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-11-29 18:51:14 +03:00
|
|
|
public function getForeignKeys()
|
2009-11-27 00:56:08 +03:00
|
|
|
{
|
2009-11-29 18:51:14 +03:00
|
|
|
return $this->_fkConstraints;
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasOption($name)
|
|
|
|
{
|
|
|
|
return isset($this->_options[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOption($name)
|
|
|
|
{
|
|
|
|
return $this->_options[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptions()
|
|
|
|
{
|
|
|
|
return $this->_options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Visitor $visitor
|
|
|
|
*/
|
|
|
|
public function visit(Visitor $visitor)
|
|
|
|
{
|
|
|
|
$visitor->acceptTable($this);
|
|
|
|
|
2009-11-28 01:00:43 +03:00
|
|
|
foreach ($this->getColumns() AS $column) {
|
2010-02-07 15:36:30 +03:00
|
|
|
$visitor->acceptColumn($this, $column);
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
|
2009-11-28 01:00:43 +03:00
|
|
|
foreach ($this->getIndexes() AS $index) {
|
2009-11-27 00:56:08 +03:00
|
|
|
$visitor->acceptIndex($this, $index);
|
|
|
|
}
|
|
|
|
|
2009-11-29 18:51:14 +03:00
|
|
|
foreach ($this->getForeignKeys() AS $constraint) {
|
|
|
|
$visitor->acceptForeignKey($this, $constraint);
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-14 01:58:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone of a Table triggers a deep clone of all affected assets
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
foreach ($this->_columns AS $k => $column) {
|
|
|
|
$this->_columns[$k] = clone $column;
|
|
|
|
}
|
|
|
|
foreach ($this->_indexes AS $k => $index) {
|
|
|
|
$this->_indexes[$k] = clone $index;
|
|
|
|
}
|
|
|
|
foreach ($this->_fkConstraints AS $k => $fk) {
|
|
|
|
$this->_fkConstraints[$k] = clone $fk;
|
|
|
|
$this->_fkConstraints[$k]->setLocalTable($this);
|
|
|
|
}
|
|
|
|
}
|
2009-11-27 00:56:08 +03:00
|
|
|
}
|