1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
class Migration_Driver_Mysql_Table extends Migration_Driver_Table |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
public function params(array $columns = NULL, array $options = NULL) |
7
|
|
|
{ |
8
|
|
|
$columns = (array) $columns; |
9
|
|
|
$this->options = Arr::get( (array) $options, 'options'); |
|
|
|
|
10
|
|
|
|
11
|
|
|
if (Arr::get((array) $options, 'id', TRUE)) |
12
|
|
|
{ |
13
|
|
|
$columns = array_merge(array('id' => 'primary_key'), $columns); |
14
|
1 |
|
} |
15
|
|
|
|
16
|
1 |
|
foreach ($columns as $column_name => $params) |
17
|
1 |
|
{ |
18
|
|
|
$this->columns[$column_name] = $this->driver->column($column_name)->params($params); |
19
|
1 |
|
} |
20
|
1 |
|
|
21
|
1 |
|
return $this; |
22
|
1 |
|
} |
23
|
|
|
|
24
|
1 |
|
public function load() |
25
|
|
|
{ |
26
|
1 |
|
$this->columns = array(); |
27
|
1 |
|
$this->options = array(); |
28
|
|
|
$this->keys = array(); |
|
|
|
|
29
|
1 |
|
|
30
|
|
|
$result = $this->driver->pdo->query("SHOW TABLE STATUS LIKE '{$this->name}'"); |
|
|
|
|
31
|
|
|
|
32
|
1 |
|
if ($result->rowCount() !== 1) |
33
|
|
|
{ |
34
|
1 |
|
throw new Migration_Exception(":table does not exist", array(':table' => $this->name)); |
35
|
1 |
|
} |
36
|
1 |
|
|
37
|
|
|
$table->options[] = 'ENGINE='.$result->fetchObject()->Engine; |
|
|
|
|
38
|
1 |
|
|
39
|
|
|
$fields_reuslt = $this->driver->pdo->query("SHOW COLUMNS FROM `{$this->name}`"); |
40
|
1 |
|
|
41
|
1 |
|
while($result = $fields_reuslt->fetchObject()) |
42
|
1 |
|
{ |
43
|
|
|
$this->columns[$result->Field] = $this->driver->column($result->Field)->load($result); |
44
|
|
|
} |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function sql() |
49
|
|
|
{ |
50
|
|
|
$primary_keys = array(); |
51
|
|
|
$columns = array(); |
52
|
|
|
|
53
|
|
|
foreach ($this->columns as $column) |
54
|
|
|
{ |
55
|
|
|
$columns[] = $column->sql(); |
56
|
1 |
|
if ($column->param('primary')) |
57
|
|
|
{ |
58
|
1 |
|
$primary_keys[] = "`".$column->name()."`"; |
59
|
1 |
|
} |
60
|
|
|
} |
61
|
1 |
|
|
62
|
|
|
if ($primary_keys) |
|
|
|
|
63
|
1 |
|
{ |
64
|
1 |
|
$columns[] = 'PRIMARY KEY ('.join(', ', $primary_keys).')'; |
65
|
1 |
|
} |
66
|
1 |
|
|
67
|
1 |
|
return join(' ', array_filter(array( |
68
|
1 |
|
"CREATE TABLE `{$this->name}`", |
69
|
|
|
'('. join(', ', $columns).')', |
70
|
|
|
$this->options |
71
|
1 |
|
))); |
72
|
1 |
|
} |
73
|
|
|
} |
74
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..