You are here

public function DatabaseSchema_pgsql::queryTableInformation in Drupal 7

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

includes/database/pgsql/schema.inc, line 39
Database schema code for PostgreSQL database servers.

Class

DatabaseSchema_pgsql

Code

public function queryTableInformation($table) {

  // Generate a key to reference this table's information on.
  $key = $this->connection
    ->prefixTables('{' . $table . '}');
  if (!strpos($key, '.')) {
    $key = 'public.' . $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.
    $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%',
    ));
    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];
}