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  $props = array();
41  if ($schema->inherits) {
42  //$props [] = "INHERITS {$schema->inherits}";
43  }
44  /*
45  if (true === $schema->oids) {
46  $props [] = "WITH OIDS";
47  } elseif (false === $schema->oids) {
48  $props [] = "WITHOUT OIDS";
49  }
50  */
51  if ($schema->commit) {
52  $props [] = "ON COMMIT {$schema->commit}";
53  }
54  if ($schema->tablespace) {
55  $props [] = "TABLESPACE {$schema->tablespace}";
56  }
57 
58  $columns = array();
59  foreach ($schema->columns() as $column) {
60  $columns[] = $column->toSql();
61  }
62 
63  $columns = array_filter($columns);
64  $columns = join(', ' . PHP_EOL, $columns);
65 
66  $name = $schema->getName();
67  $query = array();
68  $query[] = "DROP TABLE IF EXISTS {$name}";
69  $query[] = "CREATE TABLE {$name} (" . PHP_EOL
70  . $columns . PHP_EOL
71  . ")" . PHP_EOL
72  . join(' ' . PHP_EOL, $props);
73 
74  return join(';' . PHP_EOL, $query);
75  }
76 
83  public function alterTable(ZendDbSchema_Db_Schema_Table $schema)
84  {
85  if (!$schema->isExist()) {
86  return null;
87  }
88 
89  $query = array();
90 
91  $name = $schema->getName();
92  if ($schema->isDirty(ZendDbSchema_Db_Schema_Table::NAME_KEY)) {
93  $oldName = $schema->getOriginName();
94  if ($oldName && $oldName != $name) {
95  $query[] = "ALTER TABLE {$oldName} RENAME TO {$name}";
96  }
97  }
98 
99  if ($schema->isDirty('owner')) {
100  $query[] = "ALTER TABLE {$name} OWNER TO {$schema->owner}";
101  }
102 
103  if ($schema->isDirty('tablespace')) {
104  $query[] = "ALTER TABLE {$name} SET TABLESPACE {$schema->tablespace}";
105  }
106 
107  if ($schema->isDirty('schema')) {
108  // $query[] = "ALTER TABLE {$name} SET SCHEMA {$schema->schema}";
109  }
110 
111  return join(';' . PHP_EOL, $query);
112  }
113 
120  public function dropTable(ZendDbSchema_Db_Schema_Table $schema)
121  {
122  if (!$schema->isExist()) {
123  return null;
124  }
125  return "DROP TABLE IF EXISTS {$schema->getOriginName()}";
126  }
127 
135  {
136  $query = array();
137 
138  $query[] = $schema->type;
139 
140  if ($schema->getLength()) {
141  $query[] = "({$schema->getLength()})";
142  }
143 
144  if (!$schema->isNullable()) {
145  $query[] = "NOT NULL";
146  } elseif (null === $schema->default) {
147  $query[] = "DEFAULT NULL";
148  }
149 
150  if (null !== $schema->default) {
151  $query[] = "DEFAULT {$schema->default}";
152  }
153  return join(' ', $query);
154  }
155 
163  {
164  $name = $schema->getName();
165  return "{$name}" . ' ' . $this->_columnSql($schema);
166  }
167 
175  {
176  if (!$schema->getTable()->isExist() || !$schema->isDirty()) {
177  return null;
178  }
179  $name = $schema->getName();
180 
181  $query = array();
182  $prefix = "ALTER TABLE {$schema->getTable()->getName()}";
183 
184  if ($schema->isExist()) {
185  $oldName = $schema->getOriginName();
186  if ($oldName && $oldName != $name) {
187  $oldName = $schema->getOriginName();
188  $query[] = $prefix . " RENAME {$oldName} TO {$name}";
189  } elseif ($schema->isDirty('type') || $schema->isDirty('length')) {
190  $query[] = $prefix . " ALTER {$name} " . $this->_columnSql($schema);
191  }
192  } else {
193  $query[] = $prefix . " ADD {$name} " . $this->_columnSql($schema);
194  }
195 
196  return join(' ', $query);
197  }
198 
206  {
207  $name = $schema->getName();
208  $table = $schema->getTable()->getName();
209 
210  return "ALTER TABLE {$table} DROP COLUMN {$name}";
211  }
212 
220  {
221  $name = $schema->getName();
222  $table = $schema->getTable()->getName();
223 
224  $columns = "";
225  if ($schema->getColumns()) {
226  $columns = "(" . join(', ', $schema->getColumns()) . ")";
227  }
228  $type = strtoupper($schema->type);
229 
230  $query = array();
231  if ($schema->isExist()) {
232  $query[] = $this->dropIndex($schema);
233  }
234  $query[] = "CREATE {$type} INDEX {$name} ON {$table} {$columns}";
235 
236  return join(';' . PHP_EOL, $query);
237  }
238 
246  {
247  return $this->createIndex($schema);
248  }
249 
257  {
258  $name = $schema->getName();
259  $table = $schema->getTable()->getName();
260 
261  $query = "DROP INDEX IF EXISTS {$name} ON {$table}";
262  return $query;
263  }
264 
272  {
273  $columns = join(', ', $schema->getColumns());
274  $table = $schema->getTable()->getName();
275 
276  $query = "ALTER TABLE {$table} ADD PRIMARY KEY ({$columns})";
277 
278  return $query;
279  }
280 
288  {
289  $table = $schema->getTable()->getName();
290 
291  $query = "ALTER TABLE {$table} DROP PRIMARY KEY";
292  return $query;
293  }
294 
302  {
303  $query = array();
304 
305  if ($name = $schema->getOriginName()) {
306  $query[] = "CONSTRAINT {$name}";
307  }
308 
309  $columns = $schema->getColumns();
310 
311  $refColumns = join(',', array_values($columns));
312  $columns = join(',', array_keys($columns));
313  $refTable = $schema->getReferences();
314 
315  $query[] = " FOREIGN KEY ({$columns}) REFERENCES {$refTable} ({$refColumns})";
316 
317  if ($delete = $schema->getOnDelete()) {
318  $query[] = " ON DELETE {$delete}";
319  }
320  if ($update = $schema->getOnUpdate()) {
321  $query[] = " ON UPDATE {$update}";
322  }
323 
324  $table = $schema->getTable()->getName();
325 
326  return "ALTER TABLE {$table} ADD " . join(' ', $query);
327  }
328 
336  {
337  $query = array();
338  if ($schema->isExist()) {
339  $query[] = $this->dropForeignKey($schema);
340  }
341  $query[] = $this->addForeignKey($schema);
342  return join(';' . PHP_EOL, $query);
343  }
344 
352  {
353  $table = $schema->getTable()->getName();
354  $name = $schema->getOriginName();
355 
356  $query = "ALTER TABLE {$table} DROP FOREIGN KEY {$name}";
357  return $query;
358  }
359 }