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 |
||
42 | abstract class Schema extends BaseObject |
||
43 | { |
||
44 | // The following are the supported abstract column data types. |
||
45 | const TYPE_PK = 'pk'; |
||
46 | const TYPE_UPK = 'upk'; |
||
47 | const TYPE_BIGPK = 'bigpk'; |
||
48 | const TYPE_UBIGPK = 'ubigpk'; |
||
49 | const TYPE_CHAR = 'char'; |
||
50 | const TYPE_STRING = 'string'; |
||
51 | const TYPE_TEXT = 'text'; |
||
52 | const TYPE_SMALLINT = 'smallint'; |
||
53 | const TYPE_INTEGER = 'integer'; |
||
54 | const TYPE_BIGINT = 'bigint'; |
||
55 | const TYPE_FLOAT = 'float'; |
||
56 | const TYPE_DOUBLE = 'double'; |
||
57 | const TYPE_DECIMAL = 'decimal'; |
||
58 | const TYPE_DATETIME = 'datetime'; |
||
59 | const TYPE_TIMESTAMP = 'timestamp'; |
||
60 | const TYPE_TIME = 'time'; |
||
61 | const TYPE_DATE = 'date'; |
||
62 | const TYPE_BINARY = 'binary'; |
||
63 | const TYPE_BOOLEAN = 'boolean'; |
||
64 | const TYPE_MONEY = 'money'; |
||
65 | const TYPE_JSON = 'json'; |
||
66 | /** |
||
67 | * Schema cache version, to detect incompatibilities in cached values when the |
||
68 | * data format of the cache changes. |
||
69 | */ |
||
70 | const SCHEMA_CACHE_VERSION = 1; |
||
71 | |||
72 | /** |
||
73 | * @var Connection the database connection |
||
74 | */ |
||
75 | public $db; |
||
76 | /** |
||
77 | * @var string the default schema name used for the current session. |
||
78 | */ |
||
79 | public $defaultSchema; |
||
80 | /** |
||
81 | * @var array map of DB errors and corresponding exceptions |
||
82 | * If left part is found in DB error message exception class from the right part is used. |
||
83 | */ |
||
84 | public $exceptionMap = [ |
||
85 | 'SQLSTATE[23' => 'yii\db\IntegrityException', |
||
86 | ]; |
||
87 | /** |
||
88 | * @var string column schema class |
||
89 | * @since 2.0.11 |
||
90 | */ |
||
91 | public $columnSchemaClass = 'yii\db\ColumnSchema'; |
||
92 | |||
93 | /** |
||
94 | * @var string|string[] character used to quote schema, table, etc. names. |
||
95 | * An array of 2 characters can be used in case starting and ending characters are different. |
||
96 | * @since 2.0.14 |
||
97 | */ |
||
98 | protected $tableQuoteCharacter = "'"; |
||
99 | /** |
||
100 | * @var string|string[] character used to quote column names. |
||
101 | * An array of 2 characters can be used in case starting and ending characters are different. |
||
102 | * @since 2.0.14 |
||
103 | */ |
||
104 | protected $columnQuoteCharacter = '"'; |
||
105 | |||
106 | /** |
||
107 | * @var array list of ALL schema names in the database, except system schemas |
||
108 | */ |
||
109 | private $_schemaNames; |
||
110 | /** |
||
111 | * @var array list of ALL table names in the database |
||
112 | */ |
||
113 | private $_tableNames = []; |
||
114 | /** |
||
115 | * @var array list of loaded table metadata (table name => metadata type => metadata). |
||
116 | */ |
||
117 | private $_tableMetadata = []; |
||
118 | /** |
||
119 | * @var QueryBuilder the query builder for this database |
||
120 | */ |
||
121 | private $_builder; |
||
122 | /** |
||
123 | * @var string server version as a string. |
||
124 | */ |
||
125 | private $_serverVersion; |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Resolves the table name and schema name (if any). |
||
130 | * @param string $name the table name |
||
131 | * @return TableSchema [[TableSchema]] with resolved table, schema, etc. names. |
||
132 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
133 | * @since 2.0.13 |
||
134 | */ |
||
135 | protected function resolveTableName($name) |
||
136 | { |
||
137 | throw new NotSupportedException(get_class($this) . ' does not support resolving table names.'); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Returns all schema names in the database, including the default one but not system schemas. |
||
142 | * This method should be overridden by child classes in order to support this feature |
||
143 | * because the default implementation simply throws an exception. |
||
144 | * @return array all schema names in the database, except system schemas. |
||
145 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
146 | * @since 2.0.4 |
||
147 | */ |
||
148 | protected function findSchemaNames() |
||
149 | { |
||
150 | throw new NotSupportedException(get_class($this) . ' does not support fetching all schema names.'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Returns all table names in the database. |
||
155 | * This method should be overridden by child classes in order to support this feature |
||
156 | * because the default implementation simply throws an exception. |
||
157 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
158 | * @return array all table names in the database. The names have NO schema name prefix. |
||
159 | * @throws NotSupportedException if this method is not supported by the DBMS. |
||
160 | */ |
||
161 | protected function findTableNames($schema = '') |
||
162 | { |
||
163 | throw new NotSupportedException(get_class($this) . ' does not support fetching all table names.'); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Loads the metadata for the specified table. |
||
168 | * @param string $name table name |
||
169 | * @return TableSchema|null DBMS-dependent table metadata, `null` if the table does not exist. |
||
170 | */ |
||
171 | abstract protected function loadTableSchema($name); |
||
172 | |||
173 | /** |
||
174 | * Creates a column schema for the database. |
||
175 | * This method may be overridden by child classes to create a DBMS-specific column schema. |
||
176 | * @return ColumnSchema column schema instance. |
||
177 | * @throws InvalidConfigException if a column schema class cannot be created. |
||
178 | */ |
||
179 | 874 | protected function createColumnSchema() |
|
180 | { |
||
181 | 874 | return Yii::createObject($this->columnSchemaClass); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Obtains the metadata for the named table. |
||
186 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
187 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
188 | * @return TableSchema|null table metadata. `null` if the named table does not exist. |
||
189 | */ |
||
190 | 954 | public function getTableSchema($name, $refresh = false) |
|
191 | { |
||
192 | 954 | return $this->getTableMetadata($name, 'schema', $refresh); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Returns the metadata for all tables in the database. |
||
197 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
198 | * @param bool $refresh whether to fetch the latest available table schemas. If this is `false`, |
||
199 | * cached data may be returned if available. |
||
200 | * @return TableSchema[] the metadata for all tables in the database. |
||
201 | * Each array element is an instance of [[TableSchema]] or its child class. |
||
202 | */ |
||
203 | 11 | public function getTableSchemas($schema = '', $refresh = false) |
|
204 | { |
||
205 | 11 | return $this->getSchemaMetadata($schema, 'schema', $refresh); |
|
206 | } |
||
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 | 2 | public function getSchemaNames($refresh = false) |
|
216 | { |
||
217 | 2 | if ($this->_schemaNames === null || $refresh) { |
|
218 | 2 | $this->_schemaNames = $this->findSchemaNames(); |
|
219 | } |
||
220 | |||
221 | 2 | return $this->_schemaNames; |
|
222 | } |
||
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 | 17 | public function getTableNames($schema = '', $refresh = false) |
|
233 | { |
||
234 | 17 | if (!isset($this->_tableNames[$schema]) || $refresh) { |
|
235 | 17 | $this->_tableNames[$schema] = $this->findTableNames($schema); |
|
236 | } |
||
237 | |||
238 | 17 | return $this->_tableNames[$schema]; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return QueryBuilder the query builder for this connection. |
||
243 | */ |
||
244 | 938 | public function getQueryBuilder() |
|
245 | { |
||
246 | 938 | if ($this->_builder === null) { |
|
247 | 884 | $this->_builder = $this->createQueryBuilder(); |
|
248 | } |
||
249 | |||
250 | 938 | return $this->_builder; |
|
251 | } |
||
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 | 988 | 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 | 43 | public function refresh() |
|
280 | { |
||
281 | /* @var $cache CacheInterface */ |
||
282 | 43 | $cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache; |
|
283 | 43 | if ($this->db->enableSchemaCache && $cache instanceof CacheInterface) { |
|
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 | 161 | public function refreshTableSchema($name) |
|
308 | |||
309 | /** |
||
310 | * Creates a query builder for the database. |
||
311 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
312 | * @return QueryBuilder query builder instance |
||
313 | */ |
||
314 | public function createQueryBuilder() |
||
318 | |||
319 | /** |
||
320 | * Create a column schema builder instance giving the type and value precision. |
||
321 | * |
||
322 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
323 | * |
||
324 | * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
||
325 | * @param int|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
||
326 | * @return ColumnSchemaBuilder column schema builder instance |
||
327 | * @since 2.0.6 |
||
328 | */ |
||
329 | 10 | public function createColumnSchemaBuilder($type, $length = null) |
|
333 | |||
334 | /** |
||
335 | * Returns all unique indexes for the given table. |
||
336 | * |
||
337 | * Each array element is of the following structure: |
||
338 | * |
||
339 | * ```php |
||
340 | * [ |
||
341 | * 'IndexName1' => ['col1' [, ...]], |
||
342 | * 'IndexName2' => ['col2' [, ...]], |
||
343 | * ] |
||
344 | * ``` |
||
345 | * |
||
346 | * This method should be overridden by child classes in order to support this feature |
||
347 | * because the default implementation simply throws an exception |
||
348 | * @param TableSchema $table the table metadata |
||
349 | * @return array all unique indexes for the given table. |
||
350 | * @throws NotSupportedException if this method is called |
||
351 | */ |
||
352 | public function findUniqueIndexes($table) |
||
356 | |||
357 | /** |
||
358 | * Returns the ID of the last inserted row or sequence value. |
||
359 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
360 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
361 | * @throws InvalidCallException if the DB connection is not active |
||
362 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
363 | */ |
||
364 | 73 | public function getLastInsertID($sequenceName = '') |
|
372 | |||
373 | /** |
||
374 | * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
375 | */ |
||
376 | 4 | public function supportsSavepoint() |
|
380 | |||
381 | /** |
||
382 | * Creates a new savepoint. |
||
383 | * @param string $name the savepoint name |
||
384 | */ |
||
385 | 4 | public function createSavepoint($name) |
|
389 | |||
390 | /** |
||
391 | * Releases an existing savepoint. |
||
392 | * @param string $name the savepoint name |
||
393 | */ |
||
394 | public function releaseSavepoint($name) |
||
398 | |||
399 | /** |
||
400 | * Rolls back to a previously created savepoint. |
||
401 | * @param string $name the savepoint name |
||
402 | */ |
||
403 | 4 | public function rollBackSavepoint($name) |
|
407 | |||
408 | /** |
||
409 | * Sets the isolation level of the current transaction. |
||
410 | * @param string $level The transaction isolation level to use for this transaction. |
||
411 | * This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
||
412 | * and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
||
413 | * after `SET TRANSACTION ISOLATION LEVEL`. |
||
414 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
415 | */ |
||
416 | 6 | public function setTransactionIsolationLevel($level) |
|
420 | |||
421 | /** |
||
422 | * Executes the INSERT command, returning primary key values. |
||
423 | * @param string $table the table that new rows will be inserted into. |
||
424 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
425 | * @return array|false primary key values or false if the command fails |
||
426 | * @since 2.0.4 |
||
427 | */ |
||
428 | 77 | public function insert($table, $columns) |
|
447 | |||
448 | /** |
||
449 | * Quotes a string value for use in a query. |
||
450 | * Note that if the parameter is not a string, it will be returned without change. |
||
451 | * @param string $str string to be quoted |
||
452 | * @return string the properly quoted string |
||
453 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
454 | */ |
||
455 | 980 | public function quoteValue($str) |
|
468 | |||
469 | /** |
||
470 | * Quotes a table name for use in a query. |
||
471 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
472 | * If the table name is already quoted or contains '(' or '{{', |
||
473 | * then this method will do nothing. |
||
474 | * @param string $name table name |
||
475 | * @return string the properly quoted table name |
||
476 | * @see quoteSimpleTableName() |
||
477 | */ |
||
478 | 1179 | public function quoteTableName($name) |
|
493 | |||
494 | /** |
||
495 | * Quotes a column name for use in a query. |
||
496 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
497 | * If the column name is already quoted or contains '(', '[[' or '{{', |
||
498 | * then this method will do nothing. |
||
499 | * @param string $name column name |
||
500 | * @return string the properly quoted column name |
||
501 | * @see quoteSimpleColumnName() |
||
502 | */ |
||
503 | 1255 | public function quoteColumnName($name) |
|
520 | |||
521 | /** |
||
522 | * Quotes a simple table name for use in a query. |
||
523 | * A simple table name should contain the table name only without any schema prefix. |
||
524 | * If the table name is already quoted, this method will do nothing. |
||
525 | * @param string $name table name |
||
526 | * @return string the properly quoted table name |
||
527 | */ |
||
528 | 1203 | public function quoteSimpleTableName($name) |
|
537 | |||
538 | /** |
||
539 | * Quotes a simple column name for use in a query. |
||
540 | * A simple column name should contain the column name only without any prefix. |
||
541 | * If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
||
542 | * @param string $name column name |
||
543 | * @return string the properly quoted column name |
||
544 | */ |
||
545 | 1246 | public function quoteSimpleColumnName($name) |
|
554 | |||
555 | /** |
||
556 | * Unquotes a simple table name. |
||
557 | * A simple table name should contain the table name only without any schema prefix. |
||
558 | * If the table name is not quoted, this method will do nothing. |
||
559 | * @param string $name table name. |
||
560 | * @return string unquoted table name. |
||
561 | * @since 2.0.14 |
||
562 | */ |
||
563 | public function unquoteSimpleTableName($name) |
||
572 | |||
573 | /** |
||
574 | * Unquotes a simple column name. |
||
575 | * A simple column name should contain the column name only without any prefix. |
||
576 | * If the column name is not quoted or is the asterisk character '*', this method will do nothing. |
||
577 | * @param string $name column name. |
||
578 | * @return string unquoted column name. |
||
579 | * @since 2.0.14 |
||
580 | */ |
||
581 | public function unquoteSimpleColumnName($name) |
||
590 | |||
591 | /** |
||
592 | * Returns the actual name of a given table name. |
||
593 | * This method will strip off curly brackets from the given table name |
||
594 | * and replace the percentage character '%' with [[Connection::tablePrefix]]. |
||
595 | * @param string $name the table name to be converted |
||
596 | * @return string the real name of the given table name |
||
597 | */ |
||
598 | 1136 | public function getRawTableName($name) |
|
608 | |||
609 | /** |
||
610 | * Extracts the PHP type from abstract DB type. |
||
611 | * @param ColumnSchema $column the column schema information |
||
612 | * @return string PHP type name |
||
613 | */ |
||
614 | 874 | protected function getColumnPhpType($column) |
|
639 | |||
640 | /** |
||
641 | * Converts a DB exception to a more concrete one if possible. |
||
642 | * |
||
643 | * @param \Exception $e |
||
644 | * @param string $rawSql SQL that produced exception |
||
645 | * @return Exception |
||
646 | */ |
||
647 | 37 | public function convertException(\Exception $e, $rawSql) |
|
663 | |||
664 | /** |
||
665 | * Returns a value indicating whether a SQL statement is for read purpose. |
||
666 | * @param string $sql the SQL statement |
||
667 | * @return bool whether a SQL statement is for read purpose. |
||
668 | */ |
||
669 | 9 | public function isReadQuery($sql) |
|
674 | |||
675 | /** |
||
676 | * Returns a server version as a string comparable by [[\version_compare()]]. |
||
677 | * @return string server version as a string. |
||
678 | * @since 2.0.14 |
||
679 | */ |
||
680 | 48 | public function getServerVersion() |
|
687 | |||
688 | /** |
||
689 | * Returns the cache key for the specified table name. |
||
690 | * @param string $name the table name. |
||
691 | * @return mixed the cache key. |
||
692 | */ |
||
693 | 25 | protected function getCacheKey($name) |
|
702 | |||
703 | /** |
||
704 | * Returns the cache tag name. |
||
705 | * This allows [[refresh()]] to invalidate all cached table schemas. |
||
706 | * @return string the cache tag name |
||
707 | */ |
||
708 | 25 | protected function getCacheTag() |
|
716 | |||
717 | /** |
||
718 | * Returns the metadata of the given type for the given table. |
||
719 | * If there's no metadata in the cache, this method will call |
||
720 | * a `'loadTable' . ucfirst($type)` named method with the table name to obtain the metadata. |
||
721 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
722 | * @param string $type metadata type. |
||
723 | * @param bool $refresh whether to reload the table metadata even if it is found in the cache. |
||
724 | * @return mixed metadata. |
||
725 | * @since 2.0.13 |
||
726 | */ |
||
727 | 1134 | protected function getTableMetadata($name, $type, $refresh) |
|
747 | |||
748 | /** |
||
749 | * Returns the metadata of the given type for all tables in the given schema. |
||
750 | * This method will call a `'getTable' . ucfirst($type)` named method with the table name |
||
751 | * and the refresh flag to obtain the metadata. |
||
752 | * @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema name. |
||
753 | * @param string $type metadata type. |
||
754 | * @param bool $refresh whether to fetch the latest available table metadata. If this is `false`, |
||
755 | * cached data may be returned if available. |
||
756 | * @return array array of metadata. |
||
757 | * @since 2.0.13 |
||
758 | */ |
||
759 | 11 | protected function getSchemaMetadata($schema, $type, $refresh) |
|
775 | |||
776 | /** |
||
777 | * Sets the metadata of the given type for the given table. |
||
778 | * @param string $name table name. |
||
779 | * @param string $type metadata type. |
||
780 | * @param mixed $data metadata. |
||
781 | * @since 2.0.13 |
||
782 | */ |
||
783 | 173 | protected function setTableMetadata($name, $type, $data) |
|
787 | |||
788 | /** |
||
789 | * Changes row's array key case to lower if PDO's one is set to uppercase. |
||
790 | * @param array $row row's array or an array of row's arrays. |
||
791 | * @param bool $multiple whether multiple rows or a single row passed. |
||
792 | * @return array normalized row or rows. |
||
793 | * @since 2.0.13 |
||
794 | */ |
||
795 | 196 | protected function normalizePdoRowKeyCase(array $row, $multiple) |
|
809 | |||
810 | /** |
||
811 | * Tries to load and populate table metadata from cache. |
||
812 | * @param Cache|null $cache |
||
813 | * @param string $name |
||
814 | */ |
||
815 | 1090 | private function loadTableMetadataFromCache($cache, $name) |
|
831 | |||
832 | /** |
||
833 | * Saves table metadata to cache. |
||
834 | * @param Cache|null $cache |
||
835 | * @param string $name |
||
836 | */ |
||
837 | 1042 | private function saveTableMetadataToCache($cache, $name) |
|
852 | } |
||
853 |