public function Schema::queryTableInformation

public Schema::queryTableInformation($table)

Fetch the list of blobs and sequences used on a table.

We introspect the database to collect the information required by insert and update queries.

Parameters

$table_name: The non-prefixed name of the table.

Return value

An object with two member variables:

  • 'blob_fields' that lists all the blob fields in the table.
  • 'sequences' that lists the sequences used in that table.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 89

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function queryTableInformation($table) {
  // Generate a key to reference this table's information on.
  $key = $this->connection->prefixTables('{' . $table . '}');

  // Take into account that temporary tables are stored in a different schema.
  // \Drupal\Core\Database\Connection::generateTemporaryTableName() sets the
  // 'db_temporary_' prefix to all temporary tables.
  if (strpos($key, '.') === FALSE && strpos($table, 'db_temporary_') === FALSE) {
    $key = 'public.' . $key;
  }
  else {
    $schema = $this->connection->query('SELECT nspname FROM pg_namespace WHERE oid = pg_my_temp_schema()')->fetchField();
    $key = $schema . '.' . $key;
  }

  if (!isset($this->tableInformation[$key])) {
    // Split the key into schema and table for querying.
    list($schema, $table_name) = explode('.', $key);
    $table_information = (object) array(
      'blob_fields' => array(),
      'sequences' => array(),
    );
    // Don't use {} around information_schema.columns table.
    $this->connection->addSavepoint();

    try {
      // Check if the table information exists in the PostgreSQL metadata.
      $table_information_exists = (bool) $this->connection->query("SELECT 1 FROM pg_class WHERE relname = :table", array(':table' => $table_name))->fetchField();

      // If the table information does not yet exist in the PostgreSQL
      // metadata, then return the default table information here, so that it
      // will not be cached.
      if (!$table_information_exists) {
        $this->connection->releaseSavepoint();
        return $table_information;
      }
      else {
        $result = $this->connection->query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = :schema AND table_name = :table AND (data_type = 'bytea' OR (numeric_precision IS NOT NULL AND column_default LIKE :default))", array(
          ':schema' => $schema,
          ':table' => $table_name,
          ':default' => '%nextval%',
        ));
      }
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      throw $e;
    }
    $this->connection->releaseSavepoint();

    foreach ($result as $column) {
      if ($column->data_type == 'bytea') {
        $table_information->blob_fields[$column->column_name] = TRUE;
      }
      elseif (preg_match("/nextval\('([^']+)'/", $column->column_default, $matches)) {
        // We must know of any sequences in the table structure to help us
        // return the last insert id. If there is more than 1 sequences the
        // first one (index 0 of the sequences array) will be used.
        $table_information->sequences[] = $matches[1];
        $table_information->serial_fields[] = $column->column_name;
      }
    }
    $this->tableInformation[$key] = $table_information;
  }
  return $this->tableInformation[$key];
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Schema.php/function/Schema::queryTableInformation/8.1.x