Complex classes like QueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class QueryBuilder extends \yii\db\QueryBuilder |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
| 25 | */ |
||
| 26 | public $typeMap = [ |
||
| 27 | Schema::TYPE_PK => 'integer NOT NULL PRIMARY KEY', |
||
| 28 | Schema::TYPE_UPK => 'integer NOT NULL PRIMARY KEY', |
||
| 29 | Schema::TYPE_BIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
| 30 | Schema::TYPE_UBIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
| 31 | Schema::TYPE_CHAR => 'char(1)', |
||
| 32 | Schema::TYPE_STRING => 'varchar(255)', |
||
| 33 | Schema::TYPE_TEXT => 'blob sub_type text', |
||
| 34 | Schema::TYPE_SMALLINT => 'smallint', |
||
| 35 | Schema::TYPE_INTEGER => 'integer', |
||
| 36 | Schema::TYPE_BIGINT => 'bigint', |
||
| 37 | Schema::TYPE_FLOAT => 'float', |
||
| 38 | Schema::TYPE_DOUBLE => 'double precision', |
||
| 39 | Schema::TYPE_DECIMAL => 'numeric(10,0)', |
||
| 40 | Schema::TYPE_DATETIME => 'timestamp', |
||
| 41 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
| 42 | Schema::TYPE_TIME => 'time', |
||
| 43 | Schema::TYPE_DATE => 'date', |
||
| 44 | Schema::TYPE_BINARY => 'blob', |
||
| 45 | Schema::TYPE_BOOLEAN => 'smallint', |
||
| 46 | Schema::TYPE_MONEY => 'numeric(18,4)', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
| 51 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
| 52 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
| 53 | * be included in the result with the additional parameters generated during the query building process. |
||
| 54 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
| 55 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
| 56 | * include those provided in `$params`. |
||
| 57 | */ |
||
| 58 | 176 | public function build($query, $params = []) |
|
| 59 | { |
||
| 60 | 176 | $query = $query->prepare($this); |
|
| 61 | |||
| 62 | 176 | $params = empty($params) ? $query->params : array_merge($params, $query->params); |
|
| 63 | |||
| 64 | $clauses = [ |
||
| 65 | 176 | $this->buildSelect($query->select, $params, $query->distinct, $query->selectOption), |
|
| 66 | 176 | $this->buildFrom($query->from, $params), |
|
| 67 | 176 | $this->buildJoin($query->join, $params), |
|
| 68 | 176 | $this->buildWhere($query->where, $params), |
|
| 69 | 176 | $this->buildGroupBy($query->groupBy), |
|
| 70 | 176 | $this->buildHaving($query->having, $params), |
|
| 71 | ]; |
||
| 72 | |||
| 73 | 176 | $sql = implode($this->separator, array_filter($clauses)); |
|
| 74 | 176 | $sql = $this->buildOrderByAndLimit($sql, $query->orderBy, $query->limit, $query->offset); |
|
| 75 | |||
| 76 | 176 | if (!empty($query->orderBy)) { |
|
| 77 | 33 | foreach ($query->orderBy as $expression) { |
|
| 78 | 33 | if ($expression instanceof Expression) { |
|
| 79 | 33 | $params = array_merge($params, $expression->params); |
|
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | 176 | if (!empty($query->groupBy)) { |
|
| 84 | 2 | foreach ($query->groupBy as $expression) { |
|
| 85 | 2 | if ($expression instanceof Expression) { |
|
| 86 | 2 | $params = array_merge($params, $expression->params); |
|
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | 176 | $union = $this->buildUnion($query->union, $params); |
|
| 92 | 176 | if ($union !== '') { |
|
| 93 | 2 | $sql = "$sql{$this->separator}$union"; |
|
| 94 | } |
||
| 95 | |||
| 96 | 176 | return [$sql, $params]; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | */ |
||
| 102 | 176 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
| 103 | { |
||
| 104 | 176 | if (is_array($columns)) { |
|
| 105 | 48 | foreach ($columns as $i => $column) { |
|
| 106 | 48 | if (!is_string($column)) { |
|
| 107 | 3 | continue; |
|
| 108 | } |
||
| 109 | 47 | $matches = []; |
|
| 110 | 47 | if (preg_match('/^(COUNT|SUM|AVG|MIN|MAX)\([\{\[]{0,2}(\w+|\*)[\}\]]{0,2}\)$/i', $column, $matches)) { |
|
| 111 | 17 | $function = $matches[1]; |
|
| 112 | 17 | $alias = $matches[2] != '*' ? $matches[2] : 'ALL'; |
|
| 113 | |||
| 114 | 47 | $columns[$i] = "{$column} AS {$function}_{$alias}"; |
|
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | 176 | return parent::buildSelect($columns, $params, $distinct, $selectOption); |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | 5 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
| 126 | { |
||
| 127 | 5 | $quotedColumns = []; |
|
| 128 | 5 | foreach ($columns as $i => $column) { |
|
| 129 | 5 | $quotedColumns[$i] = strpos($column, '(') === false ? $this->db->quoteColumnName($column) : $column; |
|
| 130 | } |
||
| 131 | 5 | $vss = []; |
|
| 132 | 5 | foreach ($values as $value) { |
|
| 133 | 5 | $vs = []; |
|
| 134 | 5 | foreach ($columns as $i => $column) { |
|
| 135 | 5 | if (isset($value[$column])) { |
|
| 136 | 5 | $phName = self::PARAM_PREFIX . count($params); |
|
| 137 | 5 | $params[$phName] = $value[$column]; |
|
| 138 | 5 | $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName; |
|
| 139 | } else { |
||
| 140 | 5 | $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL'; |
|
| 141 | } |
||
| 142 | } |
||
| 143 | 5 | $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')'; |
|
| 144 | } |
||
| 145 | 5 | return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')'; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 176 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
| 152 | { |
||
| 153 | |||
| 154 | 176 | $orderBy = $this->buildOrderBy($orderBy); |
|
| 155 | 176 | if ($orderBy !== '') { |
|
| 156 | 33 | $sql .= $this->separator . $orderBy; |
|
| 157 | } |
||
| 158 | |||
| 159 | 176 | $limit = $limit !== null ? intval($limit) : -1; |
|
| 160 | 176 | $offset = $offset !== null ? intval($offset) : -1; |
|
| 161 | // If ignoring both params then do nothing |
||
| 162 | 176 | if ($offset < 0 && $limit < 0) { |
|
| 163 | 175 | return $sql; |
|
| 164 | } |
||
| 165 | // If we are ignoring limit then return full result set starting |
||
| 166 | // from $offset. In Firebird this can only be done with SKIP |
||
| 167 | 3 | if ($offset >= 0 && $limit < 0) { |
|
| 168 | 2 | $count = 1; //Only do it once |
|
| 169 | 2 | $sql = preg_replace('/^SELECT /i', 'SELECT SKIP ' . (int) $offset . ' ', $sql, $count); |
|
| 170 | 2 | return $sql; |
|
| 171 | } |
||
| 172 | // If we are ignoring $offset then return $limit rows. |
||
| 173 | // ie, return the first $limit rows in the set. |
||
| 174 | 2 | if ($offset < 0 && $limit >= 0) { |
|
| 175 | 2 | $count = 1; //Only do it once |
|
| 176 | 2 | $sql = preg_replace('/^SELECT /i', 'SELECT FIRST ' . (int) $limit . ' ', $sql, $count); |
|
| 177 | 2 | return $sql; |
|
| 178 | } |
||
| 179 | // Otherwise apply the params and return the amended sql. |
||
| 180 | 1 | if ($offset >= 0 && $limit >= 0) { |
|
| 181 | 1 | $count = 1; //Only do it once |
|
| 182 | 1 | $sql = preg_replace('/^SELECT /i', 'SELECT FIRST ' . (int) $limit . ' SKIP ' . (int) $offset . ' ', $sql, $count); |
|
| 183 | 1 | return $sql; |
|
| 184 | } |
||
| 185 | // If we have fallen through the cracks then just pass |
||
| 186 | // the sql back. |
||
| 187 | return $sql; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $unions |
||
| 192 | * @param array $params the binding parameters to be populated |
||
| 193 | * @return string the UNION clause built from [[Query::$union]]. |
||
| 194 | */ |
||
| 195 | 176 | public function buildUnion($unions, &$params) |
|
| 196 | { |
||
| 197 | 176 | if (empty($unions)) { |
|
| 198 | 176 | return ''; |
|
| 199 | } |
||
| 200 | |||
| 201 | 2 | $result = ''; |
|
| 202 | |||
| 203 | 2 | foreach ($unions as $i => $union) { |
|
| 204 | 2 | $query = $union['query']; |
|
| 205 | 2 | if ($query instanceof Query) { |
|
| 206 | 2 | list($unions[$i]['query'], $params) = $this->build($query, $params); |
|
| 207 | } |
||
| 208 | |||
| 209 | 2 | $result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . $unions[$i]['query'] . ' '; |
|
| 210 | } |
||
| 211 | |||
| 212 | 2 | return trim($result); |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * |
||
| 217 | * @param Expression $value |
||
| 218 | * @return Expression |
||
| 219 | */ |
||
| 220 | 3 | protected function convertExpression($value) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @inheritdoc |
||
| 238 | */ |
||
| 239 | 22 | public function insert($table, $columns, &$params) |
|
| 240 | { |
||
| 241 | 22 | $schema = $this->db->getSchema(); |
|
| 242 | 22 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
| 243 | 22 | $columnSchemas = $tableSchema->columns; |
|
| 244 | } else { |
||
| 245 | $columnSchemas = []; |
||
| 246 | } |
||
| 247 | |||
| 248 | //Empty insert |
||
| 249 | 22 | if (empty($columns) && !empty($columnSchemas)) { |
|
| 250 | 1 | $columns = []; |
|
| 251 | 1 | foreach ($columnSchemas as $columnSchema) { |
|
| 252 | 1 | if (!$columnSchema->autoIncrement) { |
|
| 253 | 1 | $columns[$columnSchema->name] = $columnSchema->defaultValue; |
|
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | 22 | foreach ($columns as $name => $value) { |
|
| 259 | 22 | if ($value instanceof Expression) { |
|
| 260 | 1 | $columns[$name] = $this->convertExpression($value); |
|
| 261 | 22 | } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { |
|
| 262 | 22 | $columns[$name] = [$value, \PDO::PARAM_LOB]; |
|
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | 22 | return parent::insert($table, $columns, $params); |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritdoc |
||
| 271 | */ |
||
| 272 | 13 | public function update($table, $columns, $condition, &$params) |
|
| 273 | { |
||
| 274 | 13 | $schema = $this->db->getSchema(); |
|
| 275 | 13 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
| 276 | 13 | $columnSchemas = $tableSchema->columns; |
|
| 277 | } else { |
||
| 278 | $columnSchemas = []; |
||
| 279 | } |
||
| 280 | 13 | foreach ($columns as $name => $value) { |
|
| 281 | 13 | if ($value instanceof Expression) { |
|
| 282 | 2 | $columns[$name] = $this->convertExpression($value); |
|
| 283 | 11 | } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { |
|
| 284 | 13 | $columns[$name] = [$value, \PDO::PARAM_LOB]; |
|
| 285 | } |
||
| 286 | } |
||
| 287 | 13 | return parent::update($table, $columns, $condition, $params); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @inheritdoc |
||
| 292 | */ |
||
| 293 | 1 | public function batchInsert($table, $columns, $rows) |
|
| 294 | { |
||
| 295 | 1 | if (empty($rows)) { |
|
| 296 | 1 | return ''; |
|
| 297 | } |
||
| 298 | |||
| 299 | 1 | $schema = $this->db->getSchema(); |
|
| 300 | 1 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
| 301 | 1 | $columnSchemas = $tableSchema->columns; |
|
| 302 | } else { |
||
| 303 | $columnSchemas = []; |
||
| 304 | } |
||
| 305 | |||
| 306 | 1 | $values = []; |
|
| 307 | 1 | foreach ($rows as $row) { |
|
| 308 | 1 | $vs = []; |
|
| 309 | 1 | foreach ($row as $i => $value) { |
|
| 310 | 1 | if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { |
|
| 311 | 1 | $value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
|
| 312 | } |
||
| 313 | 1 | if (is_string($value)) { |
|
| 314 | 1 | $value = $schema->quoteValue($value); |
|
| 315 | 1 | } elseif ($value === false) { |
|
| 316 | $value = 0; |
||
| 317 | 1 | } elseif ($value === null) { |
|
| 318 | 1 | $value = 'NULL'; |
|
| 319 | } |
||
| 320 | 1 | $vs[] = $value; |
|
| 321 | } |
||
| 322 | 1 | $values[] = 'INSERT INTO ' . $schema->quoteTableName($table) |
|
| 323 | 1 | . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $vs) . ');'; |
|
| 324 | } |
||
| 325 | |||
| 326 | 1 | foreach ($columns as $i => $name) { |
|
| 327 | 1 | $columns[$i] = $schema->quoteColumnName($name); |
|
| 328 | } |
||
| 329 | |||
| 330 | 1 | return 'EXECUTE block AS BEGIN ' . implode(' ', $values) . ' END;'; |
|
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @inheritdoc |
||
| 335 | */ |
||
| 336 | 1 | public function renameTable($oldName, $newName) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @inheritdoc |
||
| 343 | */ |
||
| 344 | 2 | public function truncateTable($table) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritdoc |
||
| 351 | */ |
||
| 352 | 1 | public function dropColumn($table, $column) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @inheritdoc |
||
| 360 | */ |
||
| 361 | 1 | public function renameColumn($table, $oldName, $newName) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @inheritdoc |
||
| 370 | */ |
||
| 371 | 3 | public function alterColumn($table, $column, $type) |
|
| 372 | { |
||
| 373 | 3 | $schema = $this->db->getSchema(); |
|
| 374 | 3 | $tableSchema = $schema->getTableSchema($table); |
|
| 375 | 3 | $columnSchema = $tableSchema->getColumn($column); |
|
| 376 | |||
| 377 | 3 | $allowNullNewType = !preg_match('/not +null/i', $type); |
|
| 378 | |||
| 379 | 3 | $type = preg_replace('/ +(not)? *null/i', '', $type); |
|
| 380 | |||
| 381 | 3 | $hasType = false; |
|
| 382 | |||
| 383 | 3 | $matches = []; |
|
| 384 | 3 | if (isset($this->typeMap[$type])) { |
|
| 385 | 2 | $hasType = true; |
|
| 386 | 2 | } elseif (preg_match('/^(\w+)[\( ]/', $type, $matches)) { |
|
| 387 | 2 | if (isset($this->typeMap[$matches[1]])) { |
|
| 388 | 2 | $hasType = true; |
|
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | 3 | $baseSql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
|
| 393 | 3 | . ' ALTER ' . $this->db->quoteColumnName($column) |
|
| 394 | 3 | . (($hasType) ? ' TYPE ' : ' ') . $this->getColumnType($type); |
|
| 395 | |||
| 396 | 3 | if (version_compare($this->db->firebird_version, '3.0.0', '>=')) { |
|
| 397 | $nullSql = false; |
||
| 398 | |||
| 399 | if ($columnSchema->allowNull != $allowNullNewType) { |
||
| 400 | $nullSql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
||
| 401 | . ' ALTER ' . $this->db->quoteColumnName($column) |
||
| 402 | . ($allowNullNewType ? ' DROP' : ' SET') |
||
| 403 | . ' NOT NULL'; |
||
| 404 | } |
||
| 405 | |||
| 406 | $sql = 'EXECUTE BLOCK AS BEGIN' |
||
| 407 | . ' EXECUTE STATEMENT ' . $this->db->quoteValue($baseSql) . ';'; |
||
| 408 | |||
| 409 | /** |
||
| 410 | * In any case (whichever option you choose), make sure that the column doesn't have any NULLs. |
||
| 411 | * Firebird will not check it for you. Later when you backup the database, everything is fine, |
||
| 412 | * but restore will fail as the NOT NULL column has NULLs in it. To be safe, each time you change from NULL to NOT NULL. |
||
| 413 | */ |
||
| 414 | if (!$allowNullNewType) { |
||
| 415 | $sql .= ' UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . $this->db->quoteColumnName($column) . ' = 0' |
||
| 416 | . ' WHERE ' . $this->db->quoteColumnName($column) . ' IS NULL;'; |
||
| 417 | } |
||
| 418 | |||
| 419 | if ($nullSql) { |
||
|
|
|||
| 420 | $sql .= ' EXECUTE STATEMENT ' . $this->db->quoteValue($nullSql) . ';'; |
||
| 421 | } |
||
| 422 | |||
| 423 | $sql .= ' END'; |
||
| 424 | return $sql; |
||
| 425 | } |
||
| 426 | |||
| 427 | 3 | if ($columnSchema->allowNull == $allowNullNewType) { |
|
| 428 | 2 | return $baseSql; |
|
| 429 | } else { |
||
| 430 | $sql = 'EXECUTE BLOCK AS BEGIN' |
||
| 431 | 2 | . ' EXECUTE STATEMENT ' . $this->db->quoteValue($baseSql) . ';' |
|
| 432 | 2 | . ' UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = ' . ($allowNullNewType ? 'NULL' : '1') |
|
| 433 | 2 | . ' WHERE UPPER(RDB$FIELD_NAME) = UPPER(\'' . $column . '\') AND UPPER(RDB$RELATION_NAME) = UPPER(\'' . $table . '\');'; |
|
| 434 | /** |
||
| 435 | * In any case (whichever option you choose), make sure that the column doesn't have any NULLs. |
||
| 436 | * Firebird will not check it for you. Later when you backup the database, everything is fine, |
||
| 437 | * but restore will fail as the NOT NULL column has NULLs in it. To be safe, each time you change from NULL to NOT NULL. |
||
| 438 | */ |
||
| 439 | 2 | if (!$allowNullNewType) { |
|
| 440 | 2 | $sql .= ' UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . $this->db->quoteColumnName($column) . ' = 0' |
|
| 441 | 2 | . ' WHERE ' . $this->db->quoteColumnName($column) . ' IS NULL;'; |
|
| 442 | } |
||
| 443 | 2 | $sql .= ' END'; |
|
| 444 | 2 | return $sql; |
|
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritdoc |
||
| 450 | */ |
||
| 451 | 1 | public function dropIndex($name, $table) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @inheritdoc |
||
| 458 | */ |
||
| 459 | 1 | public function resetSequence($table, $value = null) |
|
| 460 | { |
||
| 461 | 1 | $tableSchema = $this->db->getTableSchema($table); |
|
| 462 | 1 | if ($tableSchema === null) { |
|
| 463 | throw new InvalidParamException("Table not found: $table"); |
||
| 464 | } |
||
| 465 | 1 | if ($tableSchema->sequenceName === null) { |
|
| 466 | throw new InvalidParamException("There is not sequence associated with table '$table'."); |
||
| 467 | } |
||
| 468 | |||
| 469 | 1 | if ($value !== null) { |
|
| 470 | 1 | $value = (int) $value; |
|
| 471 | } else { |
||
| 472 | // use master connection to get the biggest PK value |
||
| 473 | 1 | $value = $this->db->useMaster(function (Connection $db) use ($tableSchema) { |
|
| 474 | 1 | $key = false; |
|
| 475 | 1 | foreach ($tableSchema->primaryKey as $name) { |
|
| 476 | 1 | if ($tableSchema->columns[$name]->autoIncrement) { |
|
| 477 | 1 | $key = $name; |
|
| 478 | 1 | break; |
|
| 479 | } |
||
| 480 | } |
||
| 481 | 1 | if ($key === false) { |
|
| 482 | return 0; |
||
| 483 | } |
||
| 484 | 1 | return $db->createCommand("SELECT MAX({$this->db->quoteColumnName($key)}) FROM {$this->db->quoteTableName($tableSchema->name)}")->queryScalar(); |
|
| 485 | 1 | }) + 1; |
|
| 486 | } |
||
| 487 | |||
| 488 | 1 | return "ALTER SEQUENCE {$this->db->quoteColumnName($tableSchema->sequenceName)} RESTART WITH $value"; |
|
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @inheritdoc |
||
| 493 | */ |
||
| 494 | 5 | public function createTable($table, $columns, $options = null) |
|
| 495 | { |
||
| 496 | 5 | $sql = parent::createTable($table, $columns, $options); |
|
| 497 | |||
| 498 | 5 | foreach ($columns as $name => $type) { |
|
| 499 | 5 | if (!is_string($name)) { |
|
| 500 | continue; |
||
| 501 | } |
||
| 502 | |||
| 503 | 5 | if (strpos($type, Schema::TYPE_PK) === 0 || strpos($type, Schema::TYPE_BIGPK) === 0) { |
|
| 504 | $sqlTrigger = <<<SQLTRIGGER |
||
| 505 | 4 | CREATE TRIGGER tr_{$table}_{$name} FOR {$this->db->quoteTableName($table)} |
|
| 506 | ACTIVE BEFORE INSERT POSITION 0 |
||
| 507 | AS |
||
| 508 | BEGIN |
||
| 509 | 4 | if (NEW.{$this->db->quoteColumnName($name)} is NULL) then NEW.{$this->db->quoteColumnName($name)} = NEXT VALUE FOR seq_{$table}_{$name}; |
|
| 510 | 4 | END |
|
| 511 | SQLTRIGGER; |
||
| 512 | |||
| 513 | $sqlBlock = <<<SQL |
||
| 514 | EXECUTE block AS |
||
| 515 | BEGIN |
||
| 516 | 4 | EXECUTE STATEMENT {$this->db->quoteValue($sql)}; |
|
| 517 | 4 | EXECUTE STATEMENT {$this->db->quoteValue("CREATE SEQUENCE seq_{$table}_{$name}")}; |
|
| 518 | 4 | EXECUTE STATEMENT {$this->db->quoteValue($sqlTrigger)}; |
|
| 519 | 4 | END; |
|
| 520 | SQL; |
||
| 521 | |||
| 522 | 5 | return $sqlBlock; |
|
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | 1 | return $sql; |
|
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @inheritdoc |
||
| 531 | */ |
||
| 532 | 3 | public function dropTable($table) |
|
| 533 | { |
||
| 534 | 3 | $sql = parent::dropTable($table); |
|
| 535 | |||
| 536 | 3 | $tableSchema = $this->db->getTableSchema($table); |
|
| 537 | 3 | if ($tableSchema === null || $tableSchema->sequenceName === null) { |
|
| 538 | 1 | return $sql; |
|
| 539 | } |
||
| 540 | |||
| 541 | $sqlBlock = <<<SQL |
||
| 542 | EXECUTE block AS |
||
| 543 | BEGIN |
||
| 544 | 2 | EXECUTE STATEMENT {$this->db->quoteValue($sql)}; |
|
| 545 | 2 | EXECUTE STATEMENT {$this->db->quoteValue("DROP SEQUENCE {$tableSchema->sequenceName}")}; |
|
| 546 | 2 | END; |
|
| 547 | SQL; |
||
| 548 | 2 | return $sqlBlock; |
|
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Creates a SELECT EXISTS() SQL statement. |
||
| 553 | * @param string $rawSql the subquery in a raw form to select from. |
||
| 554 | * @return string the SELECT EXISTS() SQL statement. |
||
| 555 | * |
||
| 556 | * @since 2.0.8 |
||
| 557 | */ |
||
| 558 | 3 | public function selectExists($rawSql) |
|
| 562 | } |
||
| 563 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: