You are here

public function IntColumnHandlerPostgreSQL::create in Dynamic Entity Reference 8.2

Creates the _int columns and the triggers for them.

Parameters

string $table: The non-prefix table to operate on.

array $columns: The DER target_id columns.

array $index_columns: Table columns that should be added to the index that is created for the new _int column.

Return value

array The list of new target_id_int columns.

Overrides IntColumnHandlerInterface::create

File

src/Storage/IntColumnHandlerPostgreSQL.php, line 32

Class

IntColumnHandlerPostgreSQL
PostgreSQL implementation of denormalizing into integer columns.

Namespace

Drupal\dynamic_entity_reference\Storage

Code

public function create($table, array $columns, array $index_columns = []) {
  $schema = $this->connection
    ->schema();
  if (!IntColumnHandler::allColumnsExist($schema, $table, $columns)) {
    return [];
  }

  // The integer column specification.
  $spec = [
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => FALSE,
  ];
  $new = [];
  foreach ($columns as $column) {
    $column_int = $column . '_int';

    // Make sure the integer columns exist.
    if (!$schema
      ->fieldExists($table, $column_int)) {
      $this
        ->createTriggerFunction($table, $column, $column_int);
      $this
        ->createTrigger($table, $column, $column_int);
      $index_fields = [
        $column_int,
      ];
      $full_spec = [
        'fields' => [
          $column_int => $spec,
        ],
      ];
      if (!empty($index_columns)) {
        $full_spec['fields'] = array_merge($full_spec['fields'], $index_columns);
        $index_fields = array_merge($index_fields, array_keys($index_columns));
      }
      $schema
        ->addField($table, $column_int, $spec);
      $schema
        ->addIndex($table, $column_int, $index_fields, $full_spec);
      $new[] = $column_int;
    }
  }
  return $new;
}