You are here

function views_database_connector_get_database_schema_pgsql in Views Database Connector 8

Same name and namespace in other branches
  1. 7 views_database_connector.views.inc \views_database_connector_get_database_schema_pgsql()

Gathers appropriate information from PostgreSQL driver type databases.

1 call to views_database_connector_get_database_schema_pgsql()
add_table_to_database_list in ./views_database_connector.views.inc
Adds table list to the database list based on driver.

File

./views_database_connector.views.inc, line 326
Responsible for hooking views to add each database and its tables.

Code

function views_database_connector_get_database_schema_pgsql($key) {

  // Load the appropriate data type groups.
  $types = views_database_connector_get_data_types();

  // Switch to database in question.
  Database::setActiveConnection($key);

  // The database in question.
  $new_db = Database::getConnection('default', $key);

  // Get a list of the tables in this database.
  $q = 'SELECT table_name FROM ';
  $q .= 'information_schema.tables WHERE (table_type = \'BASE TABLE\' OR table_type = \'VIEW\') AND ';
  $q .= 'table_schema NOT IN (\'pg_catalog\', \'information_schema\');';
  $tables = $new_db
    ->query($q);

  // Switch back to the main database.
  Database::setActiveConnection('default');
  $tablelist = [];

  // Fetch a row, each with a table name.
  while ($row = $tables
    ->fetchAssoc()) {
    foreach ($row as $v) {

      // Switch to database in question.
      Database::setActiveConnection($key);

      // Fetch column names and their data type from said table.
      $q = 'SELECT column_name, data_type FROM ';
      $q .= 'information_schema.columns WHERE table_name = :table;';
      $cols = $new_db
        ->query($q, [
        ':table' => $v,
      ]);

      // Switch back to the main database.
      Database::setActiveConnection('default');
      $collist = [];

      // Fetch a row, each with a column name.
      while ($r = $cols
        ->fetchAssoc()) {
        $t = 'broken';

        // Add column to column list.
        if (isset($r['column_name'])) {
          foreach ($types as $type => $matches) {
            foreach ($matches as $match) {
              if (stristr($r['data_type'], $match)) {
                $t = $type;
              }
            }
          }
          $collist[] = [
            $t,
            $r['column_name'],
          ];
        }
      }

      // Add table and its columns to the table list.
      $tablelist[] = [
        $v,
        $collist,
      ];
    }
  }
  return $tablelist;
}