ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
Table.php
1 <?php
31 {
38  public function createTable(ZendDbSchema_Db_Schema_Table $schema)
39  {
40  $name = $schema->getName();
41 
42  $engine = $schema->engine;
43  if ($engine) {
44  $engine = "ENGINE={$engine}";
45  }
46 
47  $charset = $schema->charset;
48  if ($charset) {
49  $charset = "DEFAULT CHARSET={$charset}";
50  }
51 
52  $query = array();
53  foreach ($schema->columns() as $column) {
54  $query[] = $column->toSql();
55  }
56 
57  $query = array_filter($query);
58  $query = join(', ' . PHP_EOL, $query);
59 
60  $query = "DROP TABLE IF EXISTS `{$name}`;" . PHP_EOL
61  . "CREATE TABLE `{$name}` (" . PHP_EOL
62  . $query . PHP_EOL
63  . ") {$engine} {$charset}";
64 
65  return $query;
66  }
67 
74  public function alterTable(ZendDbSchema_Db_Schema_Table $schema)
75  {
76  if (!$schema->isExist()) {
77  return null;
78  }
79 
80  $props = array();
81  if ($schema->isDirty('engine')) {
82  $props[] = "ENGINE={$schema->engine}";
83  }
84 
85  if ($schema->isDirty('charset')) {
86  $props[] = "DEFAULT CHARSET={$schema->charset}";
87  }
88 
89  $props = array_filter($props);
90  $props = join(', ' . PHP_EOL, $props);
91 
92  $query = array();
93 
94  $name = $schema->getName();
95  if ($schema->isDirty(ZendDbSchema_Db_Schema_Table::NAME_KEY)) {
96  $oldName = $schema->getOriginName();
97  if ($oldName && $oldName != $name) {
98  $query[] = "RENAME TABLE `{$oldName}` TO `{$name}`";
99  }
100  }
101  if ($props) {
102  $query[] = "ALTER TABLE `{$name}` {$props}";
103  }
104 
105  return join(';' . PHP_EOL, $query);
106  }
107 
114  public function dropTable(ZendDbSchema_Db_Schema_Table $schema)
115  {
116  if (!$schema->isExist()) {
117  return null;
118  }
119  return "DROP TABLE IF EXISTS {$schema->getOriginName()}";
120  }
121 
129  {
130  $query = array();
131 
132  $query[] = $schema->type . "({$schema->length})";
133 
134  if (!$schema->isNullable()) {
135  $query[] = "NOT NULL";
136  } elseif (null === $schema->default) {
137 
138  $query[] = "DEFAULT NULL";
139  }
140 
141  if (null !== $schema->default) {
142  $query[] = "DEFAULT {$schema->default}";
143  }
144 
145  $query[] = "COMMENT '{$schema->comment}'";
146 
147  if (false === $schema->isSigned()) {
148  $query[] = "UNSIGNED";
149  } elseif (true === $schema->isSigned()) {
150  $query[] = "SIGNED";
151  }
152 
153  if ($schema->autoincrement) {
154  $query[] = "AUTO_INCREMENT";
155  }
156 
157  if ($schema->after) {
158  $query[] = "AFTER {$schema->after}";
159  }
160 
161  return join(' ', $query);
162  }
163 
171  {
172  $name = $schema->getName();
173  return "`{$name}`" . ' ' . $this->_columnSql($schema);
174  }
175 
183  {
184  if (!$schema->getTable()->isExist() || !$schema->isDirty()) {
185  return null;
186  }
187  $name = $schema->getName();
188 
189  $query = array();
190  $query[] = "ALTER TABLE `{$schema->getTable()->getName()}`";
191 
192  if ($schema->isExist()) {
193  $oldName = $schema->getOriginName();
194  if ($oldName && $oldName != $name) {
195  $oldName = $schema->getOriginName();
196  $query[] = "CHANGE `{$name}` `{$oldName}`";
197  } else {
198  $query[] = "MODIFY `{$name}`";
199  }
200  } else {
201  $query[] = "ADD `{$name}`";
202  }
203 
204  return join(' ', $query) . ' ' . $this->_columnSql($schema);
205  }
206 
214  {
215  $name = $schema->getName();
216  $table = $schema->getTable()->getName();
217 
218  return "ALTER TABLE {$table} DROP COLUMN {$name}";
219  }
220 
228  {
229  $name = $schema->getName();
230  $table = $schema->getTable()->getName();
231 
232  $columns = "";
233  if ($schema->getColumns()) {
234  $columns = "(" . join(', ', $schema->getColumns()) . ")";
235  }
236  $type = strtoupper($schema->type);
237 
238  $query = array();
239  if ($schema->isExist()) {
240  $query[] = $this->dropIndex($schema);
241  }
242  $query[] = "CREATE {$type} INDEX `{$name}` ON `{$table}` {$columns}";
243 
244  return join(';' . PHP_EOL, $query);
245  }
246 
254  {
255  return $this->createIndex($schema);
256  }
257 
265  {
266  $name = $schema->getName();
267  $table = $schema->getTable()->getName();
268 
269  $query = "DROP INDEX IF EXISTS `{$name}` ON `{$table}`";
270  return $query;
271  }
272 
280  {
281  $columns = join(', ', $schema->getColumns());
282  $table = $schema->getTable()->getName();
283 
284  $query = "ALTER TABLE `{$table}` ADD PRIMARY KEY ({$columns})";
285 
286  return $query;
287  }
288 
296  {
297  $query = array();
298  $query[] = $this->dropPrimaryKey($schema);
299  $query[] = $this->addPrimaryKey($schema);
300 
301  return join(';' . PHP_EOL, $query);
302  }
303 
311  {
312  $table = $schema->getTable()->getName();
313 
314  $query = "ALTER TABLE `{$table}` DROP PRIMARY KEY";
315  return $query;
316  }
317 
325  {
326  $query = array();
327 
328  if ($name = $schema->getOriginName()) {
329  $query[] = "CONSTRAINT {$name}";
330  }
331 
332  $columns = $schema->getColumns();
333 
334  $refColumns = join('`,`', array_values($columns));
335  $columns = join('`,`', array_keys($columns));
336  $refTable = $schema->getReferences();
337 
338  $query[] = " FOREIGN KEY (`{$columns}`) REFERENCES `{$refTable}` (`{$refColumns}`)";
339 
340  if ($delete = $schema->getOnDelete()) {
341  $query[] = " ON DELETE {$delete}";
342  }
343  if ($update = $schema->getOnUpdate()) {
344  $query[] = " ON UPDATE {$update}";
345  }
346 
347  $table = $schema->getTable()->getName();
348 
349  return "ALTER TABLE `{$table}` ADD " . join(' ', $query);
350  }
351 
359  {
360  $query = array();
361  if ($schema->isExist()) {
362  $query[] = $this->dropForeignKey($schema);
363  }
364  $query[] = $this->addForeignKey($schema);
365  return join(';' . PHP_EOL, $query);
366  }
367 
375  {
376  $table = $schema->getTable()->getName();
377  $name = $schema->getOriginName();
378 
379  $query = "ALTER TABLE `{$table}` DROP FOREIGN KEY {$name}";
380  return $query;
381  }
382 }