Commit 651e6a3f by Carsten Brandt

added support for BIT default values

fixes #422
parent 4f95fcd9
...@@ -57,6 +57,7 @@ Yii Framework 2 Change Log ...@@ -57,6 +57,7 @@ Yii Framework 2 Change Log
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul) - Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue) - Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
- Bug: Fixed Object of class Imagick could not be converted to string in CaptchaAction (eXprojects, cebe) - Bug: Fixed Object of class Imagick could not be converted to string in CaptchaAction (eXprojects, cebe)
- Enh #422: Added Support for BIT(M) data type default values in Schema (cebe)
- Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue) - Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue)
- Enh #2435: `yii\db\IntegrityException` is now thrown on database integrity errors instead of general `yii\db\Exception` (samdark) - Enh #2435: `yii\db\IntegrityException` is now thrown on database integrity errors instead of general `yii\db\Exception` (samdark)
- Enh #2558: Enhanced support for memcached by adding `yii\caching\MemCache::persistentId` and `yii\caching\MemCache::options` (qiangxue) - Enh #2558: Enhanced support for memcached by adding `yii\caching\MemCache::persistentId` and `yii\caching\MemCache::options` (qiangxue)
......
...@@ -55,8 +55,8 @@ class Schema extends \yii\db\Schema ...@@ -55,8 +55,8 @@ class Schema extends \yii\db\Schema
'blob' => self::TYPE_BINARY, 'blob' => self::TYPE_BINARY,
'clob' => self::TYPE_BINARY, 'clob' => self::TYPE_BINARY,
// Bit string data types // Bit string data types
'bit' => self::TYPE_STRING, 'bit' => self::TYPE_INTEGER,
'bit varying' => self::TYPE_STRING, 'bit varying' => self::TYPE_INTEGER,
// Collection data types (considered strings for now) // Collection data types (considered strings for now)
'set' => self::TYPE_STRING, 'set' => self::TYPE_STRING,
'multiset' => self::TYPE_STRING, 'multiset' => self::TYPE_STRING,
...@@ -223,6 +223,15 @@ class Schema extends \yii\db\Schema ...@@ -223,6 +223,15 @@ class Schema extends \yii\db\Schema
if (isset($values[1])) { if (isset($values[1])) {
$column->scale = (int) $values[1]; $column->scale = (int) $values[1];
} }
if ($column->size === 1 && $type === 'bit') {
$column->type = 'boolean';
} elseif ($type === 'bit') {
if ($column->size > 32) {
$column->type = 'bigint';
} elseif ($column->size === 32) {
$column->type = 'integer';
}
}
} }
} }
} }
...@@ -239,6 +248,8 @@ class Schema extends \yii\db\Schema ...@@ -239,6 +248,8 @@ class Schema extends \yii\db\Schema
$column->type === 'time' && $info['Default'] === 'SYS_TIME' $column->type === 'time' && $info['Default'] === 'SYS_TIME'
) { ) {
$column->defaultValue = new Expression($info['Default']); $column->defaultValue = new Expression($info['Default']);
} elseif (isset($type) && $type === 'bit') {
$column->defaultValue = hexdec(trim($info['Default'],'X\''));
} else { } else {
$column->defaultValue = $column->typecast($info['Default']); $column->defaultValue = $column->typecast($info['Default']);
} }
......
...@@ -24,7 +24,7 @@ class Schema extends \yii\db\Schema ...@@ -24,7 +24,7 @@ class Schema extends \yii\db\Schema
*/ */
public $typeMap = [ public $typeMap = [
'tinyint' => self::TYPE_SMALLINT, 'tinyint' => self::TYPE_SMALLINT,
'bit' => self::TYPE_SMALLINT, 'bit' => self::TYPE_INTEGER,
'smallint' => self::TYPE_SMALLINT, 'smallint' => self::TYPE_SMALLINT,
'mediumint' => self::TYPE_INTEGER, 'mediumint' => self::TYPE_INTEGER,
'int' => self::TYPE_INTEGER, 'int' => self::TYPE_INTEGER,
...@@ -172,6 +172,8 @@ class Schema extends \yii\db\Schema ...@@ -172,6 +172,8 @@ class Schema extends \yii\db\Schema
if (!$column->isPrimaryKey) { if (!$column->isPrimaryKey) {
if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP') { if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP') {
$column->defaultValue = new Expression('CURRENT_TIMESTAMP'); $column->defaultValue = new Expression('CURRENT_TIMESTAMP');
} elseif (isset($type) && $type === 'bit') {
$column->defaultValue = bindec(trim($info['Default'],'b\''));
} else { } else {
$column->defaultValue = $column->typecast($info['Default']); $column->defaultValue = $column->typecast($info['Default']);
} }
......
...@@ -29,9 +29,9 @@ class Schema extends \yii\db\Schema ...@@ -29,9 +29,9 @@ class Schema extends \yii\db\Schema
* @see http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-TABLE * @see http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-TABLE
*/ */
public $typeMap = [ public $typeMap = [
'bit' => self::TYPE_STRING, 'bit' => self::TYPE_INTEGER,
'bit varying' => self::TYPE_STRING, 'bit varying' => self::TYPE_INTEGER,
'varbit' => self::TYPE_STRING, 'varbit' => self::TYPE_INTEGER,
'bool' => self::TYPE_BOOLEAN, 'bool' => self::TYPE_BOOLEAN,
'boolean' => self::TYPE_BOOLEAN, 'boolean' => self::TYPE_BOOLEAN,
...@@ -408,7 +408,9 @@ SQL; ...@@ -408,7 +408,9 @@ SQL;
} }
$column->defaultValue = null; $column->defaultValue = null;
} elseif ($column->defaultValue) { } elseif ($column->defaultValue) {
if (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches) || preg_match("/^(.*?)::/", $column->defaultValue, $matches)) { if (stripos($column->dbType, 'bit') === 0 || stripos($column->dbType, 'varbit') === 0) {
$column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches) || preg_match("/^(.*?)::/", $column->defaultValue, $matches)) {
$column->defaultValue = $matches[1]; $column->defaultValue = $matches[1];
} }
} }
......
...@@ -111,7 +111,8 @@ CREATE TABLE "type" ( ...@@ -111,7 +111,8 @@ CREATE TABLE "type" (
"time" timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', "time" timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
"bool_col" tinyint NOT NULL, "bool_col" tinyint NOT NULL,
"bool_col2" tinyint DEFAULT '1', "bool_col2" tinyint DEFAULT '1',
"ts_default" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "ts_default" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"bit_col" BIT(8) NOT NULL DEFAULT b'10000010'
); );
CREATE TABLE "composite_fk" ( CREATE TABLE "composite_fk" (
......
...@@ -121,7 +121,8 @@ CREATE TABLE `type` ( ...@@ -121,7 +121,8 @@ CREATE TABLE `type` (
`time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', `time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
`bool_col` tinyint(1) NOT NULL, `bool_col` tinyint(1) NOT NULL,
`bool_col2` tinyint(1) DEFAULT '1', `bool_col2` tinyint(1) DEFAULT '1',
`ts_default` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP `ts_default` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`bit_col` BIT(8) NOT NULL DEFAULT b'10000010'
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `profile` (description) VALUES ('profile customer 1'); INSERT INTO `profile` (description) VALUES ('profile customer 1');
......
...@@ -100,7 +100,8 @@ CREATE TABLE "type" ( ...@@ -100,7 +100,8 @@ CREATE TABLE "type" (
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
bool_col smallint NOT NULL, bool_col smallint NOT NULL,
bool_col2 smallint DEFAULT '1', bool_col2 smallint DEFAULT '1',
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
bit_col BIT NOT NULL DEFAULT B'10000010'
); );
INSERT INTO "profile" (description) VALUES ('profile customer 1'); INSERT INTO "profile" (description) VALUES ('profile customer 1');
......
...@@ -263,6 +263,18 @@ class SchemaTest extends DatabaseTestCase ...@@ -263,6 +263,18 @@ class SchemaTest extends DatabaseTestCase
'scale' => null, 'scale' => null,
'defaultValue' => new Expression('CURRENT_TIMESTAMP'), 'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
], ],
'bit_col' => [
'type' => 'integer',
'dbType' => 'bit(8)',
'phpType' => 'integer',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 8,
'precision' => 8,
'scale' => null,
'defaultValue' => 130, // b'10000010'
],
]; ];
} }
......
...@@ -15,6 +15,7 @@ class SqliteSchemaTest extends SchemaTest ...@@ -15,6 +15,7 @@ class SqliteSchemaTest extends SchemaTest
{ {
$columns = parent::getExpectedColumns(); $columns = parent::getExpectedColumns();
unset($columns['enum_col']); unset($columns['enum_col']);
unset($columns['bit_col']);
$columns['int_col']['dbType'] = 'integer'; $columns['int_col']['dbType'] = 'integer';
$columns['int_col']['size'] = null; $columns['int_col']['size'] = null;
$columns['int_col']['precision'] = null; $columns['int_col']['precision'] = null;
......
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