Commit 9b723baa by Qiang Xue

Added `TableSchema::fullName` property

parent e13557d3
...@@ -78,6 +78,7 @@ Yii Framework 2 Change Log ...@@ -78,6 +78,7 @@ Yii Framework 2 Change Log
- Enh: Added support for installing packages conforming to PSR-4 standard (qiangxue) - Enh: Added support for installing packages conforming to PSR-4 standard (qiangxue)
- Enh: Better exception message when class cannot be loaded (samdark) - Enh: Better exception message when class cannot be loaded (samdark)
- Enh: `init` of advanced application now allows to specify answer for overwriting files via `init --overwrite=n` (samdark) - Enh: `init` of advanced application now allows to specify answer for overwriting files via `init --overwrite=n` (samdark)
- Enh: Added `TableSchema::fullName` property (qiangxue)
- Enh #1839: Added support for getting file extension and basename from uploaded file (anfrantic) - Enh #1839: Added support for getting file extension and basename from uploaded file (anfrantic)
- Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue)
- Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
......
...@@ -423,9 +423,9 @@ class Connection extends Component ...@@ -423,9 +423,9 @@ class Connection extends Component
} else { } else {
$driver = $this->getDriverName(); $driver = $this->getDriverName();
if (isset($this->schemaMap[$driver])) { if (isset($this->schemaMap[$driver])) {
$this->_schema = Yii::createObject($this->schemaMap[$driver]); $config = !is_array($this->schemaMap[$driver]) ? ['class' => $this->schemaMap[$driver]] : $this->schemaMap[$driver];
$this->_schema->db = $this; $config['db'] = $this;
return $this->_schema; return $this->_schema = Yii::createObject($config);
} else { } else {
throw new NotSupportedException("Connection does not support reading schema information for '$driver' DBMS."); throw new NotSupportedException("Connection does not support reading schema information for '$driver' DBMS.");
} }
......
...@@ -56,6 +56,10 @@ abstract class Schema extends Object ...@@ -56,6 +56,10 @@ abstract class Schema extends Object
*/ */
public $db; public $db;
/** /**
* @var string the default schema name used for the current session.
*/
public $defaultSchema;
/**
* @var array list of ALL table names in the database * @var array list of ALL table names in the database
*/ */
private $_tableNames = []; private $_tableNames = [];
......
...@@ -21,14 +21,20 @@ use yii\base\InvalidParamException; ...@@ -21,14 +21,20 @@ use yii\base\InvalidParamException;
class TableSchema extends Object class TableSchema extends Object
{ {
/** /**
* @var string name of the schema that this table belongs to. * @var string the name of the schema that this table belongs to.
*/ */
public $schemaName; public $schemaName;
/** /**
* @var string name of this table. * @var string the name of this table. The schema name is not included. Use [[fullName]] to get the name with schema name prefix.
*/ */
public $name; public $name;
/** /**
* @var string the full name of this table, which includes the schema name prefix, if any.
* Note that if the schema name is the same as the [[Schema::defaultSchema|default schema name]],
* the schema name will not be included.
*/
public $fullName;
/**
* @var string[] primary keys of this table. * @var string[] primary keys of this table.
*/ */
public $primaryKey = []; public $primaryKey = [];
...@@ -53,6 +59,7 @@ class TableSchema extends Object ...@@ -53,6 +59,7 @@ class TableSchema extends Object
*/ */
public $columns = []; public $columns = [];
/** /**
* Gets the named column metadata. * Gets the named column metadata.
* This is a convenient method for retrieving a named column even if it does not exist. * This is a convenient method for retrieving a named column even if it does not exist.
......
...@@ -128,9 +128,12 @@ class Schema extends \yii\db\Schema ...@@ -128,9 +128,12 @@ class Schema extends \yii\db\Schema
$this->db->open(); $this->db->open();
$tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name); $tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
if (isset($tableInfo[0]['NAME'])) { if (!isset($tableInfo[0]['NAME'])) {
return null;
}
$table = new TableSchema(); $table = new TableSchema();
$table->name = $tableInfo[0]['NAME']; $table->fullName = $table->name = $tableInfo[0]['NAME'];
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name); $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll(); $columns = $this->db->createCommand($sql)->queryAll();
...@@ -164,9 +167,6 @@ class Schema extends \yii\db\Schema ...@@ -164,9 +167,6 @@ class Schema extends \yii\db\Schema
$table->foreignKeys = array_values($table->foreignKeys); $table->foreignKeys = array_values($table->foreignKeys);
return $table; return $table;
} else {
return null;
}
} }
/** /**
......
...@@ -18,15 +18,14 @@ use yii\db\ColumnSchema; ...@@ -18,15 +18,14 @@ use yii\db\ColumnSchema;
class Schema extends \yii\db\Schema class Schema extends \yii\db\Schema
{ {
/** /**
* Default schema name to be used. * @var string the default schema used for the current session.
*/ */
const DEFAULT_SCHEMA = 'dbo'; public $defaultSchema = 'dbo';
/** /**
* @var array mapping from physical column types (keys) to abstract column types (values) * @var array mapping from physical column types (keys) to abstract column types (values)
*/ */
public $typeMap = [ public $typeMap = [
// exact numerics // exact numbers
'bigint' => self::TYPE_BIGINT, 'bigint' => self::TYPE_BIGINT,
'numeric' => self::TYPE_DECIMAL, 'numeric' => self::TYPE_DECIMAL,
'bit' => self::TYPE_SMALLINT, 'bit' => self::TYPE_SMALLINT,
...@@ -37,7 +36,7 @@ class Schema extends \yii\db\Schema ...@@ -37,7 +36,7 @@ class Schema extends \yii\db\Schema
'tinyint' => self::TYPE_SMALLINT, 'tinyint' => self::TYPE_SMALLINT,
'money' => self::TYPE_MONEY, 'money' => self::TYPE_MONEY,
// approximate numerics // approximate numbers
'float' => self::TYPE_FLOAT, 'float' => self::TYPE_FLOAT,
'real' => self::TYPE_FLOAT, 'real' => self::TYPE_FLOAT,
...@@ -137,14 +136,16 @@ class Schema extends \yii\db\Schema ...@@ -137,14 +136,16 @@ class Schema extends \yii\db\Schema
$table->catalogName = $parts[0]; $table->catalogName = $parts[0];
$table->schemaName = $parts[1]; $table->schemaName = $parts[1];
$table->name = $parts[2]; $table->name = $parts[2];
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
} elseif ($partCount == 2) { } elseif ($partCount == 2) {
// only schema name and table name passed // only schema name and table name passed
$table->schemaName = $parts[0]; $table->schemaName = $parts[0];
$table->name = $parts[1]; $table->name = $parts[1];
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
} else { } else {
// only schema name passed // only table name passed
$table->schemaName = static::DEFAULT_SCHEMA; $table->schemaName = $this->defaultSchema;
$table->name = $parts[0]; $table->fullName = $table->name = $parts[0];
} }
} }
...@@ -339,7 +340,7 @@ SQL; ...@@ -339,7 +340,7 @@ SQL;
protected function findTableNames($schema = '') protected function findTableNames($schema = '')
{ {
if ($schema === '') { if ($schema === '') {
$schema = static::DEFAULT_SCHEMA; $schema = $this->defaultSchema;
} }
$sql = <<<SQL $sql = <<<SQL
......
...@@ -109,8 +109,9 @@ class Schema extends \yii\db\Schema ...@@ -109,8 +109,9 @@ class Schema extends \yii\db\Schema
if (isset($parts[1])) { if (isset($parts[1])) {
$table->schemaName = $parts[0]; $table->schemaName = $parts[0];
$table->name = $parts[1]; $table->name = $parts[1];
$table->fullName = $table->schemaName . '.' . $table->name;
} else { } else {
$table->name = $parts[0]; $table->fullName = $table->name = $parts[0];
} }
} }
......
...@@ -15,14 +15,21 @@ use yii\db\ColumnSchema; ...@@ -15,14 +15,21 @@ use yii\db\ColumnSchema;
* *
* @todo mapping from physical types to abstract types * @todo mapping from physical types to abstract types
* *
* @property string $defaultSchema Default schema.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Schema extends \yii\db\Schema class Schema extends \yii\db\Schema
{ {
private $_defaultSchema; /**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->defaultSchema === null) {
$this->defaultSchema = $this->db->username;
}
}
/** /**
* @inheritdoc * @inheritdoc
...@@ -77,28 +84,11 @@ class Schema extends \yii\db\Schema ...@@ -77,28 +84,11 @@ class Schema extends \yii\db\Schema
$table->schemaName = $parts[0]; $table->schemaName = $parts[0];
$table->name = $parts[1]; $table->name = $parts[1];
} else { } else {
$table->schemaName = $this->getDefaultSchema(); $table->schemaName = $this->defaultSchema;
$table->name = $parts[0]; $table->name = $name;
}
} }
/** $table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
* @return string default schema.
*/
public function getDefaultSchema()
{
if ($this->_defaultSchema === null) {
$this->setDefaultSchema(strtoupper($this->db->username));
}
return $this->_defaultSchema;
}
/**
* @param string $schema default schema.
*/
public function setDefaultSchema($schema)
{
$this->_defaultSchema = $schema;
} }
/** /**
......
...@@ -20,13 +20,10 @@ use yii\db\ColumnSchema; ...@@ -20,13 +20,10 @@ use yii\db\ColumnSchema;
*/ */
class Schema extends \yii\db\Schema class Schema extends \yii\db\Schema
{ {
/** /**
* The default schema used for the current session. * @var string the default schema used for the current session.
* @var string
*/ */
public $defaultSchema = 'public'; public $defaultSchema = 'public';
/** /**
* @var array mapping from physical column types (keys) to abstract * @var array mapping from physical column types (keys) to abstract
* column types (values) * column types (values)
...@@ -92,15 +89,16 @@ class Schema extends \yii\db\Schema ...@@ -92,15 +89,16 @@ class Schema extends \yii\db\Schema
protected function resolveTableNames($table, $name) protected function resolveTableNames($table, $name)
{ {
$parts = explode('.', str_replace('"', '', $name)); $parts = explode('.', str_replace('"', '', $name));
if (isset($parts[1])) { if (isset($parts[1])) {
$table->schemaName = $parts[0]; $table->schemaName = $parts[0];
$table->name = $parts[1]; $table->name = $parts[1];
} else { } else {
$table->name = $parts[0];
}
if ($table->schemaName === null) {
$table->schemaName = $this->defaultSchema; $table->schemaName = $this->defaultSchema;
$table->name = $name;
} }
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
} }
/** /**
......
...@@ -84,8 +84,7 @@ class Schema extends \yii\db\Schema ...@@ -84,8 +84,7 @@ class Schema extends \yii\db\Schema
/** /**
* Returns all table names in the database. * Returns all table names in the database.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
* If not empty, the returned table names will be prefixed with the schema name. * @return array all table names in the database. The names have NO schema name prefix.
* @return array all table names in the database.
*/ */
protected function findTableNames($schema = '') protected function findTableNames($schema = '')
{ {
...@@ -102,6 +101,7 @@ class Schema extends \yii\db\Schema ...@@ -102,6 +101,7 @@ class Schema extends \yii\db\Schema
{ {
$table = new TableSchema; $table = new TableSchema;
$table->name = $name; $table->name = $name;
$table->fullName = $name;
if ($this->findColumns($table)) { if ($this->findColumns($table)) {
$this->findConstraints($table); $this->findConstraints($table);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment