Ruckusing_Adapter_PgSQL_Base::indexes PHP Method

indexes() public method

Return all indexes of a table
public indexes ( string $table_name ) : array
$table_name string the table name
return array
    public function indexes($table_name)
    {
        $sql = <<<SQL
       SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
       FROM pg_class t
       INNER JOIN pg_index d ON t.oid = d.indrelid
       INNER JOIN pg_class i ON d.indexrelid = i.oid
       WHERE i.relkind = 'i'
         AND d.indisprimary = 'f'
         AND t.relname = '%s'
         AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
      ORDER BY i.relname
SQL;
        $sql = sprintf($sql, $table_name);
        $result = $this->select_all($sql);
        $indexes = array();
        foreach ($result as $row) {
            $indexes[] = array('name' => $row['relname'], 'unique' => $row['indisunique'] == 't' ? true : false);
        }
        return $indexes;
    }

Usage Example

 /**
  * test if we can list indexes
  */
 public function test_can_list_indexes()
 {
     $this->adapter->execute_ddl('DROP TABLE IF EXISTS "animals"');
     $this->adapter->execute_ddl("CREATE TABLE animals (id serial primary key, name varchar(32))");
     $this->adapter->execute_ddl("CREATE INDEX idx_animals_on_name ON animals(name)");
     $indexes = $this->adapter->indexes('animals');
     $length = count($indexes);
     $this->assertEquals(1, $length);
     $this->adapter->execute_ddl('DROP TABLE IF EXISTS "animals"');
 }