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 |
||
43 | abstract class Schema extends BaseObject |
||
44 | { |
||
45 | // The following are the supported abstract column data types. |
||
46 | const TYPE_PK = 'pk'; |
||
47 | const TYPE_UPK = 'upk'; |
||
48 | const TYPE_BIGPK = 'bigpk'; |
||
49 | const TYPE_UBIGPK = 'ubigpk'; |
||
50 | const TYPE_CHAR = 'char'; |
||
51 | const TYPE_STRING = 'string'; |
||
52 | const TYPE_TEXT = 'text'; |
||
53 | const TYPE_TINYINT = 'tinyint'; |
||
54 | const TYPE_SMALLINT = 'smallint'; |
||
55 | const TYPE_INTEGER = 'integer'; |
||
56 | const TYPE_BIGINT = 'bigint'; |
||
57 | const TYPE_FLOAT = 'float'; |
||
58 | const TYPE_DOUBLE = 'double'; |
||
59 | const TYPE_DECIMAL = 'decimal'; |
||
60 | const TYPE_DATETIME = 'datetime'; |
||
61 | const TYPE_TIMESTAMP = 'timestamp'; |
||
62 | const TYPE_TIME = 'time'; |
||
63 | const TYPE_DATE = 'date'; |
||
64 | const TYPE_BINARY = 'binary'; |
||
65 | const TYPE_BOOLEAN = 'boolean'; |
||
66 | const TYPE_MONEY = 'money'; |
||
67 | const TYPE_JSON = 'json'; |
||
68 | /** |
||
69 | * Schema cache version, to detect incompatibilities in cached values when the |
||
70 | * data format of the cache changes. |
||
71 | */ |
||
72 | const SCHEMA_CACHE_VERSION = 1; |
||
73 | |||
74 | /** |
||
75 | * @var Connection the database connection |
||
76 | */ |
||
77 | public $db; |
||
78 | /** |
||
79 | * @var string the default schema name used for the current session. |
||
80 | */ |
||
81 | public $defaultSchema; |
||
82 | /** |
||
83 | * @var array map of DB errors and corresponding exceptions |
||
84 | * If left part is found in DB error message exception class from the right part is used. |
||
85 | */ |
||
86 | public $exceptionMap = [ |
||
87 | 'SQLSTATE[23' => IntegrityException::class, |
||
88 | ]; |
||
89 | /** |
||
90 | * @var string|array column schema class or class config |
||
91 | * @since 2.0.11 |
||
92 | */ |
||
93 | public $columnSchemaClass = ColumnSchema::class; |
||
94 | |||
95 | /** |
||
96 | * @var string|string[] character used to quote schema, table, etc. names. |
||
97 | * An array of 2 characters can be used in case starting and ending characters are different. |
||
98 | * @since 2.0.14 |
||
99 | */ |
||
100 | protected $tableQuoteCharacter = "'"; |
||
101 | /** |
||
102 | * @var string|string[] character used to quote column names. |
||
103 | * An array of 2 characters can be used in case starting and ending characters are different. |
||
104 | * @since 2.0.14 |
||
105 | */ |
||
106 | protected $columnQuoteCharacter = '"'; |
||
107 | |||
108 | /** |
||
109 | * @var array list of ALL schema names in the database, except system schemas |
||
110 | */ |
||
111 | private $_schemaNames; |
||
112 | /** |
||
113 | * @var array list of ALL table names in the database |
||
114 | */ |
||
115 | private $_tableNames = []; |
||
116 | /** |
||
117 | * @var array list of loaded table metadata (table name => metadata type => metadata). |
||
118 | */ |
||
119 | private $_tableMetadata = []; |
||
120 | /** |
||
121 | * @var QueryBuilder the query builder for this database |
||
122 | */ |
||
123 | private $_builder; |
||
124 | /** |
||
125 | * @var string server version as a string. |
||
126 | */ |
||
127 | private $_serverVersion; |
||
128 | |||
129 | |||
130 | 3 | public function __construct(Connection $db) |
|
134 | |||
135 | /** |
||
136 | * Resolves the table name and schema name (if any). |
||
137 | * @param string $name the table name |
||
138 | * @return TableSchema [[TableSchema]] with resolved table, schema, etc. names. |
||
139 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
140 | * @since 2.0.13 |
||
141 | */ |
||
142 | protected function resolveTableName($name) |
||
146 | |||
147 | /** |
||
148 | * Returns all schema names in the database, including the default one but not system schemas. |
||
149 | * This method should be overridden by child classes in order to support this feature |
||
150 | * because the default implementation simply throws an exception. |
||
151 | * @return array all schema names in the database, except system schemas. |
||
152 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
153 | * @since 2.0.4 |
||
154 | */ |
||
155 | protected function findSchemaNames() |
||
159 | |||
160 | /** |
||
161 | * Returns all table names in the database. |
||
162 | * This method should be overridden by child classes in order to support this feature |
||
163 | * because the default implementation simply throws an exception. |
||
164 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
165 | * @return array all table names in the database. The names have NO schema name prefix. |
||
166 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
167 | */ |
||
168 | protected function findTableNames($schema = '') |
||
172 | |||
173 | /** |
||
174 | * Loads the metadata for the specified table. |
||
175 | * @param string $name table name |
||
176 | * @return TableSchema|null DBMS-dependent table metadata, `null` if the table does not exist. |
||
177 | */ |
||
178 | abstract protected function loadTableSchema($name); |
||
179 | |||
180 | /** |
||
181 | * Creates a column schema for the database. |
||
182 | * This method may be overridden by child classes to create a DBMS-specific column schema. |
||
183 | * @return ColumnSchema column schema instance. |
||
184 | * @throws InvalidConfigException if a column schema class cannot be created. |
||
185 | */ |
||
186 | protected function createColumnSchema() |
||
190 | |||
191 | /** |
||
192 | * Obtains the metadata for the named table. |
||
193 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
194 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
195 | * @return TableSchema|null table metadata. `null` if the named table does not exist. |
||
196 | */ |
||
197 | public function getTableSchema($name, $refresh = false) |
||
201 | |||
202 | /** |
||
203 | * Returns the metadata for all tables in the database. |
||
204 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
205 | * @param bool $refresh whether to fetch the latest available table schemas. If this is `false`, |
||
206 | * cached data may be returned if available. |
||
207 | * @return TableSchema[] the metadata for all tables in the database. |
||
208 | * Each array element is an instance of [[TableSchema]] or its child class. |
||
209 | */ |
||
210 | public function getTableSchemas($schema = '', $refresh = false) |
||
214 | |||
215 | /** |
||
216 | * Returns all schema names in the database, except system schemas. |
||
217 | * @param bool $refresh whether to fetch the latest available schema names. If this is false, |
||
218 | * schema names fetched previously (if available) will be returned. |
||
219 | * @return string[] all schema names in the database, except system schemas. |
||
220 | * @since 2.0.4 |
||
221 | */ |
||
222 | public function getSchemaNames($refresh = false) |
||
230 | |||
231 | /** |
||
232 | * Returns all table names in the database. |
||
233 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
234 | * If not empty, the returned table names will be prefixed with the schema name. |
||
235 | * @param bool $refresh whether to fetch the latest available table names. If this is false, |
||
236 | * table names fetched previously (if available) will be returned. |
||
237 | * @return string[] all table names in the database. |
||
238 | */ |
||
239 | public function getTableNames($schema = '', $refresh = false) |
||
247 | |||
248 | /** |
||
249 | * @return QueryBuilder the query builder for this connection. |
||
250 | */ |
||
251 | 3 | public function getQueryBuilder() |
|
259 | |||
260 | /** |
||
261 | * Determines the PDO type for the given PHP data value. |
||
262 | * @param mixed $data the data whose PDO type is to be determined |
||
263 | * @return int the PDO type |
||
264 | * @see http://www.php.net/manual/en/pdo.constants.php |
||
265 | */ |
||
266 | 1 | public function getPdoType($data) |
|
280 | |||
281 | /** |
||
282 | * Refreshes the schema. |
||
283 | * This method cleans up all cached table schemas so that they can be re-created later |
||
284 | * to reflect the database schema change. |
||
285 | */ |
||
286 | public function refresh() |
||
296 | |||
297 | /** |
||
298 | * Refreshes the particular table schema. |
||
299 | * This method cleans up cached table schema so that it can be re-created later |
||
300 | * to reflect the database schema change. |
||
301 | * @param string $name table name. |
||
302 | * @since 2.0.6 |
||
303 | */ |
||
304 | public function refreshTableSchema($name) |
||
315 | |||
316 | /** |
||
317 | * Creates a query builder for the database. |
||
318 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
319 | * @return QueryBuilder query builder instance |
||
320 | */ |
||
321 | public function createQueryBuilder() |
||
325 | |||
326 | /** |
||
327 | * Create a column schema builder instance giving the type and value precision. |
||
328 | * |
||
329 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
330 | * |
||
331 | * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
||
332 | * @param int|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
||
333 | * @return ColumnSchemaBuilder column schema builder instance |
||
334 | * @since 2.0.6 |
||
335 | */ |
||
336 | public function createColumnSchemaBuilder($type, $length = null) |
||
340 | |||
341 | /** |
||
342 | * Returns all unique indexes for the given table. |
||
343 | * |
||
344 | * Each array element is of the following structure: |
||
345 | * |
||
346 | * ```php |
||
347 | * [ |
||
348 | * 'IndexName1' => ['col1' [, ...]], |
||
349 | * 'IndexName2' => ['col2' [, ...]], |
||
350 | * ] |
||
351 | * ``` |
||
352 | * |
||
353 | * This method should be overridden by child classes in order to support this feature |
||
354 | * because the default implementation simply throws an exception |
||
355 | * @param TableSchema $table the table metadata |
||
356 | * @return array all unique indexes for the given table. |
||
357 | * @throws NotSupportedException if this method is called |
||
358 | */ |
||
359 | public function findUniqueIndexes($table) |
||
363 | |||
364 | /** |
||
365 | * Returns the ID of the last inserted row or sequence value. |
||
366 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
367 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
368 | * @throws InvalidCallException if the DB connection is not active |
||
369 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
370 | */ |
||
371 | public function getLastInsertID($sequenceName = '') |
||
379 | |||
380 | /** |
||
381 | * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
382 | */ |
||
383 | public function supportsSavepoint() |
||
387 | |||
388 | /** |
||
389 | * Creates a new savepoint. |
||
390 | * @param string $name the savepoint name |
||
391 | */ |
||
392 | public function createSavepoint($name) |
||
396 | |||
397 | /** |
||
398 | * Releases an existing savepoint. |
||
399 | * @param string $name the savepoint name |
||
400 | */ |
||
401 | public function releaseSavepoint($name) |
||
405 | |||
406 | /** |
||
407 | * Rolls back to a previously created savepoint. |
||
408 | * @param string $name the savepoint name |
||
409 | */ |
||
410 | public function rollBackSavepoint($name) |
||
414 | |||
415 | /** |
||
416 | * Sets the isolation level of the current transaction. |
||
417 | * @param string $level The transaction isolation level to use for this transaction. |
||
418 | * This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
||
419 | * and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
||
420 | * after `SET TRANSACTION ISOLATION LEVEL`. |
||
421 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
422 | */ |
||
423 | public function setTransactionIsolationLevel($level) |
||
427 | |||
428 | /** |
||
429 | * Executes the INSERT command, returning primary key values. |
||
430 | * @param string $table the table that new rows will be inserted into. |
||
431 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
432 | * @return array|false primary key values or false if the command fails |
||
433 | * @since 2.0.4 |
||
434 | */ |
||
435 | public function insert($table, $columns) |
||
454 | |||
455 | /** |
||
456 | * Quotes a string value for use in a query. |
||
457 | * Note that if the parameter is not a string, it will be returned without change. |
||
458 | * @param string $str string to be quoted |
||
459 | * @return string the properly quoted string |
||
460 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
461 | */ |
||
462 | public function quoteValue($str) |
||
475 | |||
476 | /** |
||
477 | * Quotes a table name for use in a query. |
||
478 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
479 | * If the table name is already quoted or contains '(' or '{{', |
||
480 | * then this method will do nothing. |
||
481 | * @param string $name table name |
||
482 | * @return string the properly quoted table name |
||
483 | * @see quoteSimpleTableName() |
||
484 | */ |
||
485 | 3 | public function quoteTableName($name) |
|
500 | |||
501 | /** |
||
502 | * Quotes a column name for use in a query. |
||
503 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
504 | * If the column name is already quoted or contains '(', '[[' or '{{', |
||
505 | * then this method will do nothing. |
||
506 | * @param string $name column name |
||
507 | * @return string the properly quoted column name |
||
508 | * @see quoteSimpleColumnName() |
||
509 | */ |
||
510 | public function quoteColumnName($name) |
||
527 | |||
528 | /** |
||
529 | * Quotes a simple table name for use in a query. |
||
530 | * A simple table name should contain the table name only without any schema prefix. |
||
531 | * If the table name is already quoted, this method will do nothing. |
||
532 | * @param string $name table name |
||
533 | * @return string the properly quoted table name |
||
534 | */ |
||
535 | 3 | public function quoteSimpleTableName($name) |
|
544 | |||
545 | /** |
||
546 | * Quotes a simple column name for use in a query. |
||
547 | * A simple column name should contain the column name only without any prefix. |
||
548 | * If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
||
549 | * @param string $name column name |
||
550 | * @return string the properly quoted column name |
||
551 | */ |
||
552 | public function quoteSimpleColumnName($name) |
||
561 | |||
562 | /** |
||
563 | * Unquotes a simple table name. |
||
564 | * A simple table name should contain the table name only without any schema prefix. |
||
565 | * If the table name is not quoted, this method will do nothing. |
||
566 | * @param string $name table name. |
||
567 | * @return string unquoted table name. |
||
568 | * @since 2.0.14 |
||
569 | */ |
||
570 | public function unquoteSimpleTableName($name) |
||
579 | |||
580 | /** |
||
581 | * Unquotes a simple column name. |
||
582 | * A simple column name should contain the column name only without any prefix. |
||
583 | * If the column name is not quoted or is the asterisk character '*', this method will do nothing. |
||
584 | * @param string $name column name. |
||
585 | * @return string unquoted column name. |
||
586 | * @since 2.0.14 |
||
587 | */ |
||
588 | public function unquoteSimpleColumnName($name) |
||
597 | |||
598 | /** |
||
599 | * Returns the actual name of a given table name. |
||
600 | * This method will strip off curly brackets from the given table name |
||
601 | * and replace the percentage character '%' with [[Connection::tablePrefix]]. |
||
602 | * @param string $name the table name to be converted |
||
603 | * @return string the real name of the given table name |
||
604 | */ |
||
605 | public function getRawTableName($name) |
||
615 | |||
616 | /** |
||
617 | * Extracts the PHP type from abstract DB type. |
||
618 | * @param ColumnSchema $column the column schema information |
||
619 | * @return string PHP type name |
||
620 | */ |
||
621 | protected function getColumnPhpType($column) |
||
647 | |||
648 | /** |
||
649 | * Converts a DB exception to a more concrete one if possible. |
||
650 | * |
||
651 | * @param \Exception $e |
||
652 | * @param string $rawSql SQL that produced exception |
||
653 | * @return Exception |
||
654 | */ |
||
655 | public function convertException(\Exception $e, $rawSql) |
||
671 | |||
672 | /** |
||
673 | * Returns a value indicating whether a SQL statement is for read purpose. |
||
674 | * @param string $sql the SQL statement |
||
675 | * @return bool whether a SQL statement is for read purpose. |
||
676 | */ |
||
677 | public function isReadQuery($sql) |
||
682 | |||
683 | /** |
||
684 | * Returns a server version as a string comparable by [[\version_compare()]]. |
||
685 | * @return string server version as a string. |
||
686 | * @since 2.0.14 |
||
687 | */ |
||
688 | public function getServerVersion() |
||
695 | |||
696 | /** |
||
697 | * Returns the cache key for the specified table name. |
||
698 | * @param string $name the table name. |
||
699 | * @return mixed the cache key. |
||
700 | */ |
||
701 | protected function getCacheKey($name) |
||
710 | |||
711 | /** |
||
712 | * Returns the cache tag name. |
||
713 | * This allows [[refresh()]] to invalidate all cached table schemas. |
||
714 | * @return string the cache tag name |
||
715 | */ |
||
716 | protected function getCacheTag() |
||
724 | |||
725 | /** |
||
726 | * Returns the metadata of the given type for the given table. |
||
727 | * If there's no metadata in the cache, this method will call |
||
728 | * a `'loadTable' . ucfirst($type)` named method with the table name to obtain the metadata. |
||
729 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
730 | * @param string $type metadata type. |
||
731 | * @param bool $refresh whether to reload the table metadata even if it is found in the cache. |
||
732 | * @return mixed metadata. |
||
733 | * @since 2.0.13 |
||
734 | */ |
||
735 | protected function getTableMetadata($name, $type, $refresh) |
||
755 | |||
756 | /** |
||
757 | * Returns the metadata of the given type for all tables in the given schema. |
||
758 | * This method will call a `'getTable' . ucfirst($type)` named method with the table name |
||
759 | * and the refresh flag to obtain the metadata. |
||
760 | * @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema name. |
||
761 | * @param string $type metadata type. |
||
762 | * @param bool $refresh whether to fetch the latest available table metadata. If this is `false`, |
||
763 | * cached data may be returned if available. |
||
764 | * @return array array of metadata. |
||
765 | * @since 2.0.13 |
||
766 | */ |
||
767 | protected function getSchemaMetadata($schema, $type, $refresh) |
||
783 | |||
784 | /** |
||
785 | * Sets the metadata of the given type for the given table. |
||
786 | * @param string $name table name. |
||
787 | * @param string $type metadata type. |
||
788 | * @param mixed $data metadata. |
||
789 | * @since 2.0.13 |
||
790 | */ |
||
791 | protected function setTableMetadata($name, $type, $data) |
||
795 | |||
796 | /** |
||
797 | * Changes row's array key case to lower if PDO's one is set to uppercase. |
||
798 | * @param array $row row's array or an array of row's arrays. |
||
799 | * @param bool $multiple whether multiple rows or a single row passed. |
||
800 | * @return array normalized row or rows. |
||
801 | * @since 2.0.13 |
||
802 | */ |
||
803 | protected function normalizePdoRowKeyCase(array $row, $multiple) |
||
817 | |||
818 | /** |
||
819 | * Tries to load and populate table metadata from cache. |
||
820 | * @param Cache|null $cache |
||
821 | * @param string $name |
||
822 | */ |
||
823 | private function loadTableMetadataFromCache($cache, $name) |
||
839 | |||
840 | /** |
||
841 | * Saves table metadata to cache. |
||
842 | * @param Cache|null $cache |
||
843 | * @param string $name |
||
844 | */ |
||
845 | private function saveTableMetadataToCache($cache, $name) |
||
860 | } |
||
861 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.