Code coverage for /20081101/includes/database/mysql/schema.inc

Line #Times calledCode
1
<?php
2
// $Id: schema.inc,v 1.4 2008/10/08 11:09:16 dries Exp $
3
4
/**
5
 * @file
6
 * Database schema code for MySQL database servers.
7
 */
8
9
10
/**
11
 * @ingroup schemaapi
12
 * @{
13
 */
14
15137
class DatabaseSchema_mysql extends DatabaseSchema {
16
17
  public function tableExists($table) {
1821
    return (bool) $this->connection->query("SHOW TABLES LIKE '{" . $table .
"}'", array(), array())->fetchField();
190
  }
20
21
  public function columnExists($table, $column) {
220
    return (bool) $this->connection->query("SHOW COLUMNS FROM {" .
$this->connection->escapeTable($table) . "} LIKE '" .
$this->connection->escapeTable($column) . "'", array(),
array())->fetchField();
230
  }
24
25
26
  /**
27
   * Generate SQL to create a new table from a Drupal schema definition.
28
   *
29
   * @param $name
30
   *   The name of the table to create.
31
   * @param $table
32
   *   A Schema API table definition array.
33
   * @return
34
   *   An array of SQL statements to create the table.
35
   */
36
  protected function createTableSql($name, $table) {
37136
    if (empty($table['mysql_suffix'])) {
38136
      $table['mysql_suffix'] = "/*!40100 DEFAULT CHARACTER SET UTF8 */";
39136
    }
40
41136
    $sql = "CREATE TABLE {" . $name . "} (\n";
42
43
    // Add the SQL statement for each field.
44136
    foreach ($table['fields'] as $field_name => $field) {
45136
      $sql .= $this->createFieldSql($field_name,
$this->processField($field)) . ", \n";
46136
    }
47
48
    // Process keys & indexes.
49136
    $keys = $this->createKeysSql($table);
50136
    if (count($keys)) {
51136
      $sql .= implode(", \n", $keys) . ", \n";
52136
    }
53
54
    // Remove the last comma and space.
55136
    $sql = substr($sql, 0, -3) . "\n) ";
56
57136
    $sql .= $table['mysql_suffix'];
58
59136
    return array($sql);
600
  }
61
62
  /**
63
   * Create an SQL string for a field to be used in table creation or
alteration.
64
   *
65
   * Before passing a field out of a schema definition into this function
it has
66
   * to be processed by _db_process_field().
67
   *
68
   * @param $name
69
   *   Name of the field.
70
   * @param $spec
71
   *   The field specification, as per the schema data structure format.
72
   */
73
  protected function createFieldSql($name, $spec) {
74136
    $sql = "`" . $name . "` " . $spec['mysql_type'];
75
76136
    if (isset($spec['length'])) {
77136
      $sql .= '(' . $spec['length'] . ')';
78136
    }
79136
    elseif (isset($spec['precision']) && isset($spec['scale'])) {
800
      $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
810
    }
82
83136
    if (!empty($spec['unsigned'])) {
84134
      $sql .= ' unsigned';
85134
    }
86
87136
    if (!empty($spec['not null'])) {
88136
      $sql .= ' NOT NULL';
89136
    }
90
91136
    if (!empty($spec['auto_increment'])) {
92136
      $sql .= ' auto_increment';
93136
    }
94
95136
    if (isset($spec['default'])) {
96136
      if (is_string($spec['default'])) {
97136
        $spec['default'] = "'" . $spec['default'] . "'";
98136
      }
99136
      $sql .= ' DEFAULT ' . $spec['default'];
100136
    }
101
102136
    if (empty($spec['not null']) && !isset($spec['default'])) {
103135
      $sql .= ' DEFAULT NULL';
104135
    }
105
106136
    return $sql;
1070
  }
108
109
  /**
110
   * Set database-engine specific properties for a field.
111
   *
112
   * @param $field
113
   *   A field description array, as specified in the schema documentation.
114
   */
115
  protected function processField($field) {
116
117136
    if (!isset($field['size'])) {
118136
      $field['size'] = 'normal';
119136
    }
120
121
    // Set the correct database-engine specific datatype.
122136
    if (!isset($field['mysql_type'])) {
123136
      $map = db_type_map();
124136
      $field['mysql_type'] = $map[$field['type'] . ':' . $field['size']];
125136
    }
126
127136
    if ($field['type'] == 'serial') {
128136
      $field['auto_increment'] = TRUE;
129136
    }
130
131136
    return $field;
1320
  }
133
134
  public function getFieldTypeMap() {
135
    // Put :normal last so it gets preserved by array_flip.  This makes
136
    // it much easier for modules (such as schema.module) to map
137
    // database types back into schema types.
138
    static $map = array(
139
      'varchar:normal'  => 'VARCHAR',
140
      'char:normal'     => 'CHAR',
141
142
      'text:tiny'       => 'TINYTEXT',
143
      'text:small'      => 'TINYTEXT',
144
      'text:medium'     => 'MEDIUMTEXT',
145
      'text:big'        => 'LONGTEXT',
146
      'text:normal'     => 'TEXT',
147
148
      'serial:tiny'     => 'TINYINT',
149
      'serial:small'    => 'SMALLINT',
150
      'serial:medium'   => 'MEDIUMINT',
151
      'serial:big'      => 'BIGINT',
152
      'serial:normal'   => 'INT',
153
154
      'int:tiny'        => 'TINYINT',
155
      'int:small'       => 'SMALLINT',
156
      'int:medium'      => 'MEDIUMINT',
157
      'int:big'         => 'BIGINT',
158
      'int:normal'      => 'INT',
159
160
      'float:tiny'      => 'FLOAT',
161
      'float:small'     => 'FLOAT',
162
      'float:medium'    => 'FLOAT',
163
      'float:big'       => 'DOUBLE',
164
      'float:normal'    => 'FLOAT',
165
166
      'numeric:normal'  => 'DECIMAL',
167
168
      'blob:big'        => 'LONGBLOB',
169
      'blob:normal'     => 'BLOB',
170
171
      'datetime:normal' => 'DATETIME',
172136
    );
173136
    return $map;
1740
  }
175
176
177
178
179
  protected function createKeysSql($spec) {
180136
    $keys = array();
181
182136
    if (!empty($spec['primary key'])) {
183136
      $keys[] = 'PRIMARY KEY (' . $this->createKeysSqlHelper($spec['primary
key']) . ')';
184136
    }
185136
    if (!empty($spec['unique keys'])) {
186135
      foreach ($spec['unique keys'] as $key => $fields) {
187135
        $keys[] = 'UNIQUE KEY ' . $key .' ('.
$this->createKeysSqlHelper($fields) . ')';
188135
      }
189135
    }
190136
    if (!empty($spec['indexes'])) {
191136
      foreach ($spec['indexes'] as $index => $fields) {
192136
        $keys[] = 'INDEX ' . $index . ' (' .
$this->createKeysSqlHelper($fields) . ')';
193136
      }
194136
    }
195
196136
    return $keys;
1970
  }
198
199
  protected function createKeySql($fields) {
2000
    $ret = array();
2010
    foreach ($fields as $field) {
2020
      if (is_array($field)) {
2030
        $ret[] = $field[0] . '(' . $field[1] . ')';
2040
      }
205
      else {
2060
        $ret[] = $field;
207
      }
2080
    }
2090
    return implode(', ', $ret);
2100
  }
211
212
  protected function createKeysSqlHelper($fields) {
213136
    $ret = array();
214136
    foreach ($fields as $field) {
215136
      if (is_array($field)) {
216135
        $ret[] = $field[0] . '(' . $field[1] . ')';
217135
      }
218
      else {
219136
        $ret[] = $field;
220
      }
221136
    }
222136
    return implode(', ', $ret);
2230
  }
224
225
  public function renameTable(&$ret, $table, $new_name) {
2260
    $ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' .
$new_name . '}');
2270
  }
228
229
  public function dropTable(&$ret, $table) {
230135
    $ret[] = update_sql('DROP TABLE {' . $table . '}');
231135
  }
232
233
  public function addField(&$ret, $table, $field, $spec, $keys_new =
array()) {
2340
    $fixnull = FALSE;
2350
    if (!empty($spec['not null']) && !isset($spec['default'])) {
2360
      $fixnull = TRUE;
2370
      $spec['not null'] = FALSE;
2380
    }
2390
    $query = 'ALTER TABLE {' . $table . '} ADD ';
2400
    $query .= $this->createFieldSql($field, $this->processField($spec));
2410
    if (count($keys_new)) {
2420
      $query .= ', ADD ' . implode(', ADD ',
$this->createKeysSql($keys_new));
2430
    }
2440
    $ret[] = update_sql($query);
2450
    if (isset($spec['initial'])) {
246
      // All this because update_sql does not support %-placeholders.
2470
      $sql = 'UPDATE {' . $table . '} SET ' . $field . ' = ' .
db_type_placeholder($spec['type']);
2480
      $result = db_query($sql, $spec['initial']);
2490
      $ret[] = array('success' => $result !== FALSE, 'query' =>
check_plain($sql . ' (' . $spec['initial'] . ')'));
2500
    }
2510
    if ($fixnull) {
2520
      $spec['not null'] = TRUE;
2530
      $this->changeField($ret, $table, $field, $field, $spec);
2540
    }
2550
  }
256
257
  public function dropField(&$ret, $table, $field) {
2580
    $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP ' . $field);
2590
  }
260
261
  public function fieldSetDefault(&$ret, $table, $field, $default) {
2620
    if ($default == NULL) {
2630
      $default = 'NULL';
2640
    }
265
    else {
2660
      $default = is_string($default) ? "'$default'" : $default;
267
    }
268
2690
    $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' .
$field . ' SET DEFAULT ' . $default);
2700
  }
271
272
  public function fieldSetNoDefault(&$ret, $table, $field) {
2730
    $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' .
$field . ' DROP DEFAULT');
2740
  }
275
276
  public function addPrimaryKey(&$ret, $table, $fields) {
2770
    $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' .
$this->createKeySql($fields) . ')');
2780
  }
279
280
  public function dropPrimaryKey(&$ret, $table) {
2810
    $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
2820
  }
283
284
  public function addUniqueKey(&$ret, $table, $name, $fields) {
2850
    $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY ' .
$name . ' (' . $this->createKeySql($fields) . ')');
2860
  }
287
288
  public function dropUniqueKey(&$ret, $table, $name) {
2890
    $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY ' . $name);
2900
  }
291
292
  public function addIndex(&$ret, $table, $name, $fields) {
2930
    $query = 'ALTER TABLE {' . $table . '} ADD INDEX ' . $name . ' (' .
$this->createKeySql($fields) . ')';
2940
    $ret[] = update_sql($query);
2950
  }
296
297
  public function dropIndex(&$ret, $table, $name) {
2980
    $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX ' .
$name);
2990
  }
300
301
  public function changeField(&$ret, $table, $field, $field_new, $spec,
$keys_new = array()) {
3020
    $sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' .
$this->createFieldSql($field_new, $this->processField($spec));
3030
    if (count($keys_new)) {
3040
      $sql .= ', ADD ' . implode(', ADD ',
$this->createKeysSql($keys_new));
3050
    }
3060
    $ret[] = update_sql($sql);
3070
  }
308
309
}
310
311
/**
312
 * @} End of "ingroup schemaapi".
313
 */
314137