Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class Schema extends Object |
||
39 | { |
||
40 | // The following are the supported abstract column data types. |
||
41 | const TYPE_PK = 'pk'; |
||
42 | const TYPE_UPK = 'upk'; |
||
43 | const TYPE_BIGPK = 'bigpk'; |
||
44 | const TYPE_UBIGPK = 'ubigpk'; |
||
45 | const TYPE_CHAR = 'char'; |
||
46 | const TYPE_STRING = 'string'; |
||
47 | const TYPE_TEXT = 'text'; |
||
48 | const TYPE_SMALLINT = 'smallint'; |
||
49 | const TYPE_INTEGER = 'integer'; |
||
50 | const TYPE_BIGINT = 'bigint'; |
||
51 | const TYPE_FLOAT = 'float'; |
||
52 | const TYPE_DOUBLE = 'double'; |
||
53 | const TYPE_DECIMAL = 'decimal'; |
||
54 | const TYPE_DATETIME = 'datetime'; |
||
55 | const TYPE_TIMESTAMP = 'timestamp'; |
||
56 | const TYPE_TIME = 'time'; |
||
57 | const TYPE_DATE = 'date'; |
||
58 | const TYPE_BINARY = 'binary'; |
||
59 | const TYPE_BOOLEAN = 'boolean'; |
||
60 | const TYPE_MONEY = 'money'; |
||
61 | |||
62 | /** |
||
63 | * @var Connection the database connection |
||
64 | */ |
||
65 | public $db; |
||
66 | /** |
||
67 | * @var string the default schema name used for the current session. |
||
68 | */ |
||
69 | public $defaultSchema; |
||
70 | /** |
||
71 | * @var array map of DB errors and corresponding exceptions |
||
72 | * If left part is found in DB error message exception class from the right part is used. |
||
73 | */ |
||
74 | public $exceptionMap = [ |
||
75 | 'SQLSTATE[23' => 'yii\db\IntegrityException', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @var string column schema class |
||
80 | * @since 2.0.11 |
||
81 | */ |
||
82 | public $columnSchemaClass = 'yii\db\ColumnSchema'; |
||
83 | |||
84 | /** |
||
85 | * @var array list of ALL schema names in the database, except system schemas |
||
86 | */ |
||
87 | private $_schemaNames; |
||
88 | /** |
||
89 | * @var array list of ALL table names in the database |
||
90 | */ |
||
91 | private $_tableNames = []; |
||
92 | /** |
||
93 | * @var array list of loaded table metadata (table name => TableSchema) |
||
94 | */ |
||
95 | private $_tables = []; |
||
96 | /** |
||
97 | * @var QueryBuilder the query builder for this database |
||
98 | */ |
||
99 | private $_builder; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * @return \yii\db\ColumnSchema |
||
104 | * @throws \yii\base\InvalidConfigException |
||
105 | */ |
||
106 | 283 | protected function createColumnSchema() |
|
110 | |||
111 | /** |
||
112 | * Loads the metadata for the specified table. |
||
113 | * @param string $name table name |
||
114 | * @return null|TableSchema DBMS-dependent table metadata, null if the table does not exist. |
||
115 | */ |
||
116 | abstract protected function loadTableSchema($name); |
||
117 | |||
118 | /** |
||
119 | * Obtains the metadata for the named table. |
||
120 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
121 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
122 | * @return null|TableSchema table metadata. Null if the named table does not exist. |
||
123 | */ |
||
124 | 331 | public function getTableSchema($name, $refresh = false) |
|
155 | |||
156 | /** |
||
157 | * Returns the cache key for the specified table name. |
||
158 | * @param string $name the table name |
||
159 | * @return mixed the cache key |
||
160 | */ |
||
161 | 4 | protected function getCacheKey($name) |
|
170 | |||
171 | /** |
||
172 | * Returns the cache tag name. |
||
173 | * This allows [[refresh()]] to invalidate all cached table schemas. |
||
174 | * @return string the cache tag name |
||
175 | */ |
||
176 | 4 | protected function getCacheTag() |
|
184 | |||
185 | /** |
||
186 | * Returns the metadata for all tables in the database. |
||
187 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
188 | * @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||
189 | * cached data may be returned if available. |
||
190 | * @return TableSchema[] the metadata for all tables in the database. |
||
191 | * Each array element is an instance of [[TableSchema]] or its child class. |
||
192 | */ |
||
193 | 6 | public function getTableSchemas($schema = '', $refresh = false) |
|
207 | |||
208 | /** |
||
209 | * Returns all schema names in the database, except system schemas. |
||
210 | * @param bool $refresh whether to fetch the latest available schema names. If this is false, |
||
211 | * schema names fetched previously (if available) will be returned. |
||
212 | * @return string[] all schema names in the database, except system schemas. |
||
213 | * @since 2.0.4 |
||
214 | */ |
||
215 | 1 | public function getSchemaNames($refresh = false) |
|
223 | |||
224 | /** |
||
225 | * Returns all table names in the database. |
||
226 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
227 | * If not empty, the returned table names will be prefixed with the schema name. |
||
228 | * @param bool $refresh whether to fetch the latest available table names. If this is false, |
||
229 | * table names fetched previously (if available) will be returned. |
||
230 | * @return string[] all table names in the database. |
||
231 | */ |
||
232 | 12 | public function getTableNames($schema = '', $refresh = false) |
|
240 | |||
241 | /** |
||
242 | * @return QueryBuilder the query builder for this connection. |
||
243 | */ |
||
244 | 354 | public function getQueryBuilder() |
|
252 | |||
253 | /** |
||
254 | * Determines the PDO type for the given PHP data value. |
||
255 | * @param mixed $data the data whose PDO type is to be determined |
||
256 | * @return int the PDO type |
||
257 | * @see http://www.php.net/manual/en/pdo.constants.php |
||
258 | */ |
||
259 | 320 | public function getPdoType($data) |
|
273 | |||
274 | /** |
||
275 | * Refreshes the schema. |
||
276 | * This method cleans up all cached table schemas so that they can be re-created later |
||
277 | * to reflect the database schema change. |
||
278 | */ |
||
279 | 13 | public function refresh() |
|
280 | { |
||
281 | /* @var $cache Cache */ |
||
282 | 13 | $cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache; |
|
283 | 13 | if ($this->db->enableSchemaCache && $cache instanceof Cache) { |
|
284 | TagDependency::invalidate($cache, $this->getCacheTag()); |
||
285 | } |
||
286 | 13 | $this->_tableNames = []; |
|
287 | 13 | $this->_tables = []; |
|
288 | 13 | } |
|
289 | |||
290 | /** |
||
291 | * Refreshes the particular table schema. |
||
292 | * This method cleans up cached table schema so that it can be re-created later |
||
293 | * to reflect the database schema change. |
||
294 | * @param string $name table name. |
||
295 | * @since 2.0.6 |
||
296 | */ |
||
297 | 13 | public function refreshTableSchema($name) |
|
307 | |||
308 | /** |
||
309 | * Creates a query builder for the database. |
||
310 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
311 | * @return QueryBuilder query builder instance |
||
312 | */ |
||
313 | public function createQueryBuilder() |
||
317 | |||
318 | /** |
||
319 | * Create a column schema builder instance giving the type and value precision. |
||
320 | * |
||
321 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
322 | * |
||
323 | * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
||
324 | * @param int|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
||
325 | * @return ColumnSchemaBuilder column schema builder instance |
||
326 | * @since 2.0.6 |
||
327 | */ |
||
328 | 2 | public function createColumnSchemaBuilder($type, $length = null) |
|
332 | |||
333 | /** |
||
334 | * Returns all schema names in the database, including the default one but not system schemas. |
||
335 | * This method should be overridden by child classes in order to support this feature |
||
336 | * because the default implementation simply throws an exception. |
||
337 | * @return array all schema names in the database, except system schemas |
||
338 | * @throws NotSupportedException if this method is called |
||
339 | * @since 2.0.4 |
||
340 | */ |
||
341 | protected function findSchemaNames() |
||
345 | |||
346 | /** |
||
347 | * Returns all table names in the database. |
||
348 | * This method should be overridden by child classes in order to support this feature |
||
349 | * because the default implementation simply throws an exception. |
||
350 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
351 | * @return array all table names in the database. The names have NO schema name prefix. |
||
352 | * @throws NotSupportedException if this method is called |
||
353 | */ |
||
354 | protected function findTableNames($schema = '') |
||
358 | |||
359 | /** |
||
360 | * Returns all unique indexes for the given table. |
||
361 | * Each array element is of the following structure: |
||
362 | * |
||
363 | * ```php |
||
364 | * [ |
||
365 | * 'IndexName1' => ['col1' [, ...]], |
||
366 | * 'IndexName2' => ['col2' [, ...]], |
||
367 | * ] |
||
368 | * ``` |
||
369 | * |
||
370 | * This method should be overridden by child classes in order to support this feature |
||
371 | * because the default implementation simply throws an exception |
||
372 | * @param TableSchema $table the table metadata |
||
373 | * @return array all unique indexes for the given table. |
||
374 | * @throws NotSupportedException if this method is called |
||
375 | */ |
||
376 | public function findUniqueIndexes($table) |
||
380 | |||
381 | /** |
||
382 | * Returns the ID of the last inserted row or sequence value. |
||
383 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
384 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
385 | * @throws InvalidCallException if the DB connection is not active |
||
386 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
387 | */ |
||
388 | 31 | public function getLastInsertID($sequenceName = '') |
|
396 | |||
397 | /** |
||
398 | * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
399 | */ |
||
400 | 2 | public function supportsSavepoint() |
|
404 | |||
405 | /** |
||
406 | * Creates a new savepoint. |
||
407 | * @param string $name the savepoint name |
||
408 | */ |
||
409 | 2 | public function createSavepoint($name) |
|
413 | |||
414 | /** |
||
415 | * Releases an existing savepoint. |
||
416 | * @param string $name the savepoint name |
||
417 | */ |
||
418 | public function releaseSavepoint($name) |
||
422 | |||
423 | /** |
||
424 | * Rolls back to a previously created savepoint. |
||
425 | * @param string $name the savepoint name |
||
426 | */ |
||
427 | 2 | public function rollBackSavepoint($name) |
|
431 | |||
432 | /** |
||
433 | * Sets the isolation level of the current transaction. |
||
434 | * @param string $level The transaction isolation level to use for this transaction. |
||
435 | * This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
||
436 | * and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
||
437 | * after `SET TRANSACTION ISOLATION LEVEL`. |
||
438 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
439 | */ |
||
440 | 2 | public function setTransactionIsolationLevel($level) |
|
444 | |||
445 | /** |
||
446 | * Executes the INSERT command, returning primary key values. |
||
447 | * @param string $table the table that new rows will be inserted into. |
||
448 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
449 | * @return array|false primary key values or false if the command fails |
||
450 | * @since 2.0.4 |
||
451 | */ |
||
452 | 32 | public function insert($table, $columns) |
|
470 | |||
471 | /** |
||
472 | * Quotes a string value for use in a query. |
||
473 | * Note that if the parameter is not a string, it will be returned without change. |
||
474 | * @param string $str string to be quoted |
||
475 | * @return string the properly quoted string |
||
476 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
477 | */ |
||
478 | 263 | public function quoteValue($str) |
|
491 | |||
492 | /** |
||
493 | * Quotes a table name for use in a query. |
||
494 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
495 | * If the table name is already quoted or contains '(' or '{{', |
||
496 | * then this method will do nothing. |
||
497 | * @param string $name table name |
||
498 | * @return string the properly quoted table name |
||
499 | * @see quoteSimpleTableName() |
||
500 | */ |
||
501 | 449 | public function quoteTableName($name) |
|
517 | |||
518 | /** |
||
519 | * Quotes a column name for use in a query. |
||
520 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
521 | * If the column name is already quoted or contains '(', '[[' or '{{', |
||
522 | * then this method will do nothing. |
||
523 | * @param string $name column name |
||
524 | * @return string the properly quoted column name |
||
525 | * @see quoteSimpleColumnName() |
||
526 | */ |
||
527 | 552 | public function quoteColumnName($name) |
|
543 | |||
544 | /** |
||
545 | * Quotes a simple table name for use in a query. |
||
546 | * A simple table name should contain the table name only without any schema prefix. |
||
547 | * If the table name is already quoted, this method will do nothing. |
||
548 | * @param string $name table name |
||
549 | * @return string the properly quoted table name |
||
550 | */ |
||
551 | public function quoteSimpleTableName($name) |
||
555 | |||
556 | /** |
||
557 | * Quotes a simple column name for use in a query. |
||
558 | * A simple column name should contain the column name only without any prefix. |
||
559 | * If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
||
560 | * @param string $name column name |
||
561 | * @return string the properly quoted column name |
||
562 | */ |
||
563 | 261 | public function quoteSimpleColumnName($name) |
|
567 | |||
568 | /** |
||
569 | * Returns the actual name of a given table name. |
||
570 | * This method will strip off curly brackets from the given table name |
||
571 | * and replace the percentage character '%' with [[Connection::tablePrefix]]. |
||
572 | * @param string $name the table name to be converted |
||
573 | * @return string the real name of the given table name |
||
574 | */ |
||
575 | 295 | public function getRawTableName($name) |
|
585 | |||
586 | /** |
||
587 | * Extracts the PHP type from abstract DB type. |
||
588 | * @param ColumnSchema $column the column schema information |
||
589 | * @return string PHP type name |
||
590 | */ |
||
591 | 283 | protected function getColumnPhpType($column) |
|
615 | |||
616 | /** |
||
617 | * Converts a DB exception to a more concrete one if possible. |
||
618 | * |
||
619 | * @param \Exception $e |
||
620 | * @param string $rawSql SQL that produced exception |
||
621 | * @return Exception |
||
622 | */ |
||
623 | 6 | public function convertException(\Exception $e, $rawSql) |
|
639 | |||
640 | /** |
||
641 | * Returns a value indicating whether a SQL statement is for read purpose. |
||
642 | * @param string $sql the SQL statement |
||
643 | * @return bool whether a SQL statement is for read purpose. |
||
644 | */ |
||
645 | 6 | public function isReadQuery($sql) |
|
650 | } |
||
651 |