123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace yii\db;
- use yii\base\BaseObject;
- use yii\helpers\StringHelper;
- class ColumnSchema extends BaseObject
- {
-
- public $name;
-
- public $allowNull;
-
- public $type;
-
- public $phpType;
-
- public $dbType;
-
- public $defaultValue;
-
- public $enumValues;
-
- public $size;
-
- public $precision;
-
- public $scale;
-
- public $isPrimaryKey;
-
- public $autoIncrement = false;
-
- public $unsigned;
-
- public $comment;
-
- public function phpTypecast($value)
- {
- return $this->typecast($value);
- }
-
- public function dbTypecast($value)
- {
-
-
- return $this->typecast($value);
- }
-
- protected function typecast($value)
- {
- if ($value === ''
- && !in_array(
- $this->type,
- [
- Schema::TYPE_TEXT,
- Schema::TYPE_STRING,
- Schema::TYPE_BINARY,
- Schema::TYPE_CHAR
- ],
- true)
- ) {
- return null;
- }
- if ($value === null
- || gettype($value) === $this->phpType
- || $value instanceof ExpressionInterface
- || $value instanceof Query
- ) {
- return $value;
- }
- if (is_array($value)
- && count($value) === 2
- && isset($value[1])
- && in_array($value[1], $this->getPdoParamTypes(), true)
- ) {
- return new PdoValue($value[0], $value[1]);
- }
- switch ($this->phpType) {
- case 'resource':
- case 'string':
- if (is_resource($value)) {
- return $value;
- }
- if (is_float($value)) {
-
- return StringHelper::floatToString($value);
- }
- return (string) $value;
- case 'integer':
- return (int) $value;
- case 'boolean':
-
-
- return (bool) $value && $value !== "\0";
- case 'double':
- return (float) $value;
- }
- return $value;
- }
-
- private function getPdoParamTypes()
- {
- return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT];
- }
- }
|