new way of detecting if table exists.

This commit is contained in:
Qiang Xue
2013-06-04 19:34:51 -04:00
parent b311b6ecf3
commit 9d6f11d5d5

View File

@@ -178,15 +178,21 @@ class Schema extends \yii\db\Schema
* Collects the metadata of table columns.
* @param TableSchema $table the table metadata
* @return boolean whether the table exists in the database
* @throws \Exception if DB query fails
*/
protected function findColumns($table)
{
$rows = $this->db->createCommand("SHOW TABLES LIKE " . $this->quoteValue($table->name))->queryAll();
if (count($rows) === 0) {
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
try {
$columns = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
$previous = $e->getPrevious();
if ($previous instanceof \PDOException && $previous->getCode() == '42S02') {
// table does not exist
return false;
}
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll();
throw $e;
}
foreach ($columns as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;