3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql\Ddl
;
12 use Zend\Db\Adapter\Platform\PlatformInterface
;
13 use Zend\Db\Adapter\Platform\Sql92
as AdapterSql92Platform
;
14 use Zend\Db\Sql\AbstractSql
;
16 class AlterTable
extends AbstractSql
implements SqlInterface
18 const ADD_COLUMNS
= 'addColumns';
19 const ADD_CONSTRAINTS
= 'addConstraints';
20 const CHANGE_COLUMNS
= 'changeColumns';
21 const DROP_COLUMNS
= 'dropColumns';
22 const DROP_CONSTRAINTS
= 'dropConstraints';
23 const TABLE
= 'table';
28 protected $addColumns = array();
33 protected $addConstraints = array();
38 protected $changeColumns = array();
43 protected $dropColumns = array();
48 protected $dropConstraints = array();
51 * Specifications for Sql String generation
54 protected $specifications = array(
55 self
::TABLE
=> "ALTER TABLE %1\$s\n",
56 self
::ADD_COLUMNS
=> array(
58 array(1 => 'ADD COLUMN %1$s', 'combinedby' => ",\n")
61 self
::CHANGE_COLUMNS
=> array(
63 array(2 => 'CHANGE COLUMN %1$s %2$s', 'combinedby' => ",\n"),
66 self
::DROP_COLUMNS
=> array(
68 array(1 => 'DROP COLUMN %1$s', 'combinedby' => ",\n"),
71 self
::ADD_CONSTRAINTS
=> array(
73 array(1 => 'ADD %1$s', 'combinedby' => ",\n"),
76 self
::DROP_CONSTRAINTS
=> array(
78 array(1 => 'DROP CONSTRAINT %1$s', 'combinedby' => ",\n"),
86 protected $table = '';
89 * @param string $table
91 public function __construct($table = '')
93 ($table) ?
$this->setTable($table) : null;
100 public function setTable($name)
102 $this->table
= $name;
108 * @param Column\ColumnInterface $column
111 public function addColumn(Column\ColumnInterface
$column)
113 $this->addColumns
[] = $column;
119 * @param string $name
120 * @param Column\ColumnInterface $column
123 public function changeColumn($name, Column\ColumnInterface
$column)
125 $this->changeColumns
[$name] = $column;
131 * @param string $name
134 public function dropColumn($name)
136 $this->dropColumns
[] = $name;
142 * @param string $name
145 public function dropConstraint($name)
147 $this->dropConstraints
[] = $name;
153 * @param Constraint\ConstraintInterface $constraint
156 public function addConstraint(Constraint\ConstraintInterface
$constraint)
158 $this->addConstraints
[] = $constraint;
164 * @param string|null $key
167 public function getRawState($key = null)
170 self
::TABLE
=> $this->table
,
171 self
::ADD_COLUMNS
=> $this->addColumns
,
172 self
::DROP_COLUMNS
=> $this->dropColumns
,
173 self
::CHANGE_COLUMNS
=> $this->changeColumns
,
174 self
::ADD_CONSTRAINTS
=> $this->addConstraints
,
175 self
::DROP_CONSTRAINTS
=> $this->dropConstraints
,
178 return (isset($key) && array_key_exists($key, $rawState)) ?
$rawState[$key] : $rawState;
182 * @param PlatformInterface $adapterPlatform
185 public function getSqlString(PlatformInterface
$adapterPlatform = null)
187 // get platform, or create default
188 $adapterPlatform = ($adapterPlatform) ?
: new AdapterSql92Platform
;
191 $parameters = array();
193 foreach ($this->specifications
as $name => $specification) {
194 $parameters[$name] = $this->{'process' . $name}($adapterPlatform, null, null, $sqls, $parameters);
195 if ($specification && is_array($parameters[$name]) && ($parameters[$name] != array(array()))) {
196 $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
198 if (stripos($name, 'table') === false && $parameters[$name] !== array(array())) {
206 $sql = implode('', $sqls);
211 protected function processTable(PlatformInterface
$adapterPlatform = null)
213 return array($adapterPlatform->quoteIdentifier($this->table
));
216 protected function processAddColumns(PlatformInterface
$adapterPlatform = null)
219 foreach ($this->addColumns
as $column) {
220 $sqls[] = $this->processExpression($column, $adapterPlatform)->getSql();
226 protected function processChangeColumns(PlatformInterface
$adapterPlatform = null)
229 foreach ($this->changeColumns
as $name => $column) {
231 $adapterPlatform->quoteIdentifier($name),
232 $this->processExpression($column, $adapterPlatform)->getSql()
239 protected function processDropColumns(PlatformInterface
$adapterPlatform = null)
242 foreach ($this->dropColumns
as $column) {
243 $sqls[] = $adapterPlatform->quoteIdentifier($column);
249 protected function processAddConstraints(PlatformInterface
$adapterPlatform = null)
252 foreach ($this->addConstraints
as $constraint) {
253 $sqls[] = $this->processExpression($constraint, $adapterPlatform);
259 protected function processDropConstraints(PlatformInterface
$adapterPlatform = null)
262 foreach ($this->dropConstraints
as $constraint) {
263 $sqls[] = $adapterPlatform->quoteIdentifier($constraint);