Skip to content

Commit

Permalink
Fix view listing.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Feb 12, 2016
1 parent e131074 commit 79afa41
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/DB/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function get_wpdb() {
}

/**
* Get a list of tables that the current user can read.
* Get a list of tables and views that the current user can read.
* @return string[] The table names.
*/
public function get_table_names() {
if ( ! $this->table_names ) {
$this->table_names = array();
foreach ( $this->wpdb->get_col( 'SHOW TABLES' ) as $table_name ) {
if ( Grants::current_user_can( Grants::READ, $table_name ) ) {
$this->table_names[] = $table_name;
$this->table_names[ $table_name ] = $table_name;
}
}
}
Expand Down Expand Up @@ -78,10 +78,10 @@ public function get_tables( $exclude_views = true ) {
if ( ! $table ) {
continue;
}
if ( $exclude_views && $table->get_type() === Table::TYPE_VIEW ) {
if ( $exclude_views && $table->is_view() ) {
continue;
}
$out[] = $table;
$out[ $table->get_name() ] = $table;
}
return $out;
}
Expand All @@ -94,8 +94,8 @@ public function get_tables( $exclude_views = true ) {
public function get_views() {
$out = array();
foreach ( $this->get_tables( false ) as $table ) {
if ( $table->get_type() === Table::TYPE_VIEW ) {
$out[] = $table;
if ( $table->is_view() ) {
$out[ $table->get_name() ] = $table;
}
}
return $out;
Expand Down
7 changes: 5 additions & 2 deletions src/DB/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ public function get_name() {
* @return string Either `Table::TYPE_TABLE` or `Table::TYPE_VIEW`.
*/
public function get_type() {
if ( ! $this->type ) {
$this->get_defining_sql();
}
return $this->type;
}

Expand All @@ -474,15 +477,15 @@ public function get_type() {
* @return boolean
*/
public function is_table() {
return $this->get_type() == self::TYPE_TABLE;
return $this->get_type() === self::TYPE_TABLE;
}

/**
* Whether this table is a view.
* @return boolean
*/
public function is_view() {
return $this->get_type() == self::TYPE_VIEW;
return $this->get_type() === self::TYPE_VIEW;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tabulate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* License: GPL-2.0+
* Text Domain: tabulate
* Domain Path: /languages
* Version: 2.7.0
* Version: 2.7.1
*/
define( 'TABULATE_VERSION', '2.7.0' );
define( 'TABULATE_VERSION', '2.7.1' );
define( 'TABULATE_SLUG', 'tabulate' );

// Load textdomain.
Expand Down
20 changes: 19 additions & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function github_21() {
* @testdox It should be possible to provide a value for a (non-autoincrementing) PK.
* @test
*/
public function timestamp_pk() {
public function provided_pk() {
$this->wpdb->query( 'DROP TABLE IF EXISTS `provided_pk`' );
$sql = "CREATE TABLE `provided_pk` ( "
. " `code` VARCHAR(10) NOT NULL PRIMARY KEY, "
Expand All @@ -339,4 +339,22 @@ public function timestamp_pk() {
$rec = $tbl->save_record( array( 'code' => 'TEST') );
$this->assertEquals( 'TEST', $rec->get_primary_key() );
}

/**
* @testdox It is possible to list base-tables and views separately.
* @test
*/
public function views() {
$sql = "CREATE OR REPLACE VIEW test_types_view AS "
. " SELECT * FROM `test_types`;";
$this->db->query( $sql );
$this->db->reset();
$view = $this->db->get_table( 'test_types_view' );
$this->assertInstanceOf( 'WordPress\\Tabulate\\DB\\Table', $view );
$this->assertTrue( $view->is_view() );
$this->assertArrayHasKey( 'test_types_view', $this->db->get_table_names() );
$this->assertArrayHasKey( 'test_types_view', $this->db->get_views() );
$this->assertArrayHasKey( 'test_types_view', $this->db->get_tables( false ) );
$this->assertArrayNotHasKey( 'test_types_view', $this->db->get_tables( true ) );
}
}

0 comments on commit 79afa41

Please sign in to comment.