Source of file AlterTable.php
Size: 7,029 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Db/Sql/Ddl/AlterTable.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Db\Sql\Ddl; use Zend\Db\Adapter\Platform\PlatformInterface; use Zend\Db\Adapter\Platform\Sql92 as AdapterSql92Platform; use Zend\Db\Sql\AbstractSql; class AlterTable extends AbstractSql implements SqlInterface { const ADD_COLUMNS = 'addColumns'; const ADD_CONSTRAINTS = 'addConstraints'; const CHANGE_COLUMNS = 'changeColumns'; const DROP_COLUMNS = 'dropColumns'; const DROP_CONSTRAINTS = 'dropConstraints'; const TABLE = 'table'; /** * @var array */ protected $addColumns = array(); /** * @var array */ protected $addConstraints = array(); /** * @var array */ protected $changeColumns = array(); /** * @var array */ protected $dropColumns = array(); /** * @var array */ protected $dropConstraints = array(); /** * Specifications for Sql String generation * @var array */ protected $specifications = array( self::TABLE => "ALTER TABLE %1\$s\n", self::ADD_COLUMNS => array( "%1\$s" => array( array(1 => 'ADD COLUMN %1$s', 'combinedby' => ",\n") ) ), self::CHANGE_COLUMNS => array( "%1\$s" => array( array(2 => 'CHANGE COLUMN %1$s %2$s', 'combinedby' => ",\n"), ) ), self::DROP_COLUMNS => array( "%1\$s" => array( array(1 => 'DROP COLUMN %1$s', 'combinedby' => ",\n"), ) ), self::ADD_CONSTRAINTS => array( "%1\$s" => array( array(1 => 'ADD %1$s', 'combinedby' => ",\n"), ) ), self::DROP_CONSTRAINTS => array( "%1\$s" => array( array(1 => 'DROP CONSTRAINT %1$s', 'combinedby' => ",\n"), ) ) ); /** * @var string */ protected $table = ''; /** * @param string $table */ public function __construct($table = '') { ($table) ? $this->setTable($table) : null; } /** * @param string $name * @return self */ public function setTable($name) { $this->table = $name; return $this; } /** * @param Column\ColumnInterface $column * @return self */ public function addColumn(Column\ColumnInterface $column) { $this->addColumns[] = $column; return $this; } /** * @param string $name * @param Column\ColumnInterface $column * @return self */ public function changeColumn($name, Column\ColumnInterface $column) { $this->changeColumns[$name] = $column; return $this; } /** * @param string $name * @return self */ public function dropColumn($name) { $this->dropColumns[] = $name; return $this; } /** * @param string $name * @return self */ public function dropConstraint($name) { $this->dropConstraints[] = $name; return $this; } /** * @param Constraint\ConstraintInterface $constraint * @return self */ public function addConstraint(Constraint\ConstraintInterface $constraint) { $this->addConstraints[] = $constraint; return $this; } /** * @param string|null $key * @return array */ public function getRawState($key = null) { $rawState = array( self::TABLE => $this->table, self::ADD_COLUMNS => $this->addColumns, self::DROP_COLUMNS => $this->dropColumns, self::CHANGE_COLUMNS => $this->changeColumns, self::ADD_CONSTRAINTS => $this->addConstraints, self::DROP_CONSTRAINTS => $this->dropConstraints, ); return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } /** * @param PlatformInterface $adapterPlatform * @return string */ public function getSqlString(PlatformInterface $adapterPlatform = null) { // get platform, or create default $adapterPlatform = ($adapterPlatform) ?: new AdapterSql92Platform; $sqls = array(); $parameters = array(); foreach ($this->specifications as $name => $specification) { $parameters[$name] = $this->{'process' . $name}($adapterPlatform, null, null, $sqls, $parameters); if ($specification && is_array($parameters[$name]) && ($parameters[$name] != array(array()))) { $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]); } if (stripos($name, 'table') === false && $parameters[$name] !== array(array())) { $sqls[] = ",\n"; } } // remove last ,\n array_pop($sqls); $sql = implode('', $sqls); return $sql; } protected function processTable(PlatformInterface $adapterPlatform = null) { return array($adapterPlatform->quoteIdentifier($this->table)); } protected function processAddColumns(PlatformInterface $adapterPlatform = null) { $sqls = array(); foreach ($this->addColumns as $column) { $sqls[] = $this->processExpression($column, $adapterPlatform)->getSql(); } return array($sqls); } protected function processChangeColumns(PlatformInterface $adapterPlatform = null) { $sqls = array(); foreach ($this->changeColumns as $name => $column) { $sqls[] = array( $adapterPlatform->quoteIdentifier($name), $this->processExpression($column, $adapterPlatform)->getSql() ); } return array($sqls); } protected function processDropColumns(PlatformInterface $adapterPlatform = null) { $sqls = array(); foreach ($this->dropColumns as $column) { $sqls[] = $adapterPlatform->quoteIdentifier($column); } return array($sqls); } protected function processAddConstraints(PlatformInterface $adapterPlatform = null) { $sqls = array(); foreach ($this->addConstraints as $constraint) { $sqls[] = $this->processExpression($constraint, $adapterPlatform); } return array($sqls); } protected function processDropConstraints(PlatformInterface $adapterPlatform = null) { $sqls = array(); foreach ($this->dropConstraints as $constraint) { $sqls[] = $adapterPlatform->quoteIdentifier($constraint); } return array($sqls); } } |