You are here

function DB_pgsql::_pgFieldFlags in Flickr API 5

Get a column's flags

Supports "not_null", "default_value", "primary_key", "unique_key" and "multiple_key". The default value is passed through rawurlencode() in case there are spaces in it.

@access private

Parameters

int $resource the PostgreSQL result identifier:

int $num_field the field number:

Return value

string the flags

1 call to DB_pgsql::_pgFieldFlags()
DB_pgsql::tableInfo in phpFlickr/PEAR/DB/pgsql.php
Returns information about a table or a result set

File

phpFlickr/PEAR/DB/pgsql.php, line 974

Class

DB_pgsql
The methods PEAR DB uses to interact with PHP's pgsql extension for interacting with PostgreSQL databases

Code

function _pgFieldFlags($resource, $num_field, $table_name) {
  $field_name = @pg_fieldname($resource, $num_field);
  $result = @pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef\n                                FROM pg_attribute f, pg_class tab, pg_type typ\n                                WHERE tab.relname = typ.typname\n                                AND typ.typrelid = f.attrelid\n                                AND f.attname = '{$field_name}'\n                                AND tab.relname = '{$table_name}'");
  if (@pg_numrows($result) > 0) {
    $row = @pg_fetch_row($result, 0);
    $flags = $row[0] == 't' ? 'not_null ' : '';
    if ($row[1] == 't') {
      $result = @pg_exec($this->connection, "SELECT a.adsrc\n                                    FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a\n                                    WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid\n                                    AND f.attrelid = a.adrelid AND f.attname = '{$field_name}'\n                                    AND tab.relname = '{$table_name}' AND f.attnum = a.adnum");
      $row = @pg_fetch_row($result, 0);
      $num = preg_replace("/'(.*)'::\\w+/", "\\1", $row[0]);
      $flags .= 'default_' . rawurlencode($num) . ' ';
    }
  }
  else {
    $flags = '';
  }
  $result = @pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey\n                                FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i\n                                WHERE tab.relname = typ.typname\n                                AND typ.typrelid = f.attrelid\n                                AND f.attrelid = i.indrelid\n                                AND f.attname = '{$field_name}'\n                                AND tab.relname = '{$table_name}'");
  $count = @pg_numrows($result);
  for ($i = 0; $i < $count; $i++) {
    $row = @pg_fetch_row($result, $i);
    $keys = explode(' ', $row[2]);
    if (in_array($num_field + 1, $keys)) {
      $flags .= $row[0] == 't' && $row[1] == 'f' ? 'unique_key ' : '';
      $flags .= $row[1] == 't' ? 'primary_key ' : '';
      if (count($keys) > 1) {
        $flags .= 'multiple_key ';
      }
    }
  }
  return trim($flags);
}