1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Generic Database schema code.
|
6
|
*/
|
7
|
|
8
|
require_once dirname(__FILE__) . '/query.inc';
|
9
|
|
10
|
/**
|
11
|
* @defgroup schemaapi Schema API
|
12
|
* @{
|
13
|
* API to handle database schemas.
|
14
|
*
|
15
|
* A Drupal schema definition is an array structure representing one or
|
16
|
* more tables and their related keys and indexes. A schema is defined by
|
17
|
* hook_schema(), which usually lives in a modulename.install file.
|
18
|
*
|
19
|
* By implementing hook_schema() and specifying the tables your module
|
20
|
* declares, you can easily create and drop these tables on all
|
21
|
* supported database engines. You don't have to deal with the
|
22
|
* different SQL dialects for table creation and alteration of the
|
23
|
* supported database engines.
|
24
|
*
|
25
|
* hook_schema() should return an array with a key for each table that
|
26
|
* the module defines.
|
27
|
*
|
28
|
* The following keys are defined:
|
29
|
* - 'description': A string in non-markup plain text describing this table
|
30
|
* and its purpose. References to other tables should be enclosed in
|
31
|
* curly-brackets. For example, the node_revisions table
|
32
|
* description field might contain "Stores per-revision title and
|
33
|
* body data for each {node}."
|
34
|
* - 'fields': An associative array ('fieldname' => specification)
|
35
|
* that describes the table's database columns. The specification
|
36
|
* is also an array. The following specification parameters are defined:
|
37
|
* - 'description': A string in non-markup plain text describing this field
|
38
|
* and its purpose. References to other tables should be enclosed in
|
39
|
* curly-brackets. For example, the node table vid field
|
40
|
* description might contain "Always holds the largest (most
|
41
|
* recent) {node_revision}.vid value for this nid."
|
42
|
* - 'type': The generic datatype: 'char', 'varchar', 'text', 'blob', 'int',
|
43
|
* 'float', 'numeric', or 'serial'. Most types just map to the according
|
44
|
* database engine specific datatypes. Use 'serial' for auto incrementing
|
45
|
* fields. This will expand to 'INT auto_increment' on MySQL.
|
46
|
* - 'mysql_type', 'pgsql_type', 'sqlite_type', etc.: If you need to
|
47
|
* use a record type not included in the officially supported list
|
48
|
* of types above, you can specify a type for each database
|
49
|
* backend. In this case, you can leave out the type parameter,
|
50
|
* but be advised that your schema will fail to load on backends that
|
51
|
* do not have a type specified. A possible solution can be to
|
52
|
* use the "text" type as a fallback.
|
53
|
* - 'serialize': A boolean indicating whether the field will be stored as
|
54
|
* a serialized string.
|
55
|
* - 'size': The data size: 'tiny', 'small', 'medium', 'normal',
|
56
|
* 'big'. This is a hint about the largest value the field will
|
57
|
* store and determines which of the database engine specific
|
58
|
* datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT).
|
59
|
* 'normal', the default, selects the base type (e.g. on MySQL,
|
60
|
* INT, VARCHAR, BLOB, etc.).
|
61
|
* Not all sizes are available for all data types. See
|
62
|
* DatabaseSchema::getFieldTypeMap() for possible combinations.
|
63
|
* - 'not null': If true, no NULL values will be allowed in this
|
64
|
* database column. Defaults to false.
|
65
|
* - 'default': The field's default value. The PHP type of the
|
66
|
* value matters: '', '0', and 0 are all different. If you
|
67
|
* specify '0' as the default value for a type 'int' field it
|
68
|
* will not work because '0' is a string containing the
|
69
|
* character "zero", not an integer.
|
70
|
* - 'length': The maximal length of a type 'char', 'varchar' or 'text'
|
71
|
* field. Ignored for other field types.
|
72
|
* - 'unsigned': A boolean indicating whether a type 'int', 'float'
|
73
|
* and 'numeric' only is signed or unsigned. Defaults to
|
74
|
* FALSE. Ignored for other field types.
|
75
|
* - 'precision', 'scale': For type 'numeric' fields, indicates
|
76
|
* the precision (total number of significant digits) and scale
|
77
|
* (decimal digits right of the decimal point). Both values are
|
78
|
* mandatory. Ignored for other field types.
|
79
|
* - 'binary': A boolean indicating that MySQL should force 'char',
|
80
|
* 'varchar' or 'text' fields to use case-sensitive binary collation.
|
81
|
* This has no effect on other database types for which case sensitivity
|
82
|
* is already the default behavior.
|
83
|
* All parameters apart from 'type' are optional except that type
|
84
|
* 'numeric' columns must specify 'precision' and 'scale', and type
|
85
|
* 'varchar' must specify the 'length' parameter.
|
86
|
* - 'primary key': An array of one or more key column specifiers (see below)
|
87
|
* that form the primary key.
|
88
|
* - 'unique keys': An associative array of unique keys ('keyname' =>
|
89
|
* specification). Each specification is an array of one or more
|
90
|
* key column specifiers (see below) that form a unique key on the table.
|
91
|
* - 'foreign keys': An associative array of relations ('my_relation' =>
|
92
|
* specification). Each specification is an array containing the name of
|
93
|
* the referenced table ('table'), and an array of column mappings
|
94
|
* ('columns'). Column mappings are defined by key pairs ('source_column' =>
|
95
|
* 'referenced_column').
|
96
|
* - 'indexes': An associative array of indexes ('indexname' =>
|
97
|
* specification). Each specification is an array of one or more
|
98
|
* key column specifiers (see below) that form an index on the
|
99
|
* table.
|
100
|
*
|
101
|
* A key column specifier is either a string naming a column or an
|
102
|
* array of two elements, column name and length, specifying a prefix
|
103
|
* of the named column.
|
104
|
*
|
105
|
* As an example, here is a SUBSET of the schema definition for
|
106
|
* Drupal's 'node' table. It show four fields (nid, vid, type, and
|
107
|
* title), the primary key on field 'nid', a unique key named 'vid' on
|
108
|
* field 'vid', and two indexes, one named 'nid' on field 'nid' and
|
109
|
* one named 'node_title_type' on the field 'title' and the first four
|
110
|
* bytes of the field 'type':
|
111
|
*
|
112
|
* @code
|
113
|
* $schema['node'] = array(
|
114
|
* 'description' => 'The base table for nodes.',
|
115
|
* 'fields' => array(
|
116
|
* 'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
|
117
|
* 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
|
118
|
* 'type' => array('type' => 'varchar','length' => 32,'not null' => TRUE, 'default' => ''),
|
119
|
* 'language' => array('type' => 'varchar','length' => 12,'not null' => TRUE,'default' => ''),
|
120
|
* 'title' => array('type' => 'varchar','length' => 255,'not null' => TRUE, 'default' => ''),
|
121
|
* 'uid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
122
|
* 'status' => array('type' => 'int', 'not null' => TRUE, 'default' => 1),
|
123
|
* 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
124
|
* 'changed' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
125
|
* 'comment' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
126
|
* 'promote' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
127
|
* 'moderate' => array('type' => 'int', 'not null' => TRUE,'default' => 0),
|
128
|
* 'sticky' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
129
|
* 'tnid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
|
130
|
* 'translate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
|
131
|
* ),
|
132
|
* 'indexes' => array(
|
133
|
* 'node_changed' => array('changed'),
|
134
|
* 'node_created' => array('created'),
|
135
|
* 'node_moderate' => array('moderate'),
|
136
|
* 'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
|
137
|
* 'node_status_type' => array('status', 'type', 'nid'),
|
138
|
* 'node_title_type' => array('title', array('type', 4)),
|
139
|
* 'node_type' => array(array('type', 4)),
|
140
|
* 'uid' => array('uid'),
|
141
|
* 'tnid' => array('tnid'),
|
142
|
* 'translate' => array('translate'),
|
143
|
* ),
|
144
|
* 'unique keys' => array(
|
145
|
* 'vid' => array('vid'),
|
146
|
* ),
|
147
|
* 'foreign keys' => array(
|
148
|
* 'node_revision' => array(
|
149
|
* 'table' => 'node_revision',
|
150
|
* 'columns' => array('vid' => 'vid'),
|
151
|
* ),
|
152
|
* 'node_author' => array(
|
153
|
* 'table' => 'users',
|
154
|
* 'columns' => array('uid' => 'uid'),
|
155
|
* ),
|
156
|
* ),
|
157
|
* 'primary key' => array('nid'),
|
158
|
* );
|
159
|
* @endcode
|
160
|
*
|
161
|
* @see drupal_install_schema()
|
162
|
*/
|
163
|
|
164
|
abstract class DatabaseSchema implements QueryPlaceholderInterface {
|
165
|
|
166
|
protected $connection;
|
167
|
|
168
|
/**
|
169
|
* The placeholder counter.
|
170
|
*/
|
171
|
protected $placeholder = 0;
|
172
|
|
173
|
/**
|
174
|
* Definition of prefixInfo array structure.
|
175
|
*
|
176
|
* Rather than redefining DatabaseSchema::getPrefixInfo() for each driver,
|
177
|
* by defining the defaultSchema variable only MySQL has to re-write the
|
178
|
* method.
|
179
|
*
|
180
|
* @see DatabaseSchema::getPrefixInfo()
|
181
|
*/
|
182
|
protected $defaultSchema = 'public';
|
183
|
|
184
|
/**
|
185
|
* A unique identifier for this query object.
|
186
|
*/
|
187
|
protected $uniqueIdentifier;
|
188
|
|
189
|
public function __construct($connection) {
|
190
|
$this->uniqueIdentifier = uniqid('', TRUE);
|
191
|
$this->connection = $connection;
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Implements the magic __clone function.
|
196
|
*/
|
197
|
public function __clone() {
|
198
|
$this->uniqueIdentifier = uniqid('', TRUE);
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Implements QueryPlaceHolderInterface::uniqueIdentifier().
|
203
|
*/
|
204
|
public function uniqueIdentifier() {
|
205
|
return $this->uniqueIdentifier;
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Implements QueryPlaceHolderInterface::nextPlaceholder().
|
210
|
*/
|
211
|
public function nextPlaceholder() {
|
212
|
return $this->placeholder++;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Get information about the table name and schema from the prefix.
|
217
|
*
|
218
|
* @param
|
219
|
* Name of table to look prefix up for. Defaults to 'default' because thats
|
220
|
* default key for prefix.
|
221
|
* @param $add_prefix
|
222
|
* Boolean that indicates whether the given table name should be prefixed.
|
223
|
*
|
224
|
* @return
|
225
|
* A keyed array with information about the schema, table name and prefix.
|
226
|
*/
|
227
|
protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
|
228
|
$info = array(
|
229
|
'schema' => $this->defaultSchema,
|
230
|
'prefix' => $this->connection->tablePrefix($table),
|
231
|
);
|
232
|
if ($add_prefix) {
|
233
|
$table = $info['prefix'] . $table;
|
234
|
}
|
235
|
// If the prefix contains a period in it, then that means the prefix also
|
236
|
// contains a schema reference in which case we will change the schema key
|
237
|
// to the value before the period in the prefix. Everything after the dot
|
238
|
// will be prefixed onto the front of the table.
|
239
|
if (($pos = strpos($table, '.')) !== FALSE) {
|
240
|
// Grab everything before the period.
|
241
|
$info['schema'] = substr($table, 0, $pos);
|
242
|
// Grab everything after the dot.
|
243
|
$info['table'] = substr($table, ++$pos);
|
244
|
}
|
245
|
else {
|
246
|
$info['table'] = $table;
|
247
|
}
|
248
|
return $info;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Create names for indexes, primary keys and constraints.
|
253
|
*
|
254
|
* This prevents using {} around non-table names like indexes and keys.
|
255
|
*/
|
256
|
function prefixNonTable($table) {
|
257
|
$args = func_get_args();
|
258
|
$info = $this->getPrefixInfo($table);
|
259
|
$args[0] = $info['table'];
|
260
|
return implode('_', $args);
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Build a condition to match a table name against a standard information_schema.
|
265
|
*
|
266
|
* The information_schema is a SQL standard that provides information about the
|
267
|
* database server and the databases, schemas, tables, columns and users within
|
268
|
* it. This makes information_schema a useful tool to use across the drupal
|
269
|
* database drivers and is used by a few different functions. The function below
|
270
|
* describes the conditions to be meet when querying information_schema.tables
|
271
|
* for drupal tables or information associated with drupal tables. Even though
|
272
|
* this is the standard method, not all databases follow standards and so this
|
273
|
* method should be overwritten by a database driver if the database provider
|
274
|
* uses alternate methods. Because information_schema.tables is used in a few
|
275
|
* different functions, a database driver will only need to override this function
|
276
|
* to make all the others work. For example see includes/databases/mysql/schema.inc.
|
277
|
*
|
278
|
* @param $table_name
|
279
|
* The name of the table in question.
|
280
|
* @param $operator
|
281
|
* The operator to apply on the 'table' part of the condition.
|
282
|
* @param $add_prefix
|
283
|
* Boolean to indicate whether the table name needs to be prefixed.
|
284
|
*
|
285
|
* @return QueryConditionInterface
|
286
|
* A DatabaseCondition object.
|
287
|
*/
|
288
|
protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
|
289
|
$info = $this->connection->getConnectionOptions();
|
290
|
|
291
|
// Retrive the table name and schema
|
292
|
$table_info = $this->getPrefixInfo($table_name, $add_prefix);
|
293
|
|
294
|
$condition = new DatabaseCondition('AND');
|
295
|
$condition->condition('table_catalog', $info['database']);
|
296
|
$condition->condition('table_schema', $table_info['schema']);
|
297
|
$condition->condition('table_name', $table_info['table'], $operator);
|
298
|
return $condition;
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Check if a table exists.
|
303
|
*
|
304
|
* @param $table
|
305
|
* The name of the table in drupal (no prefixing).
|
306
|
*
|
307
|
* @return
|
308
|
* TRUE if the given table exists, otherwise FALSE.
|
309
|
*/
|
310
|
public function tableExists($table) {
|
311
|
$condition = $this->buildTableNameCondition($table);
|
312
|
$condition->compile($this->connection, $this);
|
313
|
// Normally, we would heartily discourage the use of string
|
314
|
// concatenation for conditionals like this however, we
|
315
|
// couldn't use db_select() here because it would prefix
|
316
|
// information_schema.tables and the query would fail.
|
317
|
// Don't use {} around information_schema.tables table.
|
318
|
return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
319
|
}
|
320
|
|
321
|
/**
|
322
|
* Find all tables that are like the specified base table name.
|
323
|
*
|
324
|
* @param $table_expression
|
325
|
* An SQL expression, for example "simpletest%" (without the quotes).
|
326
|
* BEWARE: this is not prefixed, the caller should take care of that.
|
327
|
*
|
328
|
* @return
|
329
|
* Array, both the keys and the values are the matching tables.
|
330
|
*/
|
331
|
public function findTables($table_expression) {
|
332
|
$condition = $this->buildTableNameCondition($table_expression, 'LIKE', FALSE);
|
333
|
|
334
|
$condition->compile($this->connection, $this);
|
335
|
// Normally, we would heartily discourage the use of string
|
336
|
// concatenation for conditionals like this however, we
|
337
|
// couldn't use db_select() here because it would prefix
|
338
|
// information_schema.tables and the query would fail.
|
339
|
// Don't use {} around information_schema.tables table.
|
340
|
return $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0);
|
341
|
}
|
342
|
|
343
|
/**
|
344
|
* Check if a column exists in the given table.
|
345
|
*
|
346
|
* @param $table
|
347
|
* The name of the table in drupal (no prefixing).
|
348
|
* @param $name
|
349
|
* The name of the column.
|
350
|
*
|
351
|
* @return
|
352
|
* TRUE if the given column exists, otherwise FALSE.
|
353
|
*/
|
354
|
public function fieldExists($table, $column) {
|
355
|
$condition = $this->buildTableNameCondition($table);
|
356
|
$condition->condition('column_name', $column);
|
357
|
$condition->compile($this->connection, $this);
|
358
|
// Normally, we would heartily discourage the use of string
|
359
|
// concatenation for conditionals like this however, we
|
360
|
// couldn't use db_select() here because it would prefix
|
361
|
// information_schema.tables and the query would fail.
|
362
|
// Don't use {} around information_schema.columns table.
|
363
|
return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
* Returns a mapping of Drupal schema field names to DB-native field types.
|
368
|
*
|
369
|
* Because different field types do not map 1:1 between databases, Drupal has
|
370
|
* its own normalized field type names. This function returns a driver-specific
|
371
|
* mapping table from Drupal names to the native names for each database.
|
372
|
*
|
373
|
* @return array
|
374
|
* An array of Schema API field types to driver-specific field types.
|
375
|
*/
|
376
|
abstract public function getFieldTypeMap();
|
377
|
|
378
|
/**
|
379
|
* Rename a table.
|
380
|
*
|
381
|
* @param $table
|
382
|
* The table to be renamed.
|
383
|
* @param $new_name
|
384
|
* The new name for the table.
|
385
|
*
|
386
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
387
|
* If the specified table doesn't exist.
|
388
|
* @throws DatabaseSchemaObjectExistsException
|
389
|
* If a table with the specified new name already exists.
|
390
|
*/
|
391
|
abstract public function renameTable($table, $new_name);
|
392
|
|
393
|
/**
|
394
|
* Drop a table.
|
395
|
*
|
396
|
* @param $table
|
397
|
* The table to be dropped.
|
398
|
*
|
399
|
* @return
|
400
|
* TRUE if the table was successfully dropped, FALSE if there was no table
|
401
|
* by that name to begin with.
|
402
|
*/
|
403
|
abstract public function dropTable($table);
|
404
|
|
405
|
/**
|
406
|
* Add a new field to a table.
|
407
|
*
|
408
|
* @param $table
|
409
|
* Name of the table to be altered.
|
410
|
* @param $field
|
411
|
* Name of the field to be added.
|
412
|
* @param $spec
|
413
|
* The field specification array, as taken from a schema definition.
|
414
|
* The specification may also contain the key 'initial', the newly
|
415
|
* created field will be set to the value of the key in all rows.
|
416
|
* This is most useful for creating NOT NULL columns with no default
|
417
|
* value in existing tables.
|
418
|
* @param $keys_new
|
419
|
* (optional) Keys and indexes specification to be created on the
|
420
|
* table along with adding the field. The format is the same as a
|
421
|
* table specification but without the 'fields' element. If you are
|
422
|
* adding a type 'serial' field, you MUST specify at least one key
|
423
|
* or index including it in this array. See db_change_field() for more
|
424
|
* explanation why.
|
425
|
*
|
426
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
427
|
* If the specified table doesn't exist.
|
428
|
* @throws DatabaseSchemaObjectExistsException
|
429
|
* If the specified table already has a field by that name.
|
430
|
*/
|
431
|
abstract public function addField($table, $field, $spec, $keys_new = array());
|
432
|
|
433
|
/**
|
434
|
* Drop a field.
|
435
|
*
|
436
|
* @param $table
|
437
|
* The table to be altered.
|
438
|
* @param $field
|
439
|
* The field to be dropped.
|
440
|
*
|
441
|
* @return
|
442
|
* TRUE if the field was successfully dropped, FALSE if there was no field
|
443
|
* by that name to begin with.
|
444
|
*/
|
445
|
abstract public function dropField($table, $field);
|
446
|
|
447
|
/**
|
448
|
* Set the default value for a field.
|
449
|
*
|
450
|
* @param $table
|
451
|
* The table to be altered.
|
452
|
* @param $field
|
453
|
* The field to be altered.
|
454
|
* @param $default
|
455
|
* Default value to be set. NULL for 'default NULL'.
|
456
|
*
|
457
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
458
|
* If the specified table or field doesn't exist.
|
459
|
*/
|
460
|
abstract public function fieldSetDefault($table, $field, $default);
|
461
|
|
462
|
/**
|
463
|
* Set a field to have no default value.
|
464
|
*
|
465
|
* @param $table
|
466
|
* The table to be altered.
|
467
|
* @param $field
|
468
|
* The field to be altered.
|
469
|
*
|
470
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
471
|
* If the specified table or field doesn't exist.
|
472
|
*/
|
473
|
abstract public function fieldSetNoDefault($table, $field);
|
474
|
|
475
|
/**
|
476
|
* Checks if an index exists in the given table.
|
477
|
*
|
478
|
* @param $table
|
479
|
* The name of the table in drupal (no prefixing).
|
480
|
* @param $name
|
481
|
* The name of the index in drupal (no prefixing).
|
482
|
*
|
483
|
* @return
|
484
|
* TRUE if the given index exists, otherwise FALSE.
|
485
|
*/
|
486
|
abstract public function indexExists($table, $name);
|
487
|
|
488
|
/**
|
489
|
* Add a primary key.
|
490
|
*
|
491
|
* @param $table
|
492
|
* The table to be altered.
|
493
|
* @param $fields
|
494
|
* Fields for the primary key.
|
495
|
*
|
496
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
497
|
* If the specified table doesn't exist.
|
498
|
* @throws DatabaseSchemaObjectExistsException
|
499
|
* If the specified table already has a primary key.
|
500
|
*/
|
501
|
abstract public function addPrimaryKey($table, $fields);
|
502
|
|
503
|
/**
|
504
|
* Drop the primary key.
|
505
|
*
|
506
|
* @param $table
|
507
|
* The table to be altered.
|
508
|
*
|
509
|
* @return
|
510
|
* TRUE if the primary key was successfully dropped, FALSE if there was no
|
511
|
* primary key on this table to begin with.
|
512
|
*/
|
513
|
abstract public function dropPrimaryKey($table);
|
514
|
|
515
|
/**
|
516
|
* Add a unique key.
|
517
|
*
|
518
|
* @param $table
|
519
|
* The table to be altered.
|
520
|
* @param $name
|
521
|
* The name of the key.
|
522
|
* @param $fields
|
523
|
* An array of field names.
|
524
|
*
|
525
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
526
|
* If the specified table doesn't exist.
|
527
|
* @throws DatabaseSchemaObjectExistsException
|
528
|
* If the specified table already has a key by that name.
|
529
|
*/
|
530
|
abstract public function addUniqueKey($table, $name, $fields);
|
531
|
|
532
|
/**
|
533
|
* Drop a unique key.
|
534
|
*
|
535
|
* @param $table
|
536
|
* The table to be altered.
|
537
|
* @param $name
|
538
|
* The name of the key.
|
539
|
*
|
540
|
* @return
|
541
|
* TRUE if the key was successfully dropped, FALSE if there was no key by
|
542
|
* that name to begin with.
|
543
|
*/
|
544
|
abstract public function dropUniqueKey($table, $name);
|
545
|
|
546
|
/**
|
547
|
* Add an index.
|
548
|
*
|
549
|
* @param $table
|
550
|
* The table to be altered.
|
551
|
* @param $name
|
552
|
* The name of the index.
|
553
|
* @param $fields
|
554
|
* An array of field names.
|
555
|
*
|
556
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
557
|
* If the specified table doesn't exist.
|
558
|
* @throws DatabaseSchemaObjectExistsException
|
559
|
* If the specified table already has an index by that name.
|
560
|
*/
|
561
|
abstract public function addIndex($table, $name, $fields);
|
562
|
|
563
|
/**
|
564
|
* Drop an index.
|
565
|
*
|
566
|
* @param $table
|
567
|
* The table to be altered.
|
568
|
* @param $name
|
569
|
* The name of the index.
|
570
|
*
|
571
|
* @return
|
572
|
* TRUE if the index was successfully dropped, FALSE if there was no index
|
573
|
* by that name to begin with.
|
574
|
*/
|
575
|
abstract public function dropIndex($table, $name);
|
576
|
|
577
|
/**
|
578
|
* Change a field definition.
|
579
|
*
|
580
|
* IMPORTANT NOTE: To maintain database portability, you have to explicitly
|
581
|
* recreate all indices and primary keys that are using the changed field.
|
582
|
*
|
583
|
* That means that you have to drop all affected keys and indexes with
|
584
|
* db_drop_{primary_key,unique_key,index}() before calling db_change_field().
|
585
|
* To recreate the keys and indices, pass the key definitions as the
|
586
|
* optional $keys_new argument directly to db_change_field().
|
587
|
*
|
588
|
* For example, suppose you have:
|
589
|
* @code
|
590
|
* $schema['foo'] = array(
|
591
|
* 'fields' => array(
|
592
|
* 'bar' => array('type' => 'int', 'not null' => TRUE)
|
593
|
* ),
|
594
|
* 'primary key' => array('bar')
|
595
|
* );
|
596
|
* @endcode
|
597
|
* and you want to change foo.bar to be type serial, leaving it as the
|
598
|
* primary key. The correct sequence is:
|
599
|
* @code
|
600
|
* db_drop_primary_key('foo');
|
601
|
* db_change_field('foo', 'bar', 'bar',
|
602
|
* array('type' => 'serial', 'not null' => TRUE),
|
603
|
* array('primary key' => array('bar')));
|
604
|
* @endcode
|
605
|
*
|
606
|
* The reasons for this are due to the different database engines:
|
607
|
*
|
608
|
* On PostgreSQL, changing a field definition involves adding a new field
|
609
|
* and dropping an old one which* causes any indices, primary keys and
|
610
|
* sequences (from serial-type fields) that use the changed field to be dropped.
|
611
|
*
|
612
|
* On MySQL, all type 'serial' fields must be part of at least one key
|
613
|
* or index as soon as they are created. You cannot use
|
614
|
* db_add_{primary_key,unique_key,index}() for this purpose because
|
615
|
* the ALTER TABLE command will fail to add the column without a key
|
616
|
* or index specification. The solution is to use the optional
|
617
|
* $keys_new argument to create the key or index at the same time as
|
618
|
* field.
|
619
|
*
|
620
|
* You could use db_add_{primary_key,unique_key,index}() in all cases
|
621
|
* unless you are converting a field to be type serial. You can use
|
622
|
* the $keys_new argument in all cases.
|
623
|
*
|
624
|
* @param $table
|
625
|
* Name of the table.
|
626
|
* @param $field
|
627
|
* Name of the field to change.
|
628
|
* @param $field_new
|
629
|
* New name for the field (set to the same as $field if you don't want to change the name).
|
630
|
* @param $spec
|
631
|
* The field specification for the new field.
|
632
|
* @param $keys_new
|
633
|
* (optional) Keys and indexes specification to be created on the
|
634
|
* table along with changing the field. The format is the same as a
|
635
|
* table specification but without the 'fields' element.
|
636
|
*
|
637
|
* @throws DatabaseSchemaObjectDoesNotExistException
|
638
|
* If the specified table or source field doesn't exist.
|
639
|
* @throws DatabaseSchemaObjectExistsException
|
640
|
* If the specified destination field already exists.
|
641
|
*/
|
642
|
abstract public function changeField($table, $field, $field_new, $spec, $keys_new = array());
|
643
|
|
644
|
/**
|
645
|
* Create a new table from a Drupal table definition.
|
646
|
*
|
647
|
* @param $name
|
648
|
* The name of the table to create.
|
649
|
* @param $table
|
650
|
* A Schema API table definition array.
|
651
|
*
|
652
|
* @throws DatabaseSchemaObjectExistsException
|
653
|
* If the specified table already exists.
|
654
|
*/
|
655
|
public function createTable($name, $table) {
|
656
|
if ($this->tableExists($name)) {
|
657
|
throw new DatabaseSchemaObjectExistsException(t('Table @name already exists.', array('@name' => $name)));
|
658
|
}
|
659
|
$statements = $this->createTableSql($name, $table);
|
660
|
foreach ($statements as $statement) {
|
661
|
$this->connection->query($statement);
|
662
|
}
|
663
|
}
|
664
|
|
665
|
/**
|
666
|
* Return an array of field names from an array of key/index column specifiers.
|
667
|
*
|
668
|
* This is usually an identity function but if a key/index uses a column prefix
|
669
|
* specification, this function extracts just the name.
|
670
|
*
|
671
|
* @param $fields
|
672
|
* An array of key/index column specifiers.
|
673
|
*
|
674
|
* @return
|
675
|
* An array of field names.
|
676
|
*/
|
677
|
public function fieldNames($fields) {
|
678
|
$return = array();
|
679
|
foreach ($fields as $field) {
|
680
|
if (is_array($field)) {
|
681
|
$return[] = $field[0];
|
682
|
}
|
683
|
else {
|
684
|
$return[] = $field;
|
685
|
}
|
686
|
}
|
687
|
return $return;
|
688
|
}
|
689
|
|
690
|
/**
|
691
|
* Prepare a table or column comment for database query.
|
692
|
*
|
693
|
* @param $comment
|
694
|
* The comment string to prepare.
|
695
|
* @param $length
|
696
|
* Optional upper limit on the returned string length.
|
697
|
*
|
698
|
* @return
|
699
|
* The prepared comment.
|
700
|
*/
|
701
|
public function prepareComment($comment, $length = NULL) {
|
702
|
return $this->connection->quote($comment);
|
703
|
}
|
704
|
}
|
705
|
|
706
|
/**
|
707
|
* Exception thrown if an object being created already exists.
|
708
|
*
|
709
|
* For example, this exception should be thrown whenever there is an attempt to
|
710
|
* create a new database table, field, or index that already exists in the
|
711
|
* database schema.
|
712
|
*/
|
713
|
class DatabaseSchemaObjectExistsException extends Exception {}
|
714
|
|
715
|
/**
|
716
|
* Exception thrown if an object being modified doesn't exist yet.
|
717
|
*
|
718
|
* For example, this exception should be thrown whenever there is an attempt to
|
719
|
* modify a database table, field, or index that does not currently exist in
|
720
|
* the database schema.
|
721
|
*/
|
722
|
class DatabaseSchemaObjectDoesNotExistException extends Exception {}
|
723
|
|
724
|
/**
|
725
|
* @} End of "defgroup schemaapi".
|
726
|
*/
|
727
|
|