1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 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\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 = [];
/**
* @var array
*/
protected $addConstraints = [];
/**
* @var array
*/
protected $changeColumns = [];
/**
* @var array
*/
protected $dropColumns = [];
/**
* @var array
*/
protected $dropConstraints = [];
/**
* Specifications for Sql String generation
* @var array
*/
protected $specifications = [
self::TABLE => "ALTER TABLE %1\$s\n",
self::ADD_COLUMNS => [
"%1\$s" => [
[1 => "ADD COLUMN %1\$s,\n", 'combinedby' => ""]
]
],
self::CHANGE_COLUMNS => [
"%1\$s" => [
[2 => "CHANGE COLUMN %1\$s %2\$s,\n", 'combinedby' => ""],
]
],
self::DROP_COLUMNS => [
"%1\$s" => [
[1 => "DROP COLUMN %1\$s,\n", 'combinedby' => ""],
]
],
self::ADD_CONSTRAINTS => [
"%1\$s" => [
[1 => "ADD %1\$s,\n", 'combinedby' => ""],
]
],
self::DROP_CONSTRAINTS => [
"%1\$s" => [
[1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => ""],
]
]
];
/**
* @var string
*/
protected $table = '';
/**
* @param string $table
*/
public function __construct($table = '')
{
($table) ? $this->setTable($table) : null;
}
/**
* @param string $name
* @return self Provides a fluent interface
*/
public function setTable($name)
{
$this->table = $name;
return $this;
}
/**
* @param Column\ColumnInterface $column
* @return self Provides a fluent interface
*/
public function addColumn(Column\ColumnInterface $column)
{
$this->addColumns[] = $column;
return $this;
}
/**
* @param string $name
* @param Column\ColumnInterface $column
* @return self Provides a fluent interface
*/
public function changeColumn($name, Column\ColumnInterface $column)
{
$this->changeColumns[$name] = $column;
return $this;
}
/**
* @param string $name
* @return self Provides a fluent interface
*/
public function dropColumn($name)
{
$this->dropColumns[] = $name;
return $this;
}
/**
* @param string $name
* @return self Provides a fluent interface
*/
public function dropConstraint($name)
{
$this->dropConstraints[] = $name;
return $this;
}
/**
* @param Constraint\ConstraintInterface $constraint
* @return self Provides a fluent interface
*/
public function addConstraint(Constraint\ConstraintInterface $constraint)
{
$this->addConstraints[] = $constraint;
return $this;
}
/**
* @param string|null $key
* @return array
*/
public function getRawState($key = null)
{
$rawState = [
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;
}
protected function processTable(PlatformInterface $adapterPlatform = null)
{
return [$adapterPlatform->quoteIdentifier($this->table)];
}
protected function processAddColumns(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->addColumns as $column) {
$sqls[] = $this->processExpression($column, $adapterPlatform);
}
return [$sqls];
}
protected function processChangeColumns(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->changeColumns as $name => $column) {
$sqls[] = [
$adapterPlatform->quoteIdentifier($name),
$this->processExpression($column, $adapterPlatform)
];
}
return [$sqls];
}
protected function processDropColumns(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->dropColumns as $column) {
$sqls[] = $adapterPlatform->quoteIdentifier($column);
}
return [$sqls];
}
protected function processAddConstraints(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->addConstraints as $constraint) {
$sqls[] = $this->processExpression($constraint, $adapterPlatform);
}
return [$sqls];
}
protected function processDropConstraints(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->dropConstraints as $constraint) {
$sqls[] = $adapterPlatform->quoteIdentifier($constraint);
}
return [$sqls];
}
}