123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- <?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;
- use Doctrine\DBAL\Schema\Visitor\Visitor;
- use Doctrine\DBAL\DBALException;
-
- /**
- * 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 string
- */
- protected $_name = null;
-
- /**
- * @var array
- */
- protected $_columns = array();
-
- /**
- * @var array
- */
- protected $_indexes = array();
-
- /**
- * @var string
- */
- protected $_primaryKeyName = false;
-
- /**
- * @var array
- */
- protected $_fkConstraints = array();
-
- /**
- * @var array
- */
- protected $_options = array();
-
- /**
- * @var SchemaConfig
- */
- protected $_schemaConfig = null;
-
- /**
- *
- * @param string $tableName
- * @param array $columns
- * @param array $indexes
- * @param array $fkConstraints
- * @param int $idGeneratorType
- * @param array $options
- */
- public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType = 0, array $options=array())
- {
- if (strlen($tableName) == 0) {
- throw DBALException::invalidTableName($tableName);
- }
-
- $this->_setName($tableName);
- $this->_idGeneratorType = $idGeneratorType;
-
- foreach ($columns AS $column) {
- $this->_addColumn($column);
- }
-
- foreach ($indexes AS $idx) {
- $this->_addIndex($idx);
- }
-
- foreach ($fkConstraints AS $constraint) {
- $this->_addForeignKeyConstraint($constraint);
- }
-
- $this->_options = $options;
- }
-
- /**
- * @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;
- }
- }
-
- /**
- * Set Primary Key
- *
- * @param array $columns
- * @param string $indexName
- * @return Table
- */
- public function setPrimaryKey(array $columns, $indexName = false)
- {
- $primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true);
-
- foreach ($columns AS $columnName) {
- $column = $this->getColumn($columnName);
- $column->setNotnull(true);
- }
-
- return $primaryKey;
- }
-
- /**
- * @param array $columnNames
- * @param string $indexName
- * @return Table
- */
- public function addIndex(array $columnNames, $indexName = null)
- {
- if($indexName == null) {
- $indexName = $this->_generateIdentifierName(
- array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
- );
- }
-
- return $this->_createIndex($columnNames, $indexName, false, false);
- }
-
- /**
- *
- * @param array $columnNames
- * @param string $indexName
- * @return Table
- */
- public function addUniqueIndex(array $columnNames, $indexName = null)
- {
- if ($indexName == null) {
- $indexName = $this->_generateIdentifierName(
- array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
- );
- }
-
- return $this->_createIndex($columnNames, $indexName, true, false);
- }
-
- /**
- * 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) {
- /* @var $index Index */
- if ($index->spansColumns($columnsNames)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- *
- * @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);
- }
-
- foreach ($columnNames AS $columnName => $indexColOptions) {
- if (is_numeric($columnName) && is_string($indexColOptions)) {
- $columnName = $indexColOptions;
- }
-
- if ( ! $this->hasColumn($columnName)) {
- throw SchemaException::columnDoesNotExist($columnName, $this->_name);
- }
- }
- $this->_addIndex(new Index($indexName, $columnNames, $isUnique, $isPrimary));
- return $this;
- }
-
- /**
- * @param string $columnName
- * @param string $columnType
- * @param array $options
- * @return Column
- */
- public function addColumn($columnName, $typeName, array $options=array())
- {
- $column = new Column($columnName, Type::getType($typeName), $options);
-
- $this->_addColumn($column);
- return $column;
- }
-
- /**
- * 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)
- {
- $columnName = strtolower($columnName);
- $column = $this->getColumn($columnName);
- unset($this->_columns[$columnName]);
- return $this;
- }
-
-
- /**
- * Add a foreign key constraint
- *
- * Name is inferred from the local columns
- *
- * @param Table $foreignTable
- * @param array $localColumns
- * @param array $foreignColumns
- * @param array $options
- * @param string $constraintName
- * @return Table
- */
- public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null)
- {
- $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
- return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
- }
-
- /**
- * Add a foreign key constraint
- *
- * Name is to be generated by the database itsself.
- *
- * @deprecated Use {@link addForeignKeyConstraint}
- * @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->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
- }
-
- /**
- * Add a foreign key constraint with a given name
- *
- * @deprecated Use {@link addForeignKeyConstraint}
- * @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())
- {
- if ($foreignTable instanceof Table) {
- $foreignTableName = $foreignTable->getName();
-
- foreach ($foreignColumnNames AS $columnName) {
- if ( ! $foreignTable->hasColumn($columnName)) {
- throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
- }
- }
- } else {
- $foreignTableName = $foreignTable;
- }
-
- foreach ($localColumnNames AS $columnName) {
- if ( ! $this->hasColumn($columnName)) {
- throw SchemaException::columnDoesNotExist($columnName, $this->_name);
- }
- }
-
- $constraint = new ForeignKeyConstraint(
- $localColumnNames, $foreignTableName, $foreignColumnNames, $name, $options
- );
- $this->_addForeignKeyConstraint($constraint);
-
- 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)
- {
- $columnName = $column->getName();
- $columnName = strtolower($columnName);
-
- if (isset($this->_columns[$columnName])) {
- throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
- }
-
- $this->_columns[$columnName] = $column;
- }
-
- /**
- * Add index to table
- *
- * @param Index $indexCandidate
- * @return Table
- */
- protected function _addIndex(Index $indexCandidate)
- {
- // check for duplicates
- foreach ($this->_indexes AS $existingIndex) {
- if ($indexCandidate->isFullfilledBy($existingIndex)) {
- return $this;
- }
- }
-
- $indexName = $indexCandidate->getName();
- $indexName = strtolower($indexName);
-
- if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
- throw SchemaException::indexAlreadyExists($indexName, $this->_name);
- }
-
- // remove overruled indexes
- foreach ($this->_indexes AS $idxKey => $existingIndex) {
- if ($indexCandidate->overrules($existingIndex)) {
- unset($this->_indexes[$idxKey]);
- }
- }
-
- if ($indexCandidate->isPrimary()) {
- $this->_primaryKeyName = $indexName;
- }
-
- $this->_indexes[$indexName] = $indexCandidate;
- return $this;
- }
-
- /**
- * @param ForeignKeyConstraint $constraint
- */
- protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
- {
- $constraint->setLocalTable($this);
-
- if(strlen($constraint->getName())) {
- $name = $constraint->getName();
- } else {
- $name = $this->_generateIdentifierName(
- array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
- );
- }
- $name = strtolower($name);
-
- $this->_fkConstraints[$name] = $constraint;
- // add an explicit index on the foreign key columns. If there is already an index that fullfils this requirements drop the request.
- // In the case of __construct calling this method during hydration from schema-details all the explicitly added indexes
- // lead to duplicates. This creates compuation overhead in this case, however no duplicate indexes are ever added (based on columns).
- $this->addIndex($constraint->getColumns());
- }
-
- /**
- * Does Table have a foreign key constraint with the given name?
- * *
- * @param string $constraintName
- * @return bool
- */
- public function hasForeignKey($constraintName)
- {
- $constraintName = strtolower($constraintName);
- return isset($this->_fkConstraints[$constraintName]);
- }
-
- /**
- * @param string $constraintName
- * @return ForeignKeyConstraint
- */
- public function getForeignKey($constraintName)
- {
- $constraintName = strtolower($constraintName);
- if(!$this->hasForeignKey($constraintName)) {
- throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
- }
-
- return $this->_fkConstraints[$constraintName];
- }
-
- /**
- * @return Column[]
- */
- public function getColumns()
- {
- $columns = $this->_columns;
-
- $pkCols = array();
- $fkCols = array();
-
- if ($this->hasIndex($this->_primaryKeyName)) {
- $pkCols = $this->getPrimaryKey()->getColumns();
- }
- foreach ($this->getForeignKeys() AS $fk) {
- /* @var $fk ForeignKeyConstraint */
- $fkCols = array_merge($fkCols, $fk->getColumns());
- }
- $colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));
-
- uksort($columns, function($a, $b) use($colNames) {
- return (array_search($a, $colNames) >= array_search($b, $colNames));
- });
- return $columns;
- }
-
-
- /**
- * Does this table have a column with the given name?
- *
- * @param string $columnName
- * @return bool
- */
- public function hasColumn($columnName)
- {
- $columnName = $this->trimQuotes(strtolower($columnName));
- return isset($this->_columns[$columnName]);
- }
-
- /**
- * Get a column instance
- *
- * @param string $columnName
- * @return Column
- */
- public function getColumn($columnName)
- {
- $columnName = strtolower($this->trimQuotes($columnName));
- if (!$this->hasColumn($columnName)) {
- throw SchemaException::columnDoesNotExist($columnName, $this->_name);
- }
-
- return $this->_columns[$columnName];
- }
-
- /**
- * @return Index
- */
- public function getPrimaryKey()
- {
- return $this->getIndex($this->_primaryKeyName);
- }
-
- /**
- * Check if this table has a primary key.
- *
- * @return bool
- */
- public function hasPrimaryKey()
- {
- return ($this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName));
- }
-
- /**
- * @param string $indexName
- * @return bool
- */
- public function hasIndex($indexName)
- {
- $indexName = strtolower($indexName);
- return (isset($this->_indexes[$indexName]));
- }
-
- /**
- * @param string $indexName
- * @return Index
- */
- public function getIndex($indexName)
- {
- $indexName = strtolower($indexName);
- if (!$this->hasIndex($indexName)) {
- throw SchemaException::indexDoesNotExist($indexName, $this->_name);
- }
- return $this->_indexes[$indexName];
- }
-
- /**
- * @return array
- */
- public function getIndexes()
- {
- return $this->_indexes;
- }
-
- /**
- * Get Constraints
- *
- * @return array
- */
- public function getForeignKeys()
- {
- return $this->_fkConstraints;
- }
-
- 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);
-
- foreach ($this->getColumns() AS $column) {
- $visitor->acceptColumn($this, $column);
- }
-
- foreach ($this->getIndexes() AS $index) {
- $visitor->acceptIndex($this, $index);
- }
-
- foreach ($this->getForeignKeys() AS $constraint) {
- $visitor->acceptForeignKey($this, $constraint);
- }
- }
-
- /**
- * 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);
- }
- }
- }
|