| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class DatabaseSchema_mysql extends DatabaseSchema {
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
$info = Database::getConnectionInfo();
|
| 37 |
|
| 38 |
if (strpos($table_name, '.')) {
|
| 39 |
list($schema, $table_name) = explode('.', $table_name);
|
| 40 |
}
|
| 41 |
|
| 42 |
$schema = $info['default']['database'];
|
| 43 |
|
| 44 |
|
| 45 |
$condition = new DatabaseCondition('AND');
|
| 46 |
$condition->condition('table_schema', $schema);
|
| 47 |
$condition->condition('table_name', $table_name, $operator);
|
| 48 |
return $condition;
|
| 49 |
}
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
'mysql_engine' => 'InnoDB',
|
| 65 |
'mysql_character_set' => 'UTF8',
|
| 66 |
);
|
| 67 |
|
| 68 |
$sql = "CREATE TABLE {" . $name . "} (\n";
|
| 69 |
|
| 70 |
|
| 71 |
foreach ($table['fields'] as $field_name => $field) {
|
| 72 |
$sql .= $this->createFieldSql($field_name, $this->processField($field)) . ", \n";
|
| 73 |
}
|
| 74 |
|
| 75 |
|
| 76 |
$keys = $this->createKeysSql($table);
|
| 77 |
if (count($keys)) {
|
| 78 |
$sql .= implode(", \n", $keys) . ", \n";
|
| 79 |
}
|
| 80 |
|
| 81 |
|
| 82 |
$sql = substr($sql, 0, -3) . "\n) ";
|
| 83 |
|
| 84 |
$sql .= 'ENGINE = ' . $table['mysql_engine'] . ' DEFAULT CHARACTER SET ' . $table['mysql_character_set'];
|
| 85 |
|
| 86 |
|
| 87 |
if (!empty($table['description'])) {
|
| 88 |
$sql .= ' COMMENT ' . $this->prepareComment($table['description'], self::COMMENT_MAX_TABLE);
|
| 89 |
}
|
| 90 |
|
| 91 |
return array($sql);
|
| 92 |
}
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
$sql = "`" . $name . "` " . $spec['mysql_type'];
|
| 107 |
|
| 108 |
if (in_array($spec['type'], array('varchar', 'char', 'text')) && isset($spec['length'])) {
|
| 109 |
$sql .= '(' . $spec['length'] . ')';
|
| 110 |
}
|
| 111 |
elseif (isset($spec['precision']) && isset($spec['scale'])) {
|
| 112 |
$sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
|
| 113 |
}
|
| 114 |
|
| 115 |
if (!empty($spec['unsigned'])) {
|
| 116 |
$sql .= ' unsigned';
|
| 117 |
}
|
| 118 |
|
| 119 |
if (!empty($spec['not null'])) {
|
| 120 |
$sql .= ' NOT NULL';
|
| 121 |
}
|
| 122 |
|
| 123 |
if (!empty($spec['auto_increment'])) {
|
| 124 |
$sql .= ' auto_increment';
|
| 125 |
}
|
| 126 |
|
| 127 |
|
| 128 |
if (array_key_exists('default', $spec)) {
|
| 129 |
if (is_string($spec['default'])) {
|
| 130 |
$spec['default'] = "'" . $spec['default'] . "'";
|
| 131 |
}
|
| 132 |
elseif (is_null($spec['default'])) {
|
| 133 |
$spec['default'] = 'NULL';
|
| 134 |
}
|
| 135 |
$sql .= ' DEFAULT ' . $spec['default'];
|
| 136 |
}
|
| 137 |
|
| 138 |
if (empty($spec['not null']) && !isset($spec['default'])) {
|
| 139 |
$sql .= ' DEFAULT NULL';
|
| 140 |
}
|
| 141 |
|
| 142 |
|
| 143 |
if (!empty($spec['description'])) {
|
| 144 |
$sql .= ' COMMENT ' . $this->prepareComment($spec['description'], self::COMMENT_MAX_COLUMN);
|
| 145 |
}
|
| 146 |
|
| 147 |
return $sql;
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (!isset($field['size'])) {
|
| 159 |
$field['size'] = 'normal';
|
| 160 |
}
|
| 161 |
|
| 162 |
|
| 163 |
if (!isset($field['mysql_type'])) {
|
| 164 |
$map = db_type_map();
|
| 165 |
$field['mysql_type'] = $map[$field['type'] . ':' . $field['size']];
|
| 166 |
}
|
| 167 |
|
| 168 |
if ($field['type'] == 'serial') {
|
| 169 |
$field['auto_increment'] = TRUE;
|
| 170 |
}
|
| 171 |
|
| 172 |
return $field;
|
| 173 |
}
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
);
|
| 215 |
return $map;
|
| 216 |
}
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
$keys = array();
|
| 223 |
|
| 224 |
if (!empty($spec['primary key'])) {
|
| 225 |
$keys[] = 'PRIMARY KEY (' . $this->createKeysSqlHelper($spec['primary key']) . ')';
|
| 226 |
}
|
| 227 |
if (!empty($spec['unique keys'])) {
|
| 228 |
foreach ($spec['unique keys'] as $key => $fields) {
|
| 229 |
$keys[] = 'UNIQUE KEY ' . $key . ' (' . $this->createKeysSqlHelper($fields) . ')';
|
| 230 |
}
|
| 231 |
}
|
| 232 |
if (!empty($spec['indexes'])) {
|
| 233 |
foreach ($spec['indexes'] as $index => $fields) {
|
| 234 |
$keys[] = 'INDEX ' . $index . ' (' . $this->createKeysSqlHelper($fields) . ')';
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
return $keys;
|
| 239 |
}
|
| 240 |
|
| 241 |
|
| 242 |
$ret = array();
|
| 243 |
foreach ($fields as $field) {
|
| 244 |
if (is_array($field)) {
|
| 245 |
$ret[] = $field[0] . '(' . $field[1] . ')';
|
| 246 |
}
|
| 247 |
|
| 248 |
$ret[] = $field;
|
| 249 |
|
| 250 |
}
|
| 251 |
return implode(', ', $ret);
|
| 252 |
}
|
| 253 |
|
| 254 |
|
| 255 |
$ret = array();
|
| 256 |
foreach ($fields as $field) {
|
| 257 |
if (is_array($field)) {
|
| 258 |
$ret[] = $field[0] . '(' . $field[1] . ')';
|
| 259 |
}
|
| 260 |
|
| 261 |
$ret[] = $field;
|
| 262 |
|
| 263 |
}
|
| 264 |
return implode(', ', $ret);
|
| 265 |
}
|
| 266 |
|
| 267 |
|
| 268 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
|
| 269 |
}
|
| 270 |
|
| 271 |
|
| 272 |
$ret[] = update_sql('DROP TABLE {' . $table . '}');
|
| 273 |
}
|
| 274 |
|
| 275 |
|
| 276 |
$fixnull = FALSE;
|
| 277 |
if (!empty($spec['not null']) && !isset($spec['default'])) {
|
| 278 |
$fixnull = TRUE;
|
| 279 |
$spec['not null'] = FALSE;
|
| 280 |
}
|
| 281 |
$query = 'ALTER TABLE {' . $table . '} ADD ';
|
| 282 |
$query .= $this->createFieldSql($field, $this->processField($spec));
|
| 283 |
if (count($keys_new)) {
|
| 284 |
$query .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new));
|
| 285 |
}
|
| 286 |
$ret[] = update_sql($query);
|
| 287 |
if (isset($spec['initial'])) {
|
| 288 |
|
| 289 |
$sql = 'UPDATE {' . $table . '} SET ' . $field . ' = :value';
|
| 290 |
$result = db_query($sql, array(':value' => $spec['initial']));
|
| 291 |
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql . ' (' . $spec['initial'] . ')'));
|
| 292 |
}
|
| 293 |
if ($fixnull) {
|
| 294 |
$spec['not null'] = TRUE;
|
| 295 |
$this->changeField($ret, $table, $field, $field, $spec);
|
| 296 |
}
|
| 297 |
}
|
| 298 |
|
| 299 |
|
| 300 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP ' . $field);
|
| 301 |
}
|
| 302 |
|
| 303 |
|
| 304 |
if (is_null($default)) {
|
| 305 |
$default = 'NULL';
|
| 306 |
}
|
| 307 |
|
| 308 |
$default = is_string($default) ? "'$default'" : $default;
|
| 309 |
|
| 310 |
|
| 311 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' SET DEFAULT ' . $default);
|
| 312 |
}
|
| 313 |
|
| 314 |
|
| 315 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' DROP DEFAULT');
|
| 316 |
}
|
| 317 |
|
| 318 |
|
| 319 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')');
|
| 320 |
}
|
| 321 |
|
| 322 |
|
| 323 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
|
| 324 |
}
|
| 325 |
|
| 326 |
|
| 327 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY ' . $name . ' (' . $this->createKeySql($fields) . ')');
|
| 328 |
}
|
| 329 |
|
| 330 |
|
| 331 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY ' . $name);
|
| 332 |
}
|
| 333 |
|
| 334 |
|
| 335 |
$query = 'ALTER TABLE {' . $table . '} ADD INDEX ' . $name . ' (' . $this->createKeySql($fields) . ')';
|
| 336 |
$ret[] = update_sql($query);
|
| 337 |
}
|
| 338 |
|
| 339 |
|
| 340 |
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX ' . $name);
|
| 341 |
}
|
| 342 |
|
| 343 |
|
| 344 |
$sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
|
| 345 |
if (count($keys_new)) {
|
| 346 |
$sql .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new));
|
| 347 |
}
|
| 348 |
$ret[] = update_sql($sql);
|
| 349 |
}
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
$comment = str_replace("'", '’', $comment);
|
| 354 |
|
| 355 |
|
| 356 |
if (isset($length)) {
|
| 357 |
|
| 358 |
$comment = truncate_utf8($this->connection->prefixTables($comment), $length, TRUE, TRUE);
|
| 359 |
}
|
| 360 |
|
| 361 |
return $this->connection->quote($comment);
|
| 362 |
}
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
$condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}'));
|
| 369 |
if (isset($column)) {
|
| 370 |
$condition->condition('column_name', $column);
|
| 371 |
$condition->compile($this->connection);
|
| 372 |
|
| 373 |
return db_query("SELECT column_comment FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
| 374 |
}
|
| 375 |
$condition->compile($this->connection);
|
| 376 |
|
| 377 |
$comment = db_query("SELECT table_comment FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
| 378 |
|
| 379 |
return preg_replace('/; InnoDB free:.*$/', '', $comment);
|
| 380 |
}
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|