Commit 9b723baa by Qiang Xue

Added `TableSchema::fullName` property

parent e13557d3
......@@ -78,6 +78,7 @@ Yii Framework 2 Change Log
- Enh: Added support for installing packages conforming to PSR-4 standard (qiangxue)
- 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: Added `TableSchema::fullName` property (qiangxue)
- 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 #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
......
......@@ -423,9 +423,9 @@ class Connection extends Component
} else {
$driver = $this->getDriverName();
if (isset($this->schemaMap[$driver])) {
$this->_schema = Yii::createObject($this->schemaMap[$driver]);
$this->_schema->db = $this;
return $this->_schema;
$config = !is_array($this->schemaMap[$driver]) ? ['class' => $this->schemaMap[$driver]] : $this->schemaMap[$driver];
$config['db'] = $this;
return $this->_schema = Yii::createObject($config);
} else {
throw new NotSupportedException("Connection does not support reading schema information for '$driver' DBMS.");
}
......
......@@ -56,6 +56,10 @@ abstract class Schema extends Object
*/
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
*/
private $_tableNames = [];
......
......@@ -21,14 +21,20 @@ use yii\base\InvalidParamException;
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;
/**
* @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;
/**
* @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.
*/
public $primaryKey = [];
......@@ -53,6 +59,7 @@ class TableSchema extends Object
*/
public $columns = [];
/**
* Gets the named column metadata.
* This is a convenient method for retrieving a named column even if it does not exist.
......
......@@ -128,45 +128,45 @@ class Schema extends \yii\db\Schema
$this->db->open();
$tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
if (isset($tableInfo[0]['NAME'])) {
$table = new TableSchema();
$table->name = $tableInfo[0]['NAME'];
if (!isset($tableInfo[0]['NAME'])) {
return null;
}
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll();
$table = new TableSchema();
$table->fullName = $table->name = $tableInfo[0]['NAME'];
foreach ($columns as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;
}
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll();
$primaryKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name);
foreach ($primaryKeys as $key) {
$column = $table->columns[$key['ATTR_NAME']];
$column->isPrimaryKey = true;
$table->primaryKey[] = $column->name;
if ($column->autoIncrement) {
$table->sequenceName = '';
}
}
foreach ($columns as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;
}
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
foreach ($foreignKeys as $key) {
if (isset($table->foreignKeys[$key['FK_NAME']])) {
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
} else {
$table->foreignKeys[$key['FK_NAME']] = [
$key['PKTABLE_NAME'],
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME']
];
}
$primaryKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name);
foreach ($primaryKeys as $key) {
$column = $table->columns[$key['ATTR_NAME']];
$column->isPrimaryKey = true;
$table->primaryKey[] = $column->name;
if ($column->autoIncrement) {
$table->sequenceName = '';
}
$table->foreignKeys = array_values($table->foreignKeys);
}
return $table;
} else {
return null;
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
foreach ($foreignKeys as $key) {
if (isset($table->foreignKeys[$key['FK_NAME']])) {
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
} else {
$table->foreignKeys[$key['FK_NAME']] = [
$key['PKTABLE_NAME'],
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME']
];
}
}
$table->foreignKeys = array_values($table->foreignKeys);
return $table;
}
/**
......
......@@ -18,15 +18,14 @@ use yii\db\ColumnSchema;
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)
*/
public $typeMap = [
// exact numerics
// exact numbers
'bigint' => self::TYPE_BIGINT,
'numeric' => self::TYPE_DECIMAL,
'bit' => self::TYPE_SMALLINT,
......@@ -37,7 +36,7 @@ class Schema extends \yii\db\Schema
'tinyint' => self::TYPE_SMALLINT,
'money' => self::TYPE_MONEY,
// approximate numerics
// approximate numbers
'float' => self::TYPE_FLOAT,
'real' => self::TYPE_FLOAT,
......@@ -137,14 +136,16 @@ class Schema extends \yii\db\Schema
$table->catalogName = $parts[0];
$table->schemaName = $parts[1];
$table->name = $parts[2];
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
} elseif ($partCount == 2) {
// only schema name and table name passed
$table->schemaName = $parts[0];
$table->name = $parts[1];
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
} else {
// only schema name passed
$table->schemaName = static::DEFAULT_SCHEMA;
$table->name = $parts[0];
// only table name passed
$table->schemaName = $this->defaultSchema;
$table->fullName = $table->name = $parts[0];
}
}
......@@ -339,7 +340,7 @@ SQL;
protected function findTableNames($schema = '')
{
if ($schema === '') {
$schema = static::DEFAULT_SCHEMA;
$schema = $this->defaultSchema;
}
$sql = <<<SQL
......
......@@ -109,8 +109,9 @@ class Schema extends \yii\db\Schema
if (isset($parts[1])) {
$table->schemaName = $parts[0];
$table->name = $parts[1];
$table->fullName = $table->schemaName . '.' . $table->name;
} else {
$table->name = $parts[0];
$table->fullName = $table->name = $parts[0];
}
}
......
......@@ -15,14 +15,21 @@ use yii\db\ColumnSchema;
*
* @todo mapping from physical types to abstract types
*
* @property string $defaultSchema Default schema.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Schema extends \yii\db\Schema
{
private $_defaultSchema;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->defaultSchema === null) {
$this->defaultSchema = $this->db->username;
}
}
/**
* @inheritdoc
......@@ -77,28 +84,11 @@ class Schema extends \yii\db\Schema
$table->schemaName = $parts[0];
$table->name = $parts[1];
} else {
$table->schemaName = $this->getDefaultSchema();
$table->name = $parts[0];
$table->schemaName = $this->defaultSchema;
$table->name = $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;
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
}
/**
......
......@@ -20,13 +20,10 @@ use yii\db\ColumnSchema;
*/
class Schema extends \yii\db\Schema
{
/**
* The default schema used for the current session.
* @var string
* @var string the default schema used for the current session.
*/
public $defaultSchema = 'public';
/**
* @var array mapping from physical column types (keys) to abstract
* column types (values)
......@@ -92,15 +89,16 @@ class Schema extends \yii\db\Schema
protected function resolveTableNames($table, $name)
{
$parts = explode('.', str_replace('"', '', $name));
if (isset($parts[1])) {
$table->schemaName = $parts[0];
$table->name = $parts[1];
} else {
$table->name = $parts[0];
}
if ($table->schemaName === null) {
$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
/**
* 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.
* If not empty, the returned table names will be prefixed with the schema name.
* @return array all table names in the database.
* @return array all table names in the database. The names have NO schema name prefix.
*/
protected function findTableNames($schema = '')
{
......@@ -102,6 +101,7 @@ class Schema extends \yii\db\Schema
{
$table = new TableSchema;
$table->name = $name;
$table->fullName = $name;
if ($this->findColumns($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