QueryBuilder.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\base\InvalidParamException;
  9. /**
  10. * QueryBuilder is the query builder for MS SQL Server databases (version 2008 and above).
  11. *
  12. * @author Timur Ruziev <[email protected]>
  13. * @since 2.0
  14. */
  15. class QueryBuilder extends \yii\db\QueryBuilder
  16. {
  17. /**
  18. * @var array mapping from abstract column types (keys) to physical column types (values).
  19. */
  20. public $typeMap = [
  21. Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY',
  22. Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY',
  23. Schema::TYPE_STRING => 'varchar(255)',
  24. Schema::TYPE_TEXT => 'text',
  25. Schema::TYPE_SMALLINT => 'smallint',
  26. Schema::TYPE_INTEGER => 'int',
  27. Schema::TYPE_BIGINT => 'bigint',
  28. Schema::TYPE_FLOAT => 'float',
  29. Schema::TYPE_DECIMAL => 'decimal',
  30. Schema::TYPE_DATETIME => 'datetime',
  31. Schema::TYPE_TIMESTAMP => 'timestamp',
  32. Schema::TYPE_TIME => 'time',
  33. Schema::TYPE_DATE => 'date',
  34. Schema::TYPE_BINARY => 'binary',
  35. Schema::TYPE_BOOLEAN => 'bit',
  36. Schema::TYPE_MONEY => 'decimal(19,4)',
  37. ];
  38. // public function update($table, $columns, $condition, &$params)
  39. // {
  40. // return '';
  41. // }
  42. // public function delete($table, $condition, &$params)
  43. // {
  44. // return '';
  45. // }
  46. // public function buildLimit($limit, $offset)
  47. // {
  48. // return '';
  49. // }
  50. // public function resetSequence($table, $value = null)
  51. // {
  52. // return '';
  53. // }
  54. /**
  55. * Builds a SQL statement for renaming a DB table.
  56. * @param string $table the table to be renamed. The name will be properly quoted by the method.
  57. * @param string $newName the new table name. The name will be properly quoted by the method.
  58. * @return string the SQL statement for renaming a DB table.
  59. */
  60. public function renameTable($table, $newName)
  61. {
  62. return "sp_rename '$table', '$newName'";
  63. }
  64. /**
  65. * Builds a SQL statement for renaming a column.
  66. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
  67. * @param string $name the old name of the column. The name will be properly quoted by the method.
  68. * @param string $newName the new name of the column. The name will be properly quoted by the method.
  69. * @return string the SQL statement for renaming a DB column.
  70. */
  71. public function renameColumn($table, $name, $newName)
  72. {
  73. return "sp_rename '$table.$name', '$newName', 'COLUMN'";
  74. }
  75. /**
  76. * Builds a SQL statement for changing the definition of a column.
  77. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  78. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  79. * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  80. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  81. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  82. * @return string the SQL statement for changing the definition of a column.
  83. */
  84. public function alterColumn($table, $column, $type)
  85. {
  86. $type = $this->getColumnType($type);
  87. $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ALTER COLUMN '
  88. . $this->db->quoteColumnName($column) . ' '
  89. . $this->getColumnType($type);
  90. return $sql;
  91. }
  92. /**
  93. * Builds a SQL statement for enabling or disabling integrity check.
  94. * @param boolean $check whether to turn on or off the integrity check.
  95. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  96. * @param string $table the table name. Defaults to empty string, meaning that no table will be changed.
  97. * @return string the SQL statement for checking integrity
  98. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  99. */
  100. public function checkIntegrity($check = true, $schema = '', $table = '')
  101. {
  102. if ($schema !== '') {
  103. $table = "{$schema}.{$table}";
  104. }
  105. $table = $this->db->quoteTableName($table);
  106. if ($this->db->getTableSchema($table) === null) {
  107. throw new InvalidParamException("Table not found: $table");
  108. }
  109. $enable = $check ? 'CHECK' : 'NOCHECK';
  110. return "ALTER TABLE {$table} {$enable} CONSTRAINT ALL";
  111. }
  112. }