diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 822011538eb1..72bfe9409c9a 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 90c86547e1bf..7fa2d93b3496 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 3c8637d42e9c..244963f4e82e 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index 87117d170f0c..2f2b3a5dc0e0 100755 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index e9b8043b8f0c..b548cb4474c2 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/MySqlBuilder.php b/src/Illuminate/Database/Schema/MySqlBuilder.php index 7e4c6d2bcd43..fd19c7ed19dd 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/PostgresBuilder.php b/src/Illuminate/Database/Schema/PostgresBuilder.php index 8fa0a2c1909c..8afe4b547002 100755 --- a/src/Illuminate/Database/Schema/PostgresBuilder.php +++ b/src/Illuminate/Database/Schema/PostgresBuilder.php @@ -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. * diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index a6d7315abd11..554b25ff25cc 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -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');