Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Database;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Database\Query\Condition;
|
Chris@0
|
6 use Drupal\Core\Database\Query\PlaceholderInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a base implementation for Database Schema.
|
Chris@0
|
10 */
|
Chris@0
|
11 abstract class Schema implements PlaceholderInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * The database connection.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var \Drupal\Core\Database\Connection
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $connection;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The placeholder counter.
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $placeholder = 0;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Definition of prefixInfo array structure.
|
Chris@0
|
27 *
|
Chris@0
|
28 * Rather than redefining DatabaseSchema::getPrefixInfo() for each driver,
|
Chris@0
|
29 * by defining the defaultSchema variable only MySQL has to re-write the
|
Chris@0
|
30 * method.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @see DatabaseSchema::getPrefixInfo()
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $defaultSchema = 'public';
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * A unique identifier for this query object.
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $uniqueIdentifier;
|
Chris@0
|
40
|
Chris@0
|
41 public function __construct($connection) {
|
Chris@0
|
42 $this->uniqueIdentifier = uniqid('', TRUE);
|
Chris@0
|
43 $this->connection = $connection;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Implements the magic __clone function.
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __clone() {
|
Chris@0
|
50 $this->uniqueIdentifier = uniqid('', TRUE);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * {@inheritdoc}
|
Chris@0
|
55 */
|
Chris@0
|
56 public function uniqueIdentifier() {
|
Chris@0
|
57 return $this->uniqueIdentifier;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function nextPlaceholder() {
|
Chris@0
|
64 return $this->placeholder++;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Get information about the table name and schema from the prefix.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param
|
Chris@0
|
71 * Name of table to look prefix up for. Defaults to 'default' because that's
|
Chris@0
|
72 * default key for prefix.
|
Chris@0
|
73 * @param $add_prefix
|
Chris@0
|
74 * Boolean that indicates whether the given table name should be prefixed.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return
|
Chris@0
|
77 * A keyed array with information about the schema, table name and prefix.
|
Chris@0
|
78 */
|
Chris@0
|
79 protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
|
Chris@0
|
80 $info = [
|
Chris@0
|
81 'schema' => $this->defaultSchema,
|
Chris@0
|
82 'prefix' => $this->connection->tablePrefix($table),
|
Chris@0
|
83 ];
|
Chris@0
|
84 if ($add_prefix) {
|
Chris@0
|
85 $table = $info['prefix'] . $table;
|
Chris@0
|
86 }
|
Chris@0
|
87 // If the prefix contains a period in it, then that means the prefix also
|
Chris@0
|
88 // contains a schema reference in which case we will change the schema key
|
Chris@0
|
89 // to the value before the period in the prefix. Everything after the dot
|
Chris@0
|
90 // will be prefixed onto the front of the table.
|
Chris@0
|
91 if (($pos = strpos($table, '.')) !== FALSE) {
|
Chris@0
|
92 // Grab everything before the period.
|
Chris@0
|
93 $info['schema'] = substr($table, 0, $pos);
|
Chris@0
|
94 // Grab everything after the dot.
|
Chris@0
|
95 $info['table'] = substr($table, ++$pos);
|
Chris@0
|
96 }
|
Chris@0
|
97 else {
|
Chris@0
|
98 $info['table'] = $table;
|
Chris@0
|
99 }
|
Chris@0
|
100 return $info;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Create names for indexes, primary keys and constraints.
|
Chris@0
|
105 *
|
Chris@0
|
106 * This prevents using {} around non-table names like indexes and keys.
|
Chris@0
|
107 */
|
Chris@0
|
108 public function prefixNonTable($table) {
|
Chris@0
|
109 $args = func_get_args();
|
Chris@0
|
110 $info = $this->getPrefixInfo($table);
|
Chris@0
|
111 $args[0] = $info['table'];
|
Chris@0
|
112 return implode('_', $args);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Build a condition to match a table name against a standard information_schema.
|
Chris@0
|
117 *
|
Chris@0
|
118 * The information_schema is a SQL standard that provides information about the
|
Chris@0
|
119 * database server and the databases, schemas, tables, columns and users within
|
Chris@0
|
120 * it. This makes information_schema a useful tool to use across the drupal
|
Chris@0
|
121 * database drivers and is used by a few different functions. The function below
|
Chris@0
|
122 * describes the conditions to be meet when querying information_schema.tables
|
Chris@0
|
123 * for drupal tables or information associated with drupal tables. Even though
|
Chris@0
|
124 * this is the standard method, not all databases follow standards and so this
|
Chris@0
|
125 * method should be overwritten by a database driver if the database provider
|
Chris@0
|
126 * uses alternate methods. Because information_schema.tables is used in a few
|
Chris@0
|
127 * different functions, a database driver will only need to override this function
|
Chris@0
|
128 * to make all the others work. For example see
|
Chris@0
|
129 * core/includes/databases/mysql/schema.inc.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @param $table_name
|
Chris@0
|
132 * The name of the table in question.
|
Chris@0
|
133 * @param $operator
|
Chris@0
|
134 * The operator to apply on the 'table' part of the condition.
|
Chris@0
|
135 * @param $add_prefix
|
Chris@0
|
136 * Boolean to indicate whether the table name needs to be prefixed.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return \Drupal\Core\Database\Query\Condition
|
Chris@0
|
139 * A Condition object.
|
Chris@0
|
140 */
|
Chris@0
|
141 protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
|
Chris@0
|
142 $info = $this->connection->getConnectionOptions();
|
Chris@0
|
143
|
Chris@0
|
144 // Retrieve the table name and schema
|
Chris@0
|
145 $table_info = $this->getPrefixInfo($table_name, $add_prefix);
|
Chris@0
|
146
|
Chris@0
|
147 $condition = new Condition('AND');
|
Chris@0
|
148 $condition->condition('table_catalog', $info['database']);
|
Chris@0
|
149 $condition->condition('table_schema', $table_info['schema']);
|
Chris@0
|
150 $condition->condition('table_name', $table_info['table'], $operator);
|
Chris@0
|
151 return $condition;
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * Check if a table exists.
|
Chris@0
|
156 *
|
Chris@0
|
157 * @param $table
|
Chris@0
|
158 * The name of the table in drupal (no prefixing).
|
Chris@0
|
159 *
|
Chris@0
|
160 * @return
|
Chris@0
|
161 * TRUE if the given table exists, otherwise FALSE.
|
Chris@0
|
162 */
|
Chris@0
|
163 public function tableExists($table) {
|
Chris@0
|
164 $condition = $this->buildTableNameCondition($table);
|
Chris@0
|
165 $condition->compile($this->connection, $this);
|
Chris@0
|
166 // Normally, we would heartily discourage the use of string
|
Chris@0
|
167 // concatenation for conditionals like this however, we
|
Chris@0
|
168 // couldn't use db_select() here because it would prefix
|
Chris@0
|
169 // information_schema.tables and the query would fail.
|
Chris@0
|
170 // Don't use {} around information_schema.tables table.
|
Chris@0
|
171 return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Finds all tables that are like the specified base table name.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @param string $table_expression
|
Chris@0
|
178 * An SQL expression, for example "cache_%" (without the quotes).
|
Chris@0
|
179 *
|
Chris@0
|
180 * @return array
|
Chris@0
|
181 * Both the keys and the values are the matching tables.
|
Chris@0
|
182 */
|
Chris@0
|
183 public function findTables($table_expression) {
|
Chris@0
|
184 // Load all the tables up front in order to take into account per-table
|
Chris@0
|
185 // prefixes. The actual matching is done at the bottom of the method.
|
Chris@0
|
186 $condition = $this->buildTableNameCondition('%', 'LIKE');
|
Chris@0
|
187 $condition->compile($this->connection, $this);
|
Chris@0
|
188
|
Chris@0
|
189 $individually_prefixed_tables = $this->connection->getUnprefixedTablesMap();
|
Chris@0
|
190 $default_prefix = $this->connection->tablePrefix();
|
Chris@0
|
191 $default_prefix_length = strlen($default_prefix);
|
Chris@0
|
192 $tables = [];
|
Chris@0
|
193 // Normally, we would heartily discourage the use of string
|
Chris@0
|
194 // concatenation for conditionals like this however, we
|
Chris@0
|
195 // couldn't use db_select() here because it would prefix
|
Chris@0
|
196 // information_schema.tables and the query would fail.
|
Chris@0
|
197 // Don't use {} around information_schema.tables table.
|
Chris@0
|
198 $results = $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments());
|
Chris@0
|
199 foreach ($results as $table) {
|
Chris@0
|
200 // Take into account tables that have an individual prefix.
|
Chris@0
|
201 if (isset($individually_prefixed_tables[$table->table_name])) {
|
Chris@0
|
202 $prefix_length = strlen($this->connection->tablePrefix($individually_prefixed_tables[$table->table_name]));
|
Chris@0
|
203 }
|
Chris@0
|
204 elseif ($default_prefix && substr($table->table_name, 0, $default_prefix_length) !== $default_prefix) {
|
Chris@0
|
205 // This table name does not start the default prefix, which means that
|
Chris@0
|
206 // it is not managed by Drupal so it should be excluded from the result.
|
Chris@0
|
207 continue;
|
Chris@0
|
208 }
|
Chris@0
|
209 else {
|
Chris@0
|
210 $prefix_length = $default_prefix_length;
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 // Remove the prefix from the returned tables.
|
Chris@0
|
214 $unprefixed_table_name = substr($table->table_name, $prefix_length);
|
Chris@0
|
215
|
Chris@0
|
216 // The pattern can match a table which is the same as the prefix. That
|
Chris@0
|
217 // will become an empty string when we remove the prefix, which will
|
Chris@0
|
218 // probably surprise the caller, besides not being a prefixed table. So
|
Chris@0
|
219 // remove it.
|
Chris@0
|
220 if (!empty($unprefixed_table_name)) {
|
Chris@0
|
221 $tables[$unprefixed_table_name] = $unprefixed_table_name;
|
Chris@0
|
222 }
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 // Convert the table expression from its SQL LIKE syntax to a regular
|
Chris@0
|
226 // expression and escape the delimiter that will be used for matching.
|
Chris@0
|
227 $table_expression = str_replace(['%', '_'], ['.*?', '.'], preg_quote($table_expression, '/'));
|
Chris@0
|
228 $tables = preg_grep('/^' . $table_expression . '$/i', $tables);
|
Chris@0
|
229
|
Chris@0
|
230 return $tables;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 /**
|
Chris@0
|
234 * Check if a column exists in the given table.
|
Chris@0
|
235 *
|
Chris@0
|
236 * @param $table
|
Chris@0
|
237 * The name of the table in drupal (no prefixing).
|
Chris@0
|
238 * @param $name
|
Chris@0
|
239 * The name of the column.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @return
|
Chris@0
|
242 * TRUE if the given column exists, otherwise FALSE.
|
Chris@0
|
243 */
|
Chris@0
|
244 public function fieldExists($table, $column) {
|
Chris@0
|
245 $condition = $this->buildTableNameCondition($table);
|
Chris@0
|
246 $condition->condition('column_name', $column);
|
Chris@0
|
247 $condition->compile($this->connection, $this);
|
Chris@0
|
248 // Normally, we would heartily discourage the use of string
|
Chris@0
|
249 // concatenation for conditionals like this however, we
|
Chris@0
|
250 // couldn't use db_select() here because it would prefix
|
Chris@0
|
251 // information_schema.tables and the query would fail.
|
Chris@0
|
252 // Don't use {} around information_schema.columns table.
|
Chris@0
|
253 return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 /**
|
Chris@0
|
257 * Returns a mapping of Drupal schema field names to DB-native field types.
|
Chris@0
|
258 *
|
Chris@0
|
259 * Because different field types do not map 1:1 between databases, Drupal has
|
Chris@0
|
260 * its own normalized field type names. This function returns a driver-specific
|
Chris@0
|
261 * mapping table from Drupal names to the native names for each database.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @return array
|
Chris@0
|
264 * An array of Schema API field types to driver-specific field types.
|
Chris@0
|
265 */
|
Chris@0
|
266 abstract public function getFieldTypeMap();
|
Chris@0
|
267
|
Chris@0
|
268 /**
|
Chris@0
|
269 * Rename a table.
|
Chris@0
|
270 *
|
Chris@0
|
271 * @param $table
|
Chris@0
|
272 * The table to be renamed.
|
Chris@0
|
273 * @param $new_name
|
Chris@0
|
274 * The new name for the table.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
277 * If the specified table doesn't exist.
|
Chris@0
|
278 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
279 * If a table with the specified new name already exists.
|
Chris@0
|
280 */
|
Chris@0
|
281 abstract public function renameTable($table, $new_name);
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Drop a table.
|
Chris@0
|
285 *
|
Chris@0
|
286 * @param $table
|
Chris@0
|
287 * The table to be dropped.
|
Chris@0
|
288 *
|
Chris@0
|
289 * @return
|
Chris@0
|
290 * TRUE if the table was successfully dropped, FALSE if there was no table
|
Chris@0
|
291 * by that name to begin with.
|
Chris@0
|
292 */
|
Chris@0
|
293 abstract public function dropTable($table);
|
Chris@0
|
294
|
Chris@0
|
295 /**
|
Chris@0
|
296 * Add a new field to a table.
|
Chris@0
|
297 *
|
Chris@0
|
298 * @param $table
|
Chris@0
|
299 * Name of the table to be altered.
|
Chris@0
|
300 * @param $field
|
Chris@0
|
301 * Name of the field to be added.
|
Chris@0
|
302 * @param $spec
|
Chris@0
|
303 * The field specification array, as taken from a schema definition.
|
Chris@0
|
304 * The specification may also contain the key 'initial', the newly
|
Chris@0
|
305 * created field will be set to the value of the key in all rows.
|
Chris@0
|
306 * This is most useful for creating NOT NULL columns with no default
|
Chris@0
|
307 * value in existing tables.
|
Chris@0
|
308 * Alternatively, the 'initial_form_field' key may be used, which will
|
Chris@0
|
309 * auto-populate the new field with values from the specified field.
|
Chris@0
|
310 * @param $keys_new
|
Chris@0
|
311 * (optional) Keys and indexes specification to be created on the
|
Chris@0
|
312 * table along with adding the field. The format is the same as a
|
Chris@0
|
313 * table specification but without the 'fields' element. If you are
|
Chris@0
|
314 * adding a type 'serial' field, you MUST specify at least one key
|
Chris@0
|
315 * or index including it in this array. See db_change_field() for more
|
Chris@0
|
316 * explanation why.
|
Chris@0
|
317 *
|
Chris@0
|
318 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
319 * If the specified table doesn't exist.
|
Chris@0
|
320 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
321 * If the specified table already has a field by that name.
|
Chris@0
|
322 */
|
Chris@0
|
323 abstract public function addField($table, $field, $spec, $keys_new = []);
|
Chris@0
|
324
|
Chris@0
|
325 /**
|
Chris@0
|
326 * Drop a field.
|
Chris@0
|
327 *
|
Chris@0
|
328 * @param $table
|
Chris@0
|
329 * The table to be altered.
|
Chris@0
|
330 * @param $field
|
Chris@0
|
331 * The field to be dropped.
|
Chris@0
|
332 *
|
Chris@0
|
333 * @return
|
Chris@0
|
334 * TRUE if the field was successfully dropped, FALSE if there was no field
|
Chris@0
|
335 * by that name to begin with.
|
Chris@0
|
336 */
|
Chris@0
|
337 abstract public function dropField($table, $field);
|
Chris@0
|
338
|
Chris@0
|
339 /**
|
Chris@0
|
340 * Set the default value for a field.
|
Chris@0
|
341 *
|
Chris@0
|
342 * @param $table
|
Chris@0
|
343 * The table to be altered.
|
Chris@0
|
344 * @param $field
|
Chris@0
|
345 * The field to be altered.
|
Chris@0
|
346 * @param $default
|
Chris@0
|
347 * Default value to be set. NULL for 'default NULL'.
|
Chris@0
|
348 *
|
Chris@0
|
349 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
350 * If the specified table or field doesn't exist.
|
Chris@0
|
351 */
|
Chris@0
|
352 abstract public function fieldSetDefault($table, $field, $default);
|
Chris@0
|
353
|
Chris@0
|
354 /**
|
Chris@0
|
355 * Set a field to have no default value.
|
Chris@0
|
356 *
|
Chris@0
|
357 * @param $table
|
Chris@0
|
358 * The table to be altered.
|
Chris@0
|
359 * @param $field
|
Chris@0
|
360 * The field to be altered.
|
Chris@0
|
361 *
|
Chris@0
|
362 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
363 * If the specified table or field doesn't exist.
|
Chris@0
|
364 */
|
Chris@0
|
365 abstract public function fieldSetNoDefault($table, $field);
|
Chris@0
|
366
|
Chris@0
|
367 /**
|
Chris@0
|
368 * Checks if an index exists in the given table.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @param $table
|
Chris@0
|
371 * The name of the table in drupal (no prefixing).
|
Chris@0
|
372 * @param $name
|
Chris@0
|
373 * The name of the index in drupal (no prefixing).
|
Chris@0
|
374 *
|
Chris@0
|
375 * @return
|
Chris@0
|
376 * TRUE if the given index exists, otherwise FALSE.
|
Chris@0
|
377 */
|
Chris@0
|
378 abstract public function indexExists($table, $name);
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * Add a primary key.
|
Chris@0
|
382 *
|
Chris@0
|
383 * @param $table
|
Chris@0
|
384 * The table to be altered.
|
Chris@0
|
385 * @param $fields
|
Chris@0
|
386 * Fields for the primary key.
|
Chris@0
|
387 *
|
Chris@0
|
388 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
389 * If the specified table doesn't exist.
|
Chris@0
|
390 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
391 * If the specified table already has a primary key.
|
Chris@0
|
392 */
|
Chris@0
|
393 abstract public function addPrimaryKey($table, $fields);
|
Chris@0
|
394
|
Chris@0
|
395 /**
|
Chris@0
|
396 * Drop the primary key.
|
Chris@0
|
397 *
|
Chris@0
|
398 * @param $table
|
Chris@0
|
399 * The table to be altered.
|
Chris@0
|
400 *
|
Chris@0
|
401 * @return
|
Chris@0
|
402 * TRUE if the primary key was successfully dropped, FALSE if there was no
|
Chris@0
|
403 * primary key on this table to begin with.
|
Chris@0
|
404 */
|
Chris@0
|
405 abstract public function dropPrimaryKey($table);
|
Chris@0
|
406
|
Chris@0
|
407 /**
|
Chris@0
|
408 * Add a unique key.
|
Chris@0
|
409 *
|
Chris@0
|
410 * @param $table
|
Chris@0
|
411 * The table to be altered.
|
Chris@0
|
412 * @param $name
|
Chris@0
|
413 * The name of the key.
|
Chris@0
|
414 * @param $fields
|
Chris@0
|
415 * An array of field names.
|
Chris@0
|
416 *
|
Chris@0
|
417 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
418 * If the specified table doesn't exist.
|
Chris@0
|
419 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
420 * If the specified table already has a key by that name.
|
Chris@0
|
421 */
|
Chris@0
|
422 abstract public function addUniqueKey($table, $name, $fields);
|
Chris@0
|
423
|
Chris@0
|
424 /**
|
Chris@0
|
425 * Drop a unique key.
|
Chris@0
|
426 *
|
Chris@0
|
427 * @param $table
|
Chris@0
|
428 * The table to be altered.
|
Chris@0
|
429 * @param $name
|
Chris@0
|
430 * The name of the key.
|
Chris@0
|
431 *
|
Chris@0
|
432 * @return
|
Chris@0
|
433 * TRUE if the key was successfully dropped, FALSE if there was no key by
|
Chris@0
|
434 * that name to begin with.
|
Chris@0
|
435 */
|
Chris@0
|
436 abstract public function dropUniqueKey($table, $name);
|
Chris@0
|
437
|
Chris@0
|
438 /**
|
Chris@0
|
439 * Add an index.
|
Chris@0
|
440 *
|
Chris@0
|
441 * @param $table
|
Chris@0
|
442 * The table to be altered.
|
Chris@0
|
443 * @param $name
|
Chris@0
|
444 * The name of the index.
|
Chris@0
|
445 * @param $fields
|
Chris@0
|
446 * An array of field names or field information; if field information is
|
Chris@0
|
447 * passed, it's an array whose first element is the field name and whose
|
Chris@0
|
448 * second is the maximum length in the index. For example, the following
|
Chris@0
|
449 * will use the full length of the `foo` field, but limit the `bar` field to
|
Chris@0
|
450 * 4 characters:
|
Chris@0
|
451 * @code
|
Chris@0
|
452 * $fields = ['foo', ['bar', 4]];
|
Chris@0
|
453 * @endcode
|
Chris@0
|
454 * @param array $spec
|
Chris@0
|
455 * The table specification for the table to be altered. This is used in
|
Chris@0
|
456 * order to be able to ensure that the index length is not too long.
|
Chris@0
|
457 * This schema definition can usually be obtained through hook_schema(), or
|
Chris@0
|
458 * in case the table was created by the Entity API, through the schema
|
Chris@0
|
459 * handler listed in the entity class definition. For reference, see
|
Chris@0
|
460 * SqlContentEntityStorageSchema::getDedicatedTableSchema() and
|
Chris@0
|
461 * SqlContentEntityStorageSchema::getSharedTableFieldSchema().
|
Chris@0
|
462 *
|
Chris@0
|
463 * In order to prevent human error, it is recommended to pass in the
|
Chris@0
|
464 * complete table specification. However, in the edge case of the complete
|
Chris@0
|
465 * table specification not being available, we can pass in a partial table
|
Chris@0
|
466 * definition containing only the fields that apply to the index:
|
Chris@0
|
467 * @code
|
Chris@0
|
468 * $spec = [
|
Chris@0
|
469 * // Example partial specification for a table:
|
Chris@0
|
470 * 'fields' => [
|
Chris@0
|
471 * 'example_field' => [
|
Chris@0
|
472 * 'description' => 'An example field',
|
Chris@0
|
473 * 'type' => 'varchar',
|
Chris@0
|
474 * 'length' => 32,
|
Chris@0
|
475 * 'not null' => TRUE,
|
Chris@0
|
476 * 'default' => '',
|
Chris@0
|
477 * ],
|
Chris@0
|
478 * ],
|
Chris@0
|
479 * 'indexes' => [
|
Chris@0
|
480 * 'table_example_field' => ['example_field'],
|
Chris@0
|
481 * ],
|
Chris@0
|
482 * ];
|
Chris@0
|
483 * @endcode
|
Chris@0
|
484 * Note that the above is a partial table definition and that we would
|
Chris@0
|
485 * usually pass a complete table definition as obtained through
|
Chris@0
|
486 * hook_schema() instead.
|
Chris@0
|
487 *
|
Chris@0
|
488 * @see schemaapi
|
Chris@0
|
489 * @see hook_schema()
|
Chris@0
|
490 *
|
Chris@0
|
491 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
492 * If the specified table doesn't exist.
|
Chris@0
|
493 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
494 * If the specified table already has an index by that name.
|
Chris@0
|
495 *
|
Chris@0
|
496 * @todo remove the $spec argument whenever schema introspection is added.
|
Chris@0
|
497 */
|
Chris@0
|
498 abstract public function addIndex($table, $name, $fields, array $spec);
|
Chris@0
|
499
|
Chris@0
|
500 /**
|
Chris@0
|
501 * Drop an index.
|
Chris@0
|
502 *
|
Chris@0
|
503 * @param $table
|
Chris@0
|
504 * The table to be altered.
|
Chris@0
|
505 * @param $name
|
Chris@0
|
506 * The name of the index.
|
Chris@0
|
507 *
|
Chris@0
|
508 * @return
|
Chris@0
|
509 * TRUE if the index was successfully dropped, FALSE if there was no index
|
Chris@0
|
510 * by that name to begin with.
|
Chris@0
|
511 */
|
Chris@0
|
512 abstract public function dropIndex($table, $name);
|
Chris@0
|
513
|
Chris@0
|
514 /**
|
Chris@0
|
515 * Change a field definition.
|
Chris@0
|
516 *
|
Chris@0
|
517 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
|
Chris@0
|
518 * recreate all indices and primary keys that are using the changed field.
|
Chris@0
|
519 *
|
Chris@0
|
520 * That means that you have to drop all affected keys and indexes with
|
Chris@0
|
521 * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
|
Chris@0
|
522 * To recreate the keys and indices, pass the key definitions as the
|
Chris@0
|
523 * optional $keys_new argument directly to db_change_field().
|
Chris@0
|
524 *
|
Chris@0
|
525 * For example, suppose you have:
|
Chris@0
|
526 * @code
|
Chris@0
|
527 * $schema['foo'] = array(
|
Chris@0
|
528 * 'fields' => array(
|
Chris@0
|
529 * 'bar' => array('type' => 'int', 'not null' => TRUE)
|
Chris@0
|
530 * ),
|
Chris@0
|
531 * 'primary key' => array('bar')
|
Chris@0
|
532 * );
|
Chris@0
|
533 * @endcode
|
Chris@0
|
534 * and you want to change foo.bar to be type serial, leaving it as the
|
Chris@0
|
535 * primary key. The correct sequence is:
|
Chris@0
|
536 * @code
|
Chris@0
|
537 * db_drop_primary_key('foo');
|
Chris@0
|
538 * db_change_field('foo', 'bar', 'bar',
|
Chris@0
|
539 * array('type' => 'serial', 'not null' => TRUE),
|
Chris@0
|
540 * array('primary key' => array('bar')));
|
Chris@0
|
541 * @endcode
|
Chris@0
|
542 *
|
Chris@0
|
543 * The reasons for this are due to the different database engines:
|
Chris@0
|
544 *
|
Chris@0
|
545 * On PostgreSQL, changing a field definition involves adding a new field
|
Chris@0
|
546 * and dropping an old one which* causes any indices, primary keys and
|
Chris@0
|
547 * sequences (from serial-type fields) that use the changed field to be dropped.
|
Chris@0
|
548 *
|
Chris@0
|
549 * On MySQL, all type 'serial' fields must be part of at least one key
|
Chris@0
|
550 * or index as soon as they are created. You cannot use
|
Chris@0
|
551 * db_add_{primary_key,unique_key,index}() for this purpose because
|
Chris@0
|
552 * the ALTER TABLE command will fail to add the column without a key
|
Chris@0
|
553 * or index specification. The solution is to use the optional
|
Chris@0
|
554 * $keys_new argument to create the key or index at the same time as
|
Chris@0
|
555 * field.
|
Chris@0
|
556 *
|
Chris@0
|
557 * You could use db_add_{primary_key,unique_key,index}() in all cases
|
Chris@0
|
558 * unless you are converting a field to be type serial. You can use
|
Chris@0
|
559 * the $keys_new argument in all cases.
|
Chris@0
|
560 *
|
Chris@0
|
561 * @param $table
|
Chris@0
|
562 * Name of the table.
|
Chris@0
|
563 * @param $field
|
Chris@0
|
564 * Name of the field to change.
|
Chris@0
|
565 * @param $field_new
|
Chris@0
|
566 * New name for the field (set to the same as $field if you don't want to change the name).
|
Chris@0
|
567 * @param $spec
|
Chris@0
|
568 * The field specification for the new field.
|
Chris@0
|
569 * @param $keys_new
|
Chris@0
|
570 * (optional) Keys and indexes specification to be created on the
|
Chris@0
|
571 * table along with changing the field. The format is the same as a
|
Chris@0
|
572 * table specification but without the 'fields' element.
|
Chris@0
|
573 *
|
Chris@0
|
574 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
|
Chris@0
|
575 * If the specified table or source field doesn't exist.
|
Chris@0
|
576 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
577 * If the specified destination field already exists.
|
Chris@0
|
578 */
|
Chris@0
|
579 abstract public function changeField($table, $field, $field_new, $spec, $keys_new = []);
|
Chris@0
|
580
|
Chris@0
|
581 /**
|
Chris@0
|
582 * Create a new table from a Drupal table definition.
|
Chris@0
|
583 *
|
Chris@0
|
584 * @param $name
|
Chris@0
|
585 * The name of the table to create.
|
Chris@0
|
586 * @param $table
|
Chris@0
|
587 * A Schema API table definition array.
|
Chris@0
|
588 *
|
Chris@0
|
589 * @throws \Drupal\Core\Database\SchemaObjectExistsException
|
Chris@0
|
590 * If the specified table already exists.
|
Chris@0
|
591 */
|
Chris@0
|
592 public function createTable($name, $table) {
|
Chris@0
|
593 if ($this->tableExists($name)) {
|
Chris@0
|
594 throw new SchemaObjectExistsException(t('Table @name already exists.', ['@name' => $name]));
|
Chris@0
|
595 }
|
Chris@0
|
596 $statements = $this->createTableSql($name, $table);
|
Chris@0
|
597 foreach ($statements as $statement) {
|
Chris@0
|
598 $this->connection->query($statement);
|
Chris@0
|
599 }
|
Chris@0
|
600 }
|
Chris@0
|
601
|
Chris@0
|
602 /**
|
Chris@0
|
603 * Return an array of field names from an array of key/index column specifiers.
|
Chris@0
|
604 *
|
Chris@0
|
605 * This is usually an identity function but if a key/index uses a column prefix
|
Chris@0
|
606 * specification, this function extracts just the name.
|
Chris@0
|
607 *
|
Chris@0
|
608 * @param $fields
|
Chris@0
|
609 * An array of key/index column specifiers.
|
Chris@0
|
610 *
|
Chris@0
|
611 * @return
|
Chris@0
|
612 * An array of field names.
|
Chris@0
|
613 */
|
Chris@0
|
614 public function fieldNames($fields) {
|
Chris@0
|
615 $return = [];
|
Chris@0
|
616 foreach ($fields as $field) {
|
Chris@0
|
617 if (is_array($field)) {
|
Chris@0
|
618 $return[] = $field[0];
|
Chris@0
|
619 }
|
Chris@0
|
620 else {
|
Chris@0
|
621 $return[] = $field;
|
Chris@0
|
622 }
|
Chris@0
|
623 }
|
Chris@0
|
624 return $return;
|
Chris@0
|
625 }
|
Chris@0
|
626
|
Chris@0
|
627 /**
|
Chris@0
|
628 * Prepare a table or column comment for database query.
|
Chris@0
|
629 *
|
Chris@0
|
630 * @param $comment
|
Chris@0
|
631 * The comment string to prepare.
|
Chris@0
|
632 * @param $length
|
Chris@0
|
633 * Optional upper limit on the returned string length.
|
Chris@0
|
634 *
|
Chris@0
|
635 * @return
|
Chris@0
|
636 * The prepared comment.
|
Chris@0
|
637 */
|
Chris@0
|
638 public function prepareComment($comment, $length = NULL) {
|
Chris@0
|
639 // Remove semicolons to avoid triggering multi-statement check.
|
Chris@0
|
640 $comment = strtr($comment, [';' => '.']);
|
Chris@0
|
641 return $this->connection->quote($comment);
|
Chris@0
|
642 }
|
Chris@0
|
643
|
Chris@0
|
644 /**
|
Chris@0
|
645 * Return an escaped version of its parameter to be used as a default value
|
Chris@0
|
646 * on a column.
|
Chris@0
|
647 *
|
Chris@0
|
648 * @param mixed $value
|
Chris@0
|
649 * The value to be escaped (int, float, null or string).
|
Chris@0
|
650 *
|
Chris@0
|
651 * @return string|int|float
|
Chris@0
|
652 * The escaped value.
|
Chris@0
|
653 */
|
Chris@0
|
654 protected function escapeDefaultValue($value) {
|
Chris@0
|
655 if (is_null($value)) {
|
Chris@0
|
656 return 'NULL';
|
Chris@0
|
657 }
|
Chris@0
|
658 return is_string($value) ? $this->connection->quote($value) : $value;
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 }
|