You are here

protected function IntColumnHandlerPostgreSQL::createTriggerFunction in Dynamic Entity Reference 8.2

Creates the actual table function.

Parameters

string $table: The name of the table.

string $column: The name of the target_id column.

string $column_int: The name of the target_id_int column.

1 call to IntColumnHandlerPostgreSQL::createTriggerFunction()
IntColumnHandlerPostgreSQL::create in src/Storage/IntColumnHandlerPostgreSQL.php
Creates the _int columns and the triggers for them.

File

src/Storage/IntColumnHandlerPostgreSQL.php, line 79

Class

IntColumnHandlerPostgreSQL
PostgreSQL implementation of denormalizing into integer columns.

Namespace

Drupal\dynamic_entity_reference\Storage

Code

protected function createTriggerFunction($table, $column, $column_int) {
  $function_name = $this
    ->getFunctionName($table, $column_int);
  $query = "CREATE OR REPLACE FUNCTION {$function_name}() RETURNS trigger AS \$\$\n      BEGIN\n        NEW.{$column_int} = (CASE WHEN NEW.{$column} ~ '^[0-9]+\$' THEN NEW.{$column} ELSE NULL END)::integer";
  if (strpos($query, ';') !== FALSE) {
    throw new \InvalidArgumentException('; is not supported in SQL strings. Use only one statement at a time.');
  }
  $this->connection
    ->query("{$query}; RETURN NEW; END; \$\$ LANGUAGE plpgsql IMMUTABLE RETURNS NULL ON NULL INPUT", [], [
    'allow_delimiter_in_query' => TRUE,
    'allow_square_brackets' => TRUE,
  ]);
}