Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public function hasTable($table)
)) > 0;
}

/**
* Determine if the given view exists.
*
* @param string $view
* @return bool
*/
public function hasView($view)
{
$view = $this->connection->getTablePrefix().$view;

return count($this->connection->select(
$this->grammar->compileViewExists(), [$view]
)) > 0;
}

/**
* Determine if the given table has a given column.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function compileTableExists()
return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
}

/**
* Compile the query to determine if a view exists.
*
* @return string
*/
public function compileViewExists()
{
return $this->compileTableExists();
}

/**
* Compile the query to determine the list of columns.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function compileTableExists()
return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
}

/**
* Compile the query to determine if a view exists.
*
* @return string
*/
public function compileViewExists()
{
return $this->compileTableExists();
}

/**
* Compile the query to determine the list of columns.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function compileTableExists()
return "select * from sqlite_master where type = 'table' and name = ?";
}

/**
* Compile the query to determine if a view exists.
*
* @return string
*/
public function compileViewExists()
{
return "select * from sqlite_master where type = 'view' and name = ?";
}

/**
* Compile the query to determine the list of columns.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public function compileTableExists()
return "select * from sysobjects where type = 'U' and name = ?";
}

/**
* Compile the query to determine if a view exists.
*
* @return string
*/
public function compileViewExists()
{
return "select * from sysobjects where type = 'V' and name = ?";
}

/**
* Compile the query to determine the list of columns.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/MySqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public function hasTable($table)
)) > 0;
}

/**
* Determine if the given view exists.
*
* @param string $view
* @return bool
*/
public function hasView($view)
{
$view = $this->connection->getTablePrefix().$view;

return count($this->connection->select(
$this->grammar->compileViewExists(), [$this->connection->getDatabaseName(), $view]
)) > 0;
}

/**
* Get the column listing for a given table.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ public function hasTable($table)
)) > 0;
}

/**
* Determine if the given view exists.
*
* @param string $view
* @return bool
*/
public function hasView($view)
{
list($schema, $view) = $this->parseSchemaAndTable($view);

$view = $this->connection->getTablePrefix().$view;

return count($this->connection->select(
$this->grammar->compileTableExists(), [$schema, $view]
)) > 0;
}

/**
* Drop all tables from the database.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public function testHasTableCorrectlyCallsGrammar()
$this->assertTrue($builder->hasTable('table'));
}

public function testHasViewCorrectlyCallsGrammar()
{
$connection = m::mock('Illuminate\Database\Connection');
$grammar = m::mock('stdClass');
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
$builder = new Builder($connection);
$grammar->shouldReceive('compileViewExists')->once()->andReturn('sql');
$connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_');
$connection->shouldReceive('select')->once()->with('sql', ['prefix_view'])->andReturn(['prefix_view']);

$this->assertTrue($builder->hasView('view'));
}

public function testTableHasColumns()
{
$connection = m::mock('Illuminate\Database\Connection');
Expand Down