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