Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 lines
4.5KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use yii\base\Object;
  9. /**
  10. * ColumnSchema class describes the metadata of a column in a database table.
  11. *
  12. * @author Qiang Xue <qiang.xue@gmail.com>
  13. * @since 2.0
  14. */
  15. class ColumnSchema extends Object
  16. {
  17. /**
  18. * @var string name of this column (without quotes).
  19. */
  20. public $name;
  21. /**
  22. * @var boolean whether this column can be null.
  23. */
  24. public $allowNull;
  25. /**
  26. * @var string abstract type of this column. Possible abstract types include:
  27. * char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
  28. * timestamp, time, date, binary, and money.
  29. */
  30. public $type;
  31. /**
  32. * @var string the PHP type of this column. Possible PHP types include:
  33. * `string`, `boolean`, `integer`, `double`.
  34. */
  35. public $phpType;
  36. /**
  37. * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
  38. */
  39. public $dbType;
  40. /**
  41. * @var mixed default value of this column
  42. */
  43. public $defaultValue;
  44. /**
  45. * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
  46. */
  47. public $enumValues;
  48. /**
  49. * @var integer display size of the column.
  50. */
  51. public $size;
  52. /**
  53. * @var integer precision of the column data, if it is numeric.
  54. */
  55. public $precision;
  56. /**
  57. * @var integer scale of the column data, if it is numeric.
  58. */
  59. public $scale;
  60. /**
  61. * @var boolean whether this column is a primary key
  62. */
  63. public $isPrimaryKey;
  64. /**
  65. * @var boolean whether this column is auto-incremental
  66. */
  67. public $autoIncrement = false;
  68. /**
  69. * @var boolean whether this column is unsigned. This is only meaningful
  70. * when [[type]] is `smallint`, `integer` or `bigint`.
  71. */
  72. public $unsigned;
  73. /**
  74. * @var string comment of this column. Not all DBMS support this.
  75. */
  76. public $comment;
  77. /**
  78. * Converts the input value according to [[phpType]] after retrieval from the database.
  79. * If the value is null or an [[Expression]], it will not be converted.
  80. * @param mixed $value input value
  81. * @return mixed converted value
  82. */
  83. public function phpTypecast($value)
  84. {
  85. return $this->typecast($value);
  86. }
  87. /**
  88. * Converts the input value according to [[type]] and [[dbType]] for use in a db query.
  89. * If the value is null or an [[Expression]], it will not be converted.
  90. * @param mixed $value input value
  91. * @return mixed converted value. This may also be an array containing the value as the first element
  92. * and the PDO type as the second element.
  93. */
  94. public function dbTypecast($value)
  95. {
  96. // the default implementation does the same as casting for PHP, but it should be possible
  97. // to override this with annotation of explicit PDO type.
  98. return $this->typecast($value);
  99. }
  100. /**
  101. * Converts the input value according to [[phpType]] after retrieval from the database.
  102. * If the value is null or an [[Expression]], it will not be converted.
  103. * @param mixed $value input value
  104. * @return mixed converted value
  105. * @since 2.0.3
  106. */
  107. protected function typecast($value)
  108. {
  109. if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY && $this->type !== Schema::TYPE_CHAR) {
  110. return null;
  111. }
  112. if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
  113. return $value;
  114. }
  115. switch ($this->phpType) {
  116. case 'resource':
  117. case 'string':
  118. if (is_resource($value)) {
  119. return $value;
  120. }
  121. if (is_float($value)) {
  122. // ensure type cast always has . as decimal separator in all locales
  123. return str_replace(',', '.', (string) $value);
  124. }
  125. return (string) $value;
  126. case 'integer':
  127. return (int) $value;
  128. case 'boolean':
  129. // treating a 0 bit value as false too
  130. // https://github.com/yiisoft/yii2/issues/9006
  131. return (bool) $value && $value !== "\0";
  132. case 'double':
  133. return (double) $value;
  134. }
  135. return $value;
  136. }
  137. }