Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\Core\Database\Driver\mysql;
Dries Buytaert
committed
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\SchemaException;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\Core\Database\SchemaObjectDoesNotExistException;
use Drupal\Core\Database\Schema as DatabaseSchema;
Alex Pott
committed
use Drupal\Component\Utility\Unicode;
Dries Buytaert
committed
/**
* @addtogroup schemaapi
Dries Buytaert
committed
* @{
*/
/**
* MySQL implementation of \Drupal\Core\Database\Schema.
*/
class Schema extends DatabaseSchema {
Dries Buytaert
committed
/**
* Maximum length of a table comment in MySQL.
*/
const COMMENT_MAX_TABLE = 60;
/**
* Maximum length of a column comment in MySQL.
*/
const COMMENT_MAX_COLUMN = 255;
/**
* @var array
* List of MySQL string types.
*/
protected $mysqlStringTypes = [
'VARCHAR',
'CHAR',
'TINYTEXT',
'MEDIUMTEXT',
'LONGTEXT',
'TEXT',
];
Dries Buytaert
committed
* Get information about the table and database name from the prefix.
*
* @return
* A keyed array with information about the database, table name and prefix.
*/
Angie Byron
committed
protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
$info = ['prefix' => $this->connection->tablePrefix($table)];
Angie Byron
committed
if ($add_prefix) {
$table = $info['prefix'] . $table;
}
if (($pos = strpos($table, '.')) !== FALSE) {
$info['database'] = substr($table, 0, $pos);
$info['table'] = substr($table, ++$pos);
}
else {
Angie Byron
committed
$info['database'] = $this->connection->getConnectionOptions()['database'];
Angie Byron
committed
$info['table'] = $table;
}
return $info;
}
/**
* Build a condition to match a table name against a standard information_schema.
*
* MySQL uses databases like schemas rather than catalogs so when we build
* a condition to query the information_schema.tables, we set the default
* database as the schema unless specified otherwise, and exclude table_catalog
* from the condition criteria.
*/
Angie Byron
committed
protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
$table_info = $this->getPrefixInfo($table_name, $add_prefix);
Dries Buytaert
committed
$condition = new Condition('AND');
$condition->condition('table_schema', $table_info['database']);
$condition->condition('table_name', $table_info['table'], $operator);
return $condition;
}
Dries Buytaert
committed
/**
* Generate SQL to create a new table from a Drupal schema definition.
*
* @param $name
* The name of the table to create.
* @param $table
* A Schema API table definition array.
* @return
* An array of SQL statements to create the table.
*/
protected function createTableSql($name, $table) {
Dries Buytaert
committed
$info = $this->connection->getConnectionOptions();
// Provide defaults if needed.
$table += [
Angie Byron
committed
'mysql_engine' => 'InnoDB',
'mysql_character_set' => 'utf8mb4',
];
Dries Buytaert
committed
$sql = "CREATE TABLE {" . $name . "} (\n";
Dries Buytaert
committed
// Add the SQL statement for each field.
foreach ($table['fields'] as $field_name => $field) {
$sql .= $this->createFieldSql($field_name, $this->processField($field)) . ", \n";
}
Dries Buytaert
committed
// Process keys & indexes.
if (!empty($table['primary key']) && is_array($table['primary key'])) {
$this->ensureNotNullPrimaryKey($table['primary key'], $table['fields']);
}
Dries Buytaert
committed
$keys = $this->createKeysSql($table);
if (count($keys)) {
$sql .= implode(", \n", $keys) . ", \n";
}
Dries Buytaert
committed
// Remove the last comma and space.
$sql = substr($sql, 0, -3) . "\n) ";
Angie Byron
committed
$sql .= 'ENGINE = ' . $table['mysql_engine'] . ' DEFAULT CHARACTER SET ' . $table['mysql_character_set'];
Dries Buytaert
committed
// By default, MySQL uses the default collation for new tables, which is
// 'utf8mb4_general_ci' (MySQL 5) or 'utf8mb4_0900_ai_ci' (MySQL 8) for
// utf8mb4. If an alternate collation has been set, it needs to be
// explicitly specified.
// @see \Drupal\Core\Database\Driver\mysql\Schema
Dries Buytaert
committed
if (!empty($info['collation'])) {
$sql .= ' COLLATE ' . $info['collation'];
}
// Add table comment.
if (!empty($table['description'])) {
$sql .= ' COMMENT ' . $this->prepareComment($table['description'], self::COMMENT_MAX_TABLE);
}
return [$sql];
Dries Buytaert
committed
}
/**
* Create an SQL string for a field to be used in table creation or alteration.
*
* Before passing a field out of a schema definition into this function it has
* to be processed by _db_process_field().
*
* @param string $name
Dries Buytaert
committed
* Name of the field.
* @param array $spec
Dries Buytaert
committed
* The field specification, as per the schema data structure format.
*/
protected function createFieldSql($name, $spec) {
$sql = "`" . $name . "` " . $spec['mysql_type'];
if (in_array($spec['mysql_type'], $this->mysqlStringTypes)) {
if (isset($spec['length'])) {
$sql .= '(' . $spec['length'] . ')';
}
if (isset($spec['type']) && $spec['type'] == 'varchar_ascii') {
$sql .= ' CHARACTER SET ascii';
}
if (!empty($spec['binary'])) {
$sql .= ' BINARY';
}
// Note we check for the "type" key here. "mysql_type" is VARCHAR:
elseif (isset($spec['type']) && $spec['type'] == 'varchar_ascii') {
$sql .= ' COLLATE ascii_general_ci';
}
Dries Buytaert
committed
}
elseif (isset($spec['precision']) && isset($spec['scale'])) {
$sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
}
Dries Buytaert
committed
if (!empty($spec['unsigned'])) {
$sql .= ' unsigned';
}
Angie Byron
committed
if (isset($spec['not null'])) {
if ($spec['not null']) {
$sql .= ' NOT NULL';
}
else {
$sql .= ' NULL';
}
Dries Buytaert
committed
}
Dries Buytaert
committed
if (!empty($spec['auto_increment'])) {
$sql .= ' auto_increment';
}
// $spec['default'] can be NULL, so we explicitly check for the key here.
Dries Buytaert
committed
if (array_key_exists('default', $spec)) {
$sql .= ' DEFAULT ' . $this->escapeDefaultValue($spec['default']);
Dries Buytaert
committed
}
Dries Buytaert
committed
if (empty($spec['not null']) && !isset($spec['default'])) {
$sql .= ' DEFAULT NULL';
}
// Add column comment.
if (!empty($spec['description'])) {
$sql .= ' COMMENT ' . $this->prepareComment($spec['description'], self::COMMENT_MAX_COLUMN);
}
Dries Buytaert
committed
return $sql;
}
Dries Buytaert
committed
/**
* Set database-engine specific properties for a field.
*
* @param $field
* A field description array, as specified in the schema documentation.
*/
protected function processField($field) {
Dries Buytaert
committed
if (!isset($field['size'])) {
$field['size'] = 'normal';
}
Dries Buytaert
committed
// Set the correct database-engine specific datatype.
Angie Byron
committed
// In case one is already provided, force it to uppercase.
if (isset($field['mysql_type'])) {
$field['mysql_type'] = mb_strtoupper($field['mysql_type']);
Angie Byron
committed
}
else {
$map = $this->getFieldTypeMap();
Dries Buytaert
committed
$field['mysql_type'] = $map[$field['type'] . ':' . $field['size']];
}
Angie Byron
committed
if (isset($field['type']) && $field['type'] == 'serial') {
Dries Buytaert
committed
$field['auto_increment'] = TRUE;
}
Dries Buytaert
committed
return $field;
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function getFieldTypeMap() {
Dries Buytaert
committed
// Put :normal last so it gets preserved by array_flip. This makes
Dries Buytaert
committed
// it much easier for modules (such as schema.module) to map
// database types back into schema types.
// $map does not use drupal_static as its value never changes.
static $map = [
'varchar_ascii:normal' => 'VARCHAR',
Dries Buytaert
committed
'varchar:normal' => 'VARCHAR',
'char:normal' => 'CHAR',
Dries Buytaert
committed
'text:tiny' => 'TINYTEXT',
'text:small' => 'TINYTEXT',
'text:medium' => 'MEDIUMTEXT',
'text:big' => 'LONGTEXT',
'text:normal' => 'TEXT',
Dries Buytaert
committed
'serial:tiny' => 'TINYINT',
'serial:small' => 'SMALLINT',
'serial:medium' => 'MEDIUMINT',
'serial:big' => 'BIGINT',
'serial:normal' => 'INT',
Dries Buytaert
committed
'int:tiny' => 'TINYINT',
'int:small' => 'SMALLINT',
'int:medium' => 'MEDIUMINT',
'int:big' => 'BIGINT',
'int:normal' => 'INT',
Dries Buytaert
committed
'float:tiny' => 'FLOAT',
'float:small' => 'FLOAT',
'float:medium' => 'FLOAT',
'float:big' => 'DOUBLE',
'float:normal' => 'FLOAT',
Dries Buytaert
committed
'numeric:normal' => 'DECIMAL',
Dries Buytaert
committed
'blob:big' => 'LONGBLOB',
'blob:normal' => 'BLOB',
];
Dries Buytaert
committed
return $map;
}
Dries Buytaert
committed
protected function createKeysSql($spec) {
$keys = [];
Dries Buytaert
committed
if (!empty($spec['primary key'])) {
$keys[] = 'PRIMARY KEY (' . $this->createKeySql($spec['primary key']) . ')';
Dries Buytaert
committed
}
if (!empty($spec['unique keys'])) {
foreach ($spec['unique keys'] as $key => $fields) {
$keys[] = 'UNIQUE KEY `' . $key . '` (' . $this->createKeySql($fields) . ')';
Dries Buytaert
committed
}
}
if (!empty($spec['indexes'])) {
$indexes = $this->getNormalizedIndexes($spec);
foreach ($indexes as $index => $fields) {
$keys[] = 'INDEX `' . $index . '` (' . $this->createKeySql($fields) . ')';
Dries Buytaert
committed
}
}
return $keys;
}
/**
* Gets normalized indexes from a table specification.
*
* Shortens indexes to 191 characters if they apply to utf8mb4-encoded
* fields, in order to comply with the InnoDB index limitation of 756 bytes.
*
* @param array $spec
* The table specification.
*
* @return array
* List of shortened indexes.
*
* @throws \Drupal\Core\Database\SchemaException
* Thrown if field specification is missing.
*/
protected function getNormalizedIndexes(array $spec) {
$indexes = isset($spec['indexes']) ? $spec['indexes'] : [];
foreach ($indexes as $index_name => $index_fields) {
foreach ($index_fields as $index_key => $index_field) {
// Get the name of the field from the index specification.
$field_name = is_array($index_field) ? $index_field[0] : $index_field;
// Check whether the field is defined in the table specification.
if (isset($spec['fields'][$field_name])) {
// Get the MySQL type from the processed field.
$mysql_field = $this->processField($spec['fields'][$field_name]);
if (in_array($mysql_field['mysql_type'], $this->mysqlStringTypes)) {
// Check whether we need to shorten the index.
if ((!isset($mysql_field['type']) || $mysql_field['type'] != 'varchar_ascii') && (!isset($mysql_field['length']) || $mysql_field['length'] > 191)) {
// Limit the index length to 191 characters.
$this->shortenIndex($indexes[$index_name][$index_key]);
}
}
}
else {
throw new SchemaException("MySQL needs the '$field_name' field specification in order to normalize the '$index_name' index");
}
}
}
return $indexes;
}
/**
* Helper function for normalizeIndexes().
*
* Shortens an index to 191 characters.
*
* @param array $index
* The index array to be used in createKeySql.
*
* @see Drupal\Core\Database\Driver\mysql\Schema::createKeySql()
* @see Drupal\Core\Database\Driver\mysql\Schema::normalizeIndexes()
*/
protected function shortenIndex(&$index) {
if (is_array($index)) {
if ($index[1] > 191) {
$index[1] = 191;
}
}
else {
$index = [$index, 191];
}
}
Dries Buytaert
committed
protected function createKeySql($fields) {
$return = [];
Dries Buytaert
committed
foreach ($fields as $field) {
if (is_array($field)) {
Dries Buytaert
committed
$return[] = '`' . $field[0] . '`(' . $field[1] . ')';
Dries Buytaert
committed
}
else {
Dries Buytaert
committed
$return[] = '`' . $field . '`';
Dries Buytaert
committed
}
}
Dries Buytaert
committed
return implode(', ', $return);
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function renameTable($table, $new_name) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
throw new SchemaObjectDoesNotExistException(t("Cannot rename @table to @table_new: table @table doesn't exist.", ['@table' => $table, '@table_new' => $new_name]));
Dries Buytaert
committed
}
if ($this->tableExists($new_name)) {
throw new SchemaObjectExistsException(t("Cannot rename @table to @table_new: table @table_new already exists.", ['@table' => $table, '@table_new' => $new_name]));
Dries Buytaert
committed
}
$info = $this->getPrefixInfo($new_name);
return $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO `' . $info['table'] . '`');
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function dropTable($table) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
return FALSE;
}
Dries Buytaert
committed
$this->connection->query('DROP TABLE {' . $table . '}');
Dries Buytaert
committed
return TRUE;
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
public function addField($table, $field, $spec, $keys_new = []) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", ['@field' => $field, '@table' => $table]));
Dries Buytaert
committed
}
Dries Buytaert
committed
if ($this->fieldExists($table, $field)) {
throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", ['@field' => $field, '@table' => $table]));
Dries Buytaert
committed
}
// Fields that are part of a PRIMARY KEY must be added as NOT NULL.
$is_primary_key = isset($keys_new['primary key']) && in_array($field, $keys_new['primary key'], TRUE);
if ($is_primary_key) {
$this->ensureNotNullPrimaryKey($keys_new['primary key'], [$field => $spec]);
}
Dries Buytaert
committed
$fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default']) && !$is_primary_key) {
Dries Buytaert
committed
$fixnull = TRUE;
$spec['not null'] = FALSE;
}
$query = 'ALTER TABLE {' . $table . '} ADD ';
$query .= $this->createFieldSql($field, $this->processField($spec));
Dries Buytaert
committed
if ($keys_sql = $this->createKeysSql($keys_new)) {
// Make sure to drop the existing primary key before adding a new one.
// This is only needed when adding a field because this method, unlike
// changeField(), is supposed to handle primary keys automatically.
if (isset($keys_new['primary key']) && $this->indexExists($table, 'PRIMARY')) {
$query .= ', DROP PRIMARY KEY';
}
Dries Buytaert
committed
$query .= ', ADD ' . implode(', ADD ', $keys_sql);
Dries Buytaert
committed
}
Dries Buytaert
committed
$this->connection->query($query);
Alex Pott
committed
if (isset($spec['initial_from_field'])) {
if (isset($spec['initial'])) {
$expression = 'COALESCE(' . $spec['initial_from_field'] . ', :default_initial_value)';
$arguments = [':default_initial_value' => $spec['initial']];
}
else {
$expression = $spec['initial_from_field'];
$arguments = [];
}
Alex Pott
committed
$this->connection->update($table)
->expression($field, $expression, $arguments)
Alex Pott
committed
->execute();
}
elseif (isset($spec['initial'])) {
$this->connection->update($table)
->fields([$field => $spec['initial']])
->execute();
}
Dries Buytaert
committed
if ($fixnull) {
$spec['not null'] = TRUE;
Dries Buytaert
committed
$this->changeField($table, $field, $field, $spec);
Dries Buytaert
committed
}
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function dropField($table, $field) {
Dries Buytaert
committed
if (!$this->fieldExists($table, $field)) {
Dries Buytaert
committed
return FALSE;
}
// When dropping a field that is part of a composite primary key MySQL
// automatically removes the field from the primary key, which can leave the
// table in an invalid state. MariaDB 10.2.8 requires explicitly dropping
// the primary key first for this reason. We perform this deletion
// explicitly which also makes the behavior on both MySQL and MariaDB
// consistent with PostgreSQL.
// @see https://mariadb.com/kb/en/library/alter-table
$primary_key = $this->findPrimaryKeyColumns($table);
if ((count($primary_key) > 1) && in_array($field, $primary_key, TRUE)) {
$this->dropPrimaryKey($table);
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
Dries Buytaert
committed
return TRUE;
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function fieldSetDefault($table, $field, $default) {
Dries Buytaert
committed
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
Dries Buytaert
committed
}
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $this->escapeDefaultValue($default));
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function fieldSetNoDefault($table, $field) {
Dries Buytaert
committed
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
Dries Buytaert
committed
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT');
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function indexExists($table, $name) {
Dries Buytaert
committed
// Returns one row for each column in the index. Result is string or FALSE.
// Details at http://dev.mysql.com/doc/refman/5.0/en/show-index.html
Alex Pott
committed
$row = $this->connection->query('SHOW INDEX FROM {' . $table . '} WHERE key_name = ' . $this->connection->quote($name))->fetchAssoc();
return isset($row['Key_name']);
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function addPrimaryKey($table, $fields) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table @table: table doesn't exist.", ['@table' => $table]));
Dries Buytaert
committed
}
if ($this->indexExists($table, 'PRIMARY')) {
throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", ['@table' => $table]));
Dries Buytaert
committed
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')');
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function dropPrimaryKey($table) {
Dries Buytaert
committed
if (!$this->indexExists($table, 'PRIMARY')) {
return FALSE;
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
Dries Buytaert
committed
return TRUE;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
protected function findPrimaryKeyColumns($table) {
if (!$this->tableExists($table)) {
return FALSE;
}
$result = $this->connection->query("SHOW KEYS FROM {" . $table . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name');
return array_keys($result);
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function addUniqueKey($table, $name, $fields) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
throw new SchemaObjectDoesNotExistException(t("Cannot add unique key @name to table @table: table doesn't exist.", ['@table' => $table, '@name' => $name]));
Dries Buytaert
committed
}
if ($this->indexExists($table, $name)) {
throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", ['@table' => $table, '@name' => $name]));
Dries Buytaert
committed
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function dropUniqueKey($table, $name) {
Dries Buytaert
committed
if (!$this->indexExists($table, $name)) {
return FALSE;
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
Dries Buytaert
committed
return TRUE;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function addIndex($table, $name, $fields, array $spec) {
Dries Buytaert
committed
if (!$this->tableExists($table)) {
throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", ['@table' => $table, '@name' => $name]));
Dries Buytaert
committed
}
if ($this->indexExists($table, $name)) {
throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", ['@table' => $table, '@name' => $name]));
Dries Buytaert
committed
}
$spec['indexes'][$name] = $fields;
$indexes = $this->getNormalizedIndexes($spec);
$this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($indexes[$name]) . ')');
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function dropIndex($table, $name) {
Dries Buytaert
committed
if (!$this->indexExists($table, $name)) {
return FALSE;
}
Dries Buytaert
committed
$this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
Dries Buytaert
committed
return TRUE;
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
public function changeField($table, $field, $field_new, $spec, $keys_new = []) {
Dries Buytaert
committed
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot change the definition of field @table.@name: field doesn't exist.", ['@table' => $table, '@name' => $field]));
Dries Buytaert
committed
}
Dries Buytaert
committed
if (($field != $field_new) && $this->fieldExists($table, $field_new)) {
throw new SchemaObjectExistsException(t("Cannot rename field @table.@name to @name_new: target field already exists.", ['@table' => $table, '@name' => $field, '@name_new' => $field_new]));
Dries Buytaert
committed
}
if (isset($keys_new['primary key']) && in_array($field_new, $keys_new['primary key'], TRUE)) {
$this->ensureNotNullPrimaryKey($keys_new['primary key'], [$field_new => $spec]);
}
Dries Buytaert
committed
$sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
Dries Buytaert
committed
if ($keys_sql = $this->createKeysSql($keys_new)) {
$sql .= ', ADD ' . implode(', ADD ', $keys_sql);
Dries Buytaert
committed
}
Dries Buytaert
committed
$this->connection->query($sql);
Dries Buytaert
committed
}
Jess
committed
/**
* {@inheritdoc}
*/
public function prepareComment($comment, $length = NULL) {
// Truncate comment to maximum comment length.
if (isset($length)) {
// Add table prefixes before truncating.
Alex Pott
committed
$comment = Unicode::truncate($this->connection->prefixTables($comment), $length, TRUE, TRUE);
Alex Bronstein
committed
// Remove semicolons to avoid triggering multi-statement check.
$comment = strtr($comment, [';' => '.']);
return $this->connection->quote($comment);
}
/**
* Retrieve a table or column comment.
*/
public function getComment($table, $column = NULL) {
$condition = $this->buildTableNameCondition($table);
if (isset($column)) {
$condition->condition('column_name', $column);
$condition->compile($this->connection, $this);
// Don't use {} around information_schema.columns table.
return $this->connection->query("SELECT column_comment as column_comment FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
$condition->compile($this->connection, $this);
// Don't use {} around information_schema.tables table.
$comment = $this->connection->query("SELECT table_comment as table_comment FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
// Work-around for MySQL 5.0 bug http://bugs.mysql.com/bug.php?id=11379
return preg_replace('/; InnoDB free:.*$/', '', $comment);
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function tableExists($table) {
// The information_schema table is very slow to query under MySQL 5.0.
// Instead, we try to select from the table in question. If it fails,
// the most likely reason is that it does not exist. That is dramatically
// faster than using information_schema.
// @link http://bugs.mysql.com/bug.php?id=19588
// @todo This override should be removed once we require a version of MySQL
// that has that bug fixed.
Dries Buytaert
committed
try {
$this->connection->queryRange("SELECT 1 FROM {" . $table . "}", 0, 1);
return TRUE;
}
Dries Buytaert
committed
return FALSE;
}
}
Jess
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function fieldExists($table, $column) {
// The information_schema table is very slow to query under MySQL 5.0.
// Instead, we try to select from the table and field in question. If it
// fails, the most likely reason is that it does not exist. That is
// dramatically faster than using information_schema.
// @link http://bugs.mysql.com/bug.php?id=19588
// @todo This override should be removed once we require a version of MySQL
// that has that bug fixed.
Dries Buytaert
committed
try {
$this->connection->queryRange("SELECT $column FROM {" . $table . "}", 0, 1);
return TRUE;
}
Dries Buytaert
committed
return FALSE;
}
}
Dries Buytaert
committed
}
/**
* @} End of "addtogroup schemaapi".