1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
class Migration_Driver_Mysql_Column extends Migration_Driver_Column |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* Valid types |
7
|
|
|
* @var array |
8
|
|
|
*/ |
9
|
|
|
static protected $types = array |
10
|
|
|
( |
11
|
|
|
'primary_key' => array('type' => 'INT', 'null' => FALSE, 'auto' => TRUE, 'primary' => TRUE), |
12
|
|
|
'string' => array('type' => 'VARCHAR', 'limit' => 255), |
13
|
|
|
'text' => array('type' => 'TEXT'), |
14
|
|
|
'integer' => array('type' => 'INT'), |
15
|
|
|
'float' => array('type' => 'FLOAT', 'limit' => 10, 'precision' => 2), |
16
|
|
|
'long' => array('type' => 'BIGINT'), |
17
|
|
|
'decimal' => array('type' => 'DECIMAL', 'limit' => 10, 'precision' => 2), |
18
|
|
|
'datetime' => array('type' => 'DATETIME'), |
19
|
|
|
'timestamp' => array('type' => 'TIMESTAMP'), |
20
|
|
|
'time' => array('type' => 'TIME'), |
21
|
|
|
'date' => array('type' => 'DATE'), |
22
|
|
|
'binary' => array('type' => 'BLOB', 'limit' => 255), |
23
|
|
|
'boolean' => array('type' => 'TINYINT', 'limit' => 1, 'null' => FALSE, 'default' => 0), |
24
|
|
|
'enum' => array('type' => 'ENUM', 'values' => array()), |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
static protected $native_types = array |
28
|
|
|
( |
29
|
|
|
'char' => 'string', |
30
|
|
|
'varchar' => 'string', |
31
|
|
|
'text' => 'text', |
32
|
|
|
'int' => 'integer', |
33
|
|
|
'integer' => 'integer', |
34
|
|
|
'tinyint' => 'boolean', |
35
|
|
|
'bigint' => 'integer', |
36
|
|
|
'float' => 'float', |
37
|
|
|
'decimal' => 'decimal', |
38
|
|
|
'datetime' => 'datetime', |
39
|
|
|
'timestamp' => 'timestamp', |
40
|
|
|
'time' => 'time', |
41
|
|
|
'date' => 'date', |
42
|
|
|
'blob' => 'binary', |
43
|
|
|
'enum' => 'enum', |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
public function column_params_for($column) |
47
|
|
|
{ |
48
|
|
|
return Arr::get(self::$types, $column, array()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function load($table_name) |
52
|
|
|
{ |
53
|
|
|
if (is_string($table_name)) |
54
|
27 |
|
{ |
55
|
|
|
try |
56
|
27 |
|
{ |
57
|
|
|
$result = $this->driver->pdo->query("SHOW COLUMNS FROM `$table_name` LIKE '{$this->name}'"); |
|
|
|
|
58
|
|
|
} |
59
|
2 |
|
catch (PDOException $e) |
60
|
|
|
{ |
61
|
2 |
|
$result = NULL; |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
if ( ! $result OR $result->rowCount() !== 1) |
65
|
2 |
|
{ |
66
|
|
|
throw new Migration_Exception("Column :column was not found in table :table", array(':column' => $this->name, ':table' => $this->name)); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
2 |
|
$result = $result->fetchObject(); |
70
|
|
|
} |
71
|
|
|
else |
72
|
2 |
|
{ |
73
|
2 |
|
$result = $table_name; |
74
|
2 |
|
} |
75
|
|
|
|
76
|
|
|
if (preg_match('/([^\(]+)(\((\d+)\))?( UNSIGNED)?/', $result->Type, $type)) |
77
|
|
|
{ |
78
|
|
|
$limit = Arr::get($type, 3); |
79
|
|
|
$unsigned = isset($type[4]) ? TRUE : NULL; |
80
|
|
|
$type = $type[1]; |
81
|
|
|
$values = NULL; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (preg_match('/enum\(([^\)]+)\)/', $result->Type, $enum_type)) |
85
|
|
|
{ |
86
|
|
|
$type = 'ENUM'; |
87
|
|
|
$limit = NULL; |
88
|
|
|
$unsigned = NULL; |
89
|
|
|
$values = explode(',', $enum_type[1]); |
90
|
|
|
foreach ($values as & $value) |
91
|
|
|
{ |
92
|
|
|
$value = trim($value, "'"); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->params(array( |
97
|
|
|
'type' => $type, |
98
|
|
|
'limit' => $limit, |
|
|
|
|
99
|
|
|
'unsigned' => $unsigned, |
|
|
|
|
100
|
|
|
'values' => $values, |
|
|
|
|
101
|
|
|
'null' => $result->Null == 'NO' ? FALSE : TRUE, |
102
|
|
|
'default' => $result->Default ? $result->Default : NULL, |
103
|
|
|
'auto' => $result->Extra == 'auto_increment', |
104
|
|
|
'primary' => $result->Key == 'PRI', |
105
|
|
|
)); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function type() |
111
|
|
|
{ |
112
|
|
|
return Arr::get(self::native_types, strtolower($this->type)); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function sql() |
116
|
|
|
{ |
117
|
|
|
extract(Arr::extract($this->params, Migration_Driver_Column::$available_params)); |
|
|
|
|
118
|
|
|
|
119
|
|
|
return join(' ', array_filter(array( |
120
|
|
|
"`{$this->name}`", |
121
|
|
|
$type, |
122
|
|
|
$limit ? ($precision ? ( "({$limit}, {$precision})" ) : "({$limit})") : NULL, |
123
|
28 |
|
$values ? ('('.join(', ', array_map(array($this->driver->pdo, 'quote'), $values)).')') : NULL, |
|
|
|
|
124
|
|
|
$unsigned ? ("UNSIGNED") : NULL, |
125
|
28 |
|
($default OR $default === 0 OR $default === '0') ? ("DEFAULT ".$this->driver->pdo->quote($default)) : NULL, |
126
|
|
|
$null !== NULL ? ($null ? "NULL" : "NOT NULL") : NULL, |
127
|
28 |
|
$auto ? ("AUTO_INCREMENT") : NULL, |
128
|
28 |
|
$comment ? ("COMMENT '{$comment}'") : NULL, |
129
|
28 |
|
$after ? ("AFTER `{$after}`") : NULL, |
130
|
28 |
|
$first ? ("FIRST") : NULL, |
131
|
28 |
|
))); |
132
|
28 |
|
} |
133
|
|
|
} |
134
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.