ColumnSchemaBuilder.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\mssql;
  8. use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
  9. use yii\db\Expression;
  10. /**
  11. * ColumnSchemaBuilder is the schema builder for MSSQL databases.
  12. *
  13. * @property-read string|null $checkValue The `CHECK` constraint for the column. This property is read-only.
  14. * @property-read string|Expression|null $defaultValue Default value of the column. This property is
  15. * read-only.
  16. *
  17. * @author Valerii Gorbachev <darkdef@gmail.com>
  18. * @since 2.0.42
  19. */
  20. class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
  21. {
  22. protected $format = '{type}{length}{notnull}{unique}{default}{check}{append}';
  23. /**
  24. * Builds the full string for the column's schema.
  25. * @return string
  26. */
  27. public function __toString()
  28. {
  29. if ($this->getTypeCategory() === self::CATEGORY_PK) {
  30. $format = '{type}{check}{comment}{append}';
  31. } else {
  32. $format = $this->format;
  33. }
  34. return $this->buildCompleteString($format);
  35. }
  36. /**
  37. * Changes default format string to MSSQL ALTER COMMAND.
  38. */
  39. public function setAlterColumnFormat()
  40. {
  41. $this->format = '{type}{length}{notnull}{append}';
  42. }
  43. /**
  44. * Getting the `Default` value for constraint
  45. * @return string|Expression|null default value of the column.
  46. */
  47. public function getDefaultValue()
  48. {
  49. if ($this->default instanceof Expression) {
  50. return $this->default;
  51. }
  52. return $this->buildDefaultValue();
  53. }
  54. /**
  55. * Get the `Check` value for constraint
  56. * @return string|null the `CHECK` constraint for the column.
  57. */
  58. public function getCheckValue()
  59. {
  60. return $this->check !== null ? (string) $this->check : null;
  61. }
  62. /**
  63. * @return bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added.
  64. */
  65. public function isUnique()
  66. {
  67. return $this->isUnique;
  68. }
  69. }