1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
class Migration_Driver_Mysql extends Migration_Driver |
4
|
|
|
{ |
5
|
|
|
public $pdo; |
6
|
|
|
|
7
|
|
|
public function __construct($database) |
8
|
|
|
{ |
9
|
|
|
if ($database instanceof PDO) |
10
|
|
|
{ |
11
|
|
|
$this->pdo = $database; |
12
|
|
|
} |
13
|
|
|
else |
14
|
|
|
{ |
15
|
|
|
$database = Kohana::$config->load('database.'.$database); |
16
|
54 |
|
|
17
|
|
|
if ($database['type'] !== 'PDO') |
18
|
54 |
|
{ |
19
|
54 |
|
$database['connection']['dsn'] = strtolower($database['type'] |
20
|
|
|
.':host='.$database['connection']['hostname'] |
21
|
|
|
.';dbname='.$database['connection']['database'] |
22
|
|
|
.(isset($database['connection']['charset']) |
23
|
|
|
? ';charset='.$database['connection']['charset'] |
24
|
54 |
|
: '') |
25
|
|
|
); |
26
|
54 |
|
} |
27
|
54 |
|
|
28
|
54 |
|
$this->pdo = new PDO( |
29
|
54 |
|
$database['connection']['dsn'], |
30
|
54 |
|
$database['connection']['username'], |
31
|
54 |
|
$database['connection']['password'], |
32
|
54 |
|
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") |
33
|
54 |
|
); |
34
|
54 |
|
} |
35
|
54 |
|
|
36
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
37
|
54 |
|
|
38
|
54 |
|
$this->versions = new Migration_Driver_Mysql_Versions($this->pdo); |
39
|
54 |
|
} |
40
|
54 |
|
|
41
|
54 |
|
public function clear_all() |
42
|
54 |
|
{ |
43
|
|
|
$tables = array_diff(Arr::pluck($this->pdo->query('SHOW TABLES'), '0'), array(Migration_Driver_Mysql_Versions::SCHEMA_TABLE)); |
|
|
|
|
44
|
|
|
|
45
|
54 |
|
if (count($tables)) |
46
|
|
|
{ |
47
|
54 |
|
$this->execute("DROP TABLE ".join(', ', $tables)); |
48
|
54 |
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function column($name) |
54
|
|
|
{ |
55
|
|
|
return new Migration_Driver_Mysql_Column($name, $this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function table($name) |
59
|
|
|
{ |
60
|
|
|
return new Migration_Driver_Mysql_Table($name, $this); |
61
|
|
|
} |
62
|
29 |
|
|
63
|
|
|
public function execute($sql, $params = NULL) |
64
|
29 |
|
{ |
65
|
|
|
try |
66
|
|
|
{ |
67
|
2 |
|
$statement = $this->pdo->prepare($sql); |
68
|
|
|
if ($params) |
69
|
2 |
|
{ |
70
|
|
|
$statement->execute((array) $params); |
71
|
|
|
} |
72
|
|
|
else |
73
|
|
|
{ |
74
|
|
|
$statement->execute(); |
75
|
|
|
} |
76
|
|
|
$this->_affected_rows = $statement->rowCount(); |
77
|
|
|
} |
78
|
|
|
catch (PDOException $e) |
79
|
|
|
{ |
80
|
|
|
throw new Migration_Exception(":sql\n Exception: :message", array(':sql' => $sql, ":message" => $e->getMessage())); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function query($sql, $params = NULL) |
87
|
|
|
{ |
88
|
|
|
try |
89
|
|
|
{ |
90
|
|
|
$query = $this->pdo->prepare($sql); |
91
|
|
|
$query->execute($params); |
92
|
|
|
return $query; |
93
|
|
|
} |
94
|
|
|
catch (PDOException $e) |
95
|
|
|
{ |
96
|
|
|
throw new Migration_Exception(":sql\n Exception: :message", array(':sql' => $sql, ":message" => $e->getMessage())); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function quote($string) |
101
|
|
|
{ |
102
|
|
|
return $this->pdo->quote($string); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function create_table($table_name, $fields, $options = NULL) |
106
|
|
|
{ |
107
|
|
|
$this->execute($this->table($table_name)->params($fields, $options)->sql()); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function drop_table($table_name) |
112
|
|
|
{ |
113
|
|
|
$this->execute("DROP TABLE `$table_name`"); |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
1 |
|
|
117
|
1 |
|
public function rename_table($old_name, $new_name) |
118
|
|
|
{ |
119
|
|
|
$this->execute("RENAME TABLE `$old_name` TO `$new_name`"); |
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
1 |
|
|
123
|
1 |
|
public function add_column($table_name, $column_name, $params) |
124
|
|
|
{ |
125
|
|
|
$column = $this->column($column_name)->params($params); |
126
|
1 |
|
|
127
|
|
|
if ($column->param('primary')) |
128
|
1 |
|
{ |
129
|
1 |
|
$sql = "ALTER TABLE `$table_name` DROP PRIMARY KEY, ADD COLUMN ".$column->sql().', ADD PRIMARY KEY (`'.$column_name.'`)'; |
130
|
|
|
} |
131
|
|
|
else |
132
|
1 |
|
{ |
133
|
|
|
$sql = "ALTER TABLE `$table_name` ADD COLUMN ".$column->sql(); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
$this->execute($sql); |
137
|
1 |
|
return $this; |
138
|
1 |
|
} |
139
|
1 |
|
|
140
|
|
|
public function rename_column($table_name, $column_name, $new_column_name) |
141
|
|
|
{ |
142
|
1 |
|
$column = $this->column($column_name)->load($table_name)->name($new_column_name); |
143
|
|
|
$this->execute("ALTER TABLE `$table_name` CHANGE `$column_name` ".$column->sql()); |
144
|
|
|
return $this; |
145
|
1 |
|
} |
146
|
1 |
|
|
147
|
|
|
public function change_column($table_name, $column_name, $params) |
148
|
|
|
{ |
149
|
1 |
|
$this->execute("ALTER TABLE `$table_name` MODIFY ".$this->column($column_name)->params($params)->sql()); |
150
|
|
|
return $this; |
151
|
1 |
|
} |
152
|
1 |
|
|
153
|
1 |
|
public function change_table($table_name, $options) |
154
|
|
|
{ |
155
|
|
|
$this->execute("ALTER TABLE `$table_name` ".join(' ', (array) $options)); |
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
1 |
|
|
159
|
1 |
|
public function remove_column($table_name, $column_name) |
160
|
|
|
{ |
161
|
|
|
$this->execute("ALTER TABLE `$table_name` DROP COLUMN `$column_name`"); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function add_index($table_name, $index_name, $columns, $index_type = 'normal') |
167
|
|
|
{ |
168
|
1 |
|
switch ($index_type) |
169
|
|
|
{ |
170
|
1 |
|
case 'normal': $type = 'INDEX'; break; |
171
|
|
|
case 'unique': $type = 'UNIQUE INDEX'; break; |
172
|
1 |
|
case 'primary': $type = 'PRIMARY KEY'; break; |
173
|
|
|
case 'fulltext': $type = 'FULLTEXT'; break; |
174
|
|
|
case 'spatial': $type = 'SPATIAL'; break; |
175
|
1 |
|
|
176
|
|
|
default: throw new Migration_Exception("Incorrect index type :index_type, normal, unique primary, fulltext and spacial allowed", array(':index_type' => $index_type)); |
177
|
|
|
} |
178
|
|
|
$columns = (array) $columns; |
179
|
1 |
|
|
180
|
1 |
|
foreach ($columns as $i => &$column) |
181
|
1 |
|
{ |
182
|
1 |
|
$column = "`$column`"; |
183
|
1 |
|
} |
184
|
|
|
|
185
|
|
|
$sql = join(' ', array( |
186
|
|
|
"ALTER TABLE `$table_name` ADD $type `$index_name`", |
187
|
1 |
|
'('.join(', ', $columns).')' |
188
|
|
|
)); |
189
|
1 |
|
|
190
|
|
|
$this->execute($sql); |
191
|
1 |
|
return $this; |
192
|
1 |
|
} |
193
|
|
|
|
194
|
1 |
|
public function remove_index($table_name, $index_name) |
195
|
1 |
|
{ |
196
|
1 |
|
$this->execute("ALTER TABLE `$table_name` DROP INDEX `$index_name`"); |
197
|
1 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: