Fixes #14367: In yii\db\mysql\QueryBuilder added support fractional seconds for time types for MySQL >= 5.6.4

This commit is contained in:
Konstantin Voloshchuk
2018-10-11 17:43:32 +06:00
committed by Alexander Makarov
parent 5d7c5f8ae2
commit 9f383ab228
4 changed files with 107 additions and 11 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.16 under development
------------------------
- Enh #14367: In `yii\db\mysql\QueryBuilder` added support fractional seconds for time types for MySQL >= 5.6.4 (konstantin-vl)
- Bug #16766: `yii\filters\ContentNegotiator` was not setting `Vary` header to inform cache recipients (koteq, cebe, samdark)
- Bug #11960: Fixed `checked` option ignore in `yii\helpers\BaseHtml::checkbox()` (misantron)
- Bug #14759: Fixed `yii\web\JsonResponseFormatter` output for `null` data (misantron)

View File

@ -39,9 +39,6 @@ class QueryBuilder extends \yii\db\QueryBuilder
Schema::TYPE_FLOAT => 'float',
Schema::TYPE_DOUBLE => 'double',
Schema::TYPE_DECIMAL => 'decimal(10,0)',
Schema::TYPE_DATETIME => 'datetime',
Schema::TYPE_TIMESTAMP => 'timestamp',
Schema::TYPE_TIME => 'time',
Schema::TYPE_DATE => 'date',
Schema::TYPE_BINARY => 'blob',
Schema::TYPE_BOOLEAN => 'tinyint(1)',
@ -49,6 +46,15 @@ class QueryBuilder extends \yii\db\QueryBuilder
Schema::TYPE_JSON => 'json'
];
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->typeMap = array_merge($this->typeMap, $this->defaultTimeTypeMap());
}
/**
* {@inheritdoc}
@ -362,4 +368,41 @@ class QueryBuilder extends \yii\db\QueryBuilder
return null;
}
/**
* Checks the ability to use fractional seconds.
*
* @return bool
* @see https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
*/
private function supportsFractionalSeconds()
{
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
return version_compare($version, '5.6.4', '>=');
}
/**
* Returns the map for default time type.
* If the version of MySQL is lower than 5.6.4, then the types will be without fractional seconds,
* otherwise with fractional seconds.
*
* @return array
*/
private function defaultTimeTypeMap()
{
$map = [
Schema::TYPE_DATETIME => 'datetime',
Schema::TYPE_TIMESTAMP => 'timestamp',
Schema::TYPE_TIME => 'time',
];
if ($this->supportsFractionalSeconds()) {
$map = [
Schema::TYPE_DATETIME => 'datetime(0)',
Schema::TYPE_TIMESTAMP => 'timestamp(0)',
Schema::TYPE_TIME => 'time(0)',
];
}
return $map;
}
}