View source
<?php
namespace Drupal\data;
class Table {
public $name, $title, $table_schema, $meta, $export_type;
public function __construct($name) {
$this->name = \Drupal::database()
->escapeTable($name);
if ($table = _data_load_table($name)) {
foreach (array(
'title',
'name',
'table_schema',
'meta',
'export_type',
) as $key) {
if (isset($table->{$key})) {
$this->{$key} = $table->{$key};
}
}
}
}
public function create($table_schema) {
if (!_data_load_table($this->name, TRUE) && !\Drupal::database()
->schema()
->tableExists($this->name)) {
try {
\Drupal::database()
->createTable($this->name, $table_schema);
} catch (DatabaseSchemaObjectExistsException $e) {
\Drupal::messenger()
->addError(t('Error creating table.'), 'error');
return FALSE;
}
if (module_exists('schema')) {
$schema = schema_dbobject()
->inspect();
if (isset($schema[$this->name])) {
$table_schema = $schema[$this->name];
}
}
$this->table_schema = $table_schema;
$this->export_type = EXPORT_IN_DATABASE;
$table = array(
'name' => $this->name,
'table_schema' => $this->table_schema,
);
drupal_write_record('data_tables', $table);
drupal_get_schema($this->name, TRUE);
if (module_exists('views')) {
views_invalidate_cache();
}
if (module_exists('data_ui')) {
menu_rebuild();
}
return TRUE;
}
return FALSE;
}
public function adopt() {
if ($this
->defined() || !module_exists('schema')) {
return FALSE;
}
$schema = schema_dbobject()
->inspect(variable_get('schema_database_connection', 'default'), $this->name);
if (isset($schema[$this->name])) {
$table = array(
'name' => $this->name,
'title' => data_natural_name($this->name),
'table_schema' => $schema[$this->name],
'meta' => array(
'fields' => array_fill_keys(array_keys($schema[$this->name]['fields']), array()),
),
);
if (drupal_write_record('data_tables', $table)) {
return TRUE;
}
}
drupal_get_schema($this->name, TRUE);
if (module_exists('views')) {
views_invalidate_cache();
}
if (module_exists('data_ui')) {
menu_rebuild();
}
return FALSE;
}
public function disown() {
if (!module_exists('schema')) {
return FALSE;
}
$num_deleted = \Drupal::database()
->delete('data_tables')
->condition('name', $this->name)
->execute();
return $num_deleted == 1;
}
public function defined() {
return _data_load_table($this->name) ? TRUE : FALSE;
}
public function get($property) {
if (in_array($property, array(
'name',
'title',
'table_schema',
'meta',
'export_type',
))) {
return $this->{$property};
}
}
public function update($properties) {
$properties['name'] = $this->name;
if (drupal_write_record('data_tables', $properties, 'name')) {
foreach ($properties as $key => $value) {
$this->{$key} = $value;
}
}
}
public function compareSchema() {
if (module_exists('schema')) {
$this->table_schema['name'] = $this->name;
return schema_compare_table($this->table_schema);
}
}
public function addField($field, $spec) {
try {
\Drupal::database()
->schema()
->addField($this->name, $field, $spec);
$schema = $this->table_schema;
$schema['fields'][$field] = $spec;
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
if (function_exists('views_invalidate_cache')) {
views_invalidate_cache();
}
return $field;
} catch (DatabaseSchemaObjectExistsException $e) {
throw new DataException(t('Error adding field.'));
}
throw new DataException(t('Error adding field.'));
}
public function addIndex($field) {
$schema = $this->table_schema;
if ($schema['fields'][$field]) {
$index = data_get_index_definition($field, $schema['fields'][$field]);
try {
\Drupal::database()
->schema()
->addIndex($this->name, $field, $index);
} catch (DatabaseSchemaObjectExistsException $e) {
throw new DataException(t('Error adding index.'));
}
$schema['indexes'][$field] = $index;
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
}
}
public function dropIndex($field) {
$success = \Drupal::database()
->schema()
->dropIndex($this->name, $field);
if ($success) {
$schema = $this->table_schema;
unset($schema['indexes'][$field]);
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
return;
}
throw new DataException(t('Error dropping index.'));
}
public function addUniqueKey($field) {
$schema = $this->table_schema;
if ($schema['fields'][$field]) {
$index = data_get_index_definition($field, $schema['fields'][$field]);
try {
$success = \Drupal::database()
->schema()
->addUniqueKey($this->name, $field, $index);
} catch (DatabaseSchemaObjectExistsException $e) {
throw new DataException(t('Error adding unique key.'));
}
if ($success) {
$schema['unique keys'][$field] = array(
$field,
);
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
return;
}
}
}
public function dropUniqueKey($field) {
$success = \Drupal::database()
->schema()
->addUniqueKey($this->name, $field);
if ($success) {
$schema = $this->table_schema;
unset($schema['unique keys'][$field]);
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
return;
}
throw new DataException(t('Error dropping unique key.'));
}
public function changeIndex($fields) {
$schema = $this->table_schema;
$indexes = isset($schema['indexes']) ? array_keys($schema['indexes']) : array();
$add = array_diff($fields, $indexes);
$drop = array_diff($indexes, $fields);
foreach ($add as $field) {
$this
->addIndex($field);
}
foreach ($drop as $field) {
$this
->dropIndex($field);
}
}
public function addPrimaryKey($fields) {
$schema = $this->table_schema;
foreach ($fields as $field) {
if ($schema['fields'][$field]['type'] == 'text') {
throw new DataException(t('A text field cannot be made a primary key.'));
}
}
try {
\Drupal::database()
->schema()
->addPrimaryKey($this->name, $fields);
} catch (DatabaseSchemaObjectExistsException $e) {
throw new DataException(t('Error creating primary key.'));
}
$schema['primary key'] = $fields;
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
}
public function dropPrimaryKey() {
\Drupal::database()
->schema()
->dropPrimaryKey($this->name);
$schema = $this->table_schema;
$schema['primary key'] = array();
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
return;
}
public function changePrimaryKey($fields) {
$schema = $this->table_schema;
if (!empty($schema['primary key'])) {
$this
->dropPrimaryKey();
}
if (!empty($fields)) {
$this
->addPrimaryKey($fields);
}
}
public function changeField($field, $spec) {
if ($spec['type'] == 'text') {
if (in_array($field, $this->table_schema['primary key'])) {
throw new DataException(t('Cannot make a primary key field a text field.'));
}
foreach ($this->table_schema['indexes'] as $index_name => $index) {
foreach ($index as $index_field) {
if (is_array($index_field)) {
$index_field = array_shift($index_field);
}
if ($field == $index_field) {
$this
->dropIndex($index_field);
}
}
}
}
try {
\Drupal::database()
->schema()
->changeField($this->name, $field, $field, $spec);
} catch (DatabaseSchemaObjectDoesNotExistException $e) {
throw new DataException(t('Cannot change field.'));
}
$schema = $this->table_schema;
$schema['fields'][$field] = $spec;
$this
->update(array(
'table_schema' => $schema,
));
drupal_get_schema($this->name, TRUE);
}
public function dropField($field) {
$success = \Drupal::database()
->schema()
->dropField($this->name, $field);
if ($success) {
$schema = $this->table_schema;
unset($schema['fields'][$field]);
$meta = $this->meta;
unset($meta['fields'][$field]);
$this
->update(array(
'table_schema' => $schema,
), array(
'meta' => $meta,
));
drupal_get_schema($this->name, TRUE);
return;
}
throw new DataException(t('Cannot drop field.'));
}
public function drop() {
if ($this->export_type == EXPORT_IN_DATABASE) {
if (\Drupal::database()
->schema()
->tableExists($this->name)) {
\Drupal::database()
->schema()
->dropTable($this->name);
}
$this
->update(array(
'table_schema' => array(),
));
drupal_get_schema($this->name, TRUE);
\Drupal::database()
->delete('data_tables')
->condition('name', $this->name)
->execute();
$this->title = '';
$this->table_schema = $this->meta = array();
return TRUE;
}
return FALSE;
}
public function revert() {
if ($this->export_type & EXPORT_IN_CODE) {
\Drupal::database()
->delete('data_tables')
->condition('name', $this->name)
->execute();
return TRUE;
}
return FALSE;
}
public function link($left_table, $left_field, $field = NULL, $inner_join = TRUE) {
if ($field == NULL) {
$field = $left_table;
}
$this->meta['join'][$left_table] = array(
'left_field' => $left_field,
'field' => $field,
'inner_join' => $inner_join,
);
$this
->update(array(
'meta' => $this->meta,
));
}
public function unlink($left_table) {
unset($this->meta['join'][$left_table]);
$this
->update(array(
'meta' => $this->meta,
));
}
public function handler() {
return data_get_handler($this->name);
}
public static function clearCaches() {
drupal_get_schema(NULL, TRUE);
if (module_exists('views')) {
views_invalidate_cache();
}
if (module_exists('data_ui')) {
menu_rebuild();
}
}
}