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 | /** |
||
41 | * The following are the supported abstract column data types. |
||
42 | */ |
||
43 | const TYPE_PK = 'pk'; |
||
44 | const TYPE_UPK = 'upk'; |
||
45 | const TYPE_BIGPK = 'bigpk'; |
||
46 | const TYPE_UBIGPK = 'ubigpk'; |
||
47 | const TYPE_CHAR = 'char'; |
||
48 | const TYPE_STRING = 'string'; |
||
49 | const TYPE_TEXT = 'text'; |
||
50 | const TYPE_SMALLINT = 'smallint'; |
||
51 | const TYPE_INTEGER = 'integer'; |
||
52 | const TYPE_BIGINT = 'bigint'; |
||
53 | const TYPE_FLOAT = 'float'; |
||
54 | const TYPE_DOUBLE = 'double'; |
||
55 | const TYPE_DECIMAL = 'decimal'; |
||
56 | const TYPE_DATETIME = 'datetime'; |
||
57 | const TYPE_TIMESTAMP = 'timestamp'; |
||
58 | const TYPE_TIME = 'time'; |
||
59 | const TYPE_DATE = 'date'; |
||
60 | const TYPE_BINARY = 'binary'; |
||
61 | const TYPE_BOOLEAN = 'boolean'; |
||
62 | const TYPE_MONEY = 'money'; |
||
63 | |||
64 | /** |
||
65 | * @var Connection the database connection |
||
66 | */ |
||
67 | public $db; |
||
68 | /** |
||
69 | * @var string the default schema name used for the current session. |
||
70 | */ |
||
71 | public $defaultSchema; |
||
72 | /** |
||
73 | * @var array map of DB errors and corresponding exceptions |
||
74 | * If left part is found in DB error message exception class from the right part is used. |
||
75 | */ |
||
76 | public $exceptionMap = [ |
||
77 | 'SQLSTATE[23' => 'yii\db\IntegrityException', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * @var array list of ALL schema names in the database, except system schemas |
||
82 | */ |
||
83 | private $_schemaNames; |
||
84 | /** |
||
85 | * @var array list of ALL table names in the database |
||
86 | */ |
||
87 | private $_tableNames = []; |
||
88 | /** |
||
89 | * @var array list of loaded table metadata (table name => TableSchema) |
||
90 | */ |
||
91 | private $_tables = []; |
||
92 | /** |
||
93 | * @var QueryBuilder the query builder for this database |
||
94 | */ |
||
95 | private $_builder; |
||
96 | |||
97 | |||
98 | /** |
||
99 | * @return \yii\db\ColumnSchema |
||
100 | * @throws \yii\base\InvalidConfigException |
||
101 | */ |
||
102 | 349 | protected function createColumnSchema() |
|
106 | |||
107 | /** |
||
108 | * Loads the metadata for the specified table. |
||
109 | * @param string $name table name |
||
110 | * @return null|TableSchema DBMS-dependent table metadata, null if the table does not exist. |
||
111 | */ |
||
112 | abstract protected function loadTableSchema($name); |
||
113 | |||
114 | /** |
||
115 | * Obtains the metadata for the named table. |
||
116 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
117 | * @param boolean $refresh whether to reload the table schema even if it is found in the cache. |
||
118 | * @return null|TableSchema table metadata. Null if the named table does not exist. |
||
119 | */ |
||
120 | 423 | public function getTableSchema($name, $refresh = false) |
|
151 | |||
152 | /** |
||
153 | * Returns the cache key for the specified table name. |
||
154 | * @param string $name the table name |
||
155 | * @return mixed the cache key |
||
156 | */ |
||
157 | 7 | protected function getCacheKey($name) |
|
166 | |||
167 | /** |
||
168 | * Returns the cache tag name. |
||
169 | * This allows [[refresh()]] to invalidate all cached table schemas. |
||
170 | * @return string the cache tag name |
||
171 | */ |
||
172 | 7 | protected function getCacheTag() |
|
180 | |||
181 | /** |
||
182 | * Returns the metadata for all tables in the database. |
||
183 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
184 | * @param boolean $refresh whether to fetch the latest available table schemas. If this is false, |
||
185 | * cached data may be returned if available. |
||
186 | * @return TableSchema[] the metadata for all tables in the database. |
||
187 | * Each array element is an instance of [[TableSchema]] or its child class. |
||
188 | */ |
||
189 | 10 | public function getTableSchemas($schema = '', $refresh = false) |
|
203 | |||
204 | /** |
||
205 | * Returns all schema names in the database, except system schemas. |
||
206 | * @param boolean $refresh whether to fetch the latest available schema names. If this is false, |
||
207 | * schema names fetched previously (if available) will be returned. |
||
208 | * @return string[] all schema names in the database, except system schemas. |
||
209 | * @since 2.0.4 |
||
210 | */ |
||
211 | 1 | public function getSchemaNames($refresh = false) |
|
219 | |||
220 | /** |
||
221 | * Returns all table names in the database. |
||
222 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
223 | * If not empty, the returned table names will be prefixed with the schema name. |
||
224 | * @param boolean $refresh whether to fetch the latest available table names. If this is false, |
||
225 | * table names fetched previously (if available) will be returned. |
||
226 | * @return string[] all table names in the database. |
||
227 | */ |
||
228 | 16 | public function getTableNames($schema = '', $refresh = false) |
|
236 | |||
237 | /** |
||
238 | * @return QueryBuilder the query builder for this connection. |
||
239 | */ |
||
240 | 464 | public function getQueryBuilder() |
|
248 | |||
249 | /** |
||
250 | * Determines the PDO type for the given PHP data value. |
||
251 | * @param mixed $data the data whose PDO type is to be determined |
||
252 | * @return integer the PDO type |
||
253 | * @see http://www.php.net/manual/en/pdo.constants.php |
||
254 | */ |
||
255 | 452 | public function getPdoType($data) |
|
269 | |||
270 | /** |
||
271 | * Refreshes the schema. |
||
272 | * This method cleans up all cached table schemas so that they can be re-created later |
||
273 | * to reflect the database schema change. |
||
274 | */ |
||
275 | 9 | public function refresh() |
|
285 | |||
286 | /** |
||
287 | * Refreshes the particular table schema. |
||
288 | * This method cleans up cached table schema so that it can be re-created later |
||
289 | * to reflect the database schema change. |
||
290 | * @param string $name table name. |
||
291 | * @since 2.0.6 |
||
292 | */ |
||
293 | 19 | public function refreshTableSchema($name) |
|
303 | |||
304 | /** |
||
305 | * Creates a query builder for the database. |
||
306 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
307 | * @return QueryBuilder query builder instance |
||
308 | */ |
||
309 | public function createQueryBuilder() |
||
313 | |||
314 | /** |
||
315 | * Create a column schema builder instance giving the type and value precision. |
||
316 | * |
||
317 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
318 | * |
||
319 | * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
||
320 | * @param integer|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
||
321 | * @return ColumnSchemaBuilder column schema builder instance |
||
322 | * @since 2.0.6 |
||
323 | */ |
||
324 | 2 | public function createColumnSchemaBuilder($type, $length = null) |
|
328 | |||
329 | /** |
||
330 | * Returns all schema names in the database, including the default one but not system schemas. |
||
331 | * This method should be overridden by child classes in order to support this feature |
||
332 | * because the default implementation simply throws an exception. |
||
333 | * @return array all schema names in the database, except system schemas |
||
334 | * @throws NotSupportedException if this method is called |
||
335 | * @since 2.0.4 |
||
336 | */ |
||
337 | protected function findSchemaNames() |
||
341 | |||
342 | /** |
||
343 | * Returns all table names in the database. |
||
344 | * This method should be overridden by child classes in order to support this feature |
||
345 | * because the default implementation simply throws an exception. |
||
346 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
347 | * @return array all table names in the database. The names have NO schema name prefix. |
||
348 | * @throws NotSupportedException if this method is called |
||
349 | */ |
||
350 | protected function findTableNames($schema = '') |
||
354 | |||
355 | /** |
||
356 | * Returns all unique indexes for the given table. |
||
357 | * Each array element is of the following structure: |
||
358 | * |
||
359 | * ```php |
||
360 | * [ |
||
361 | * 'IndexName1' => ['col1' [, ...]], |
||
362 | * 'IndexName2' => ['col2' [, ...]], |
||
363 | * ] |
||
364 | * ``` |
||
365 | * |
||
366 | * This method should be overridden by child classes in order to support this feature |
||
367 | * because the default implementation simply throws an exception |
||
368 | * @param TableSchema $table the table metadata |
||
369 | * @return array all unique indexes for the given table. |
||
370 | * @throws NotSupportedException if this method is called |
||
371 | */ |
||
372 | public function findUniqueIndexes($table) |
||
376 | |||
377 | /** |
||
378 | * Returns the ID of the last inserted row or sequence value. |
||
379 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
380 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
381 | * @throws InvalidCallException if the DB connection is not active |
||
382 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
383 | */ |
||
384 | 37 | public function getLastInsertID($sequenceName = '') |
|
392 | |||
393 | /** |
||
394 | * @return boolean whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
395 | */ |
||
396 | 3 | public function supportsSavepoint() |
|
400 | |||
401 | /** |
||
402 | * Creates a new savepoint. |
||
403 | * @param string $name the savepoint name |
||
404 | */ |
||
405 | 3 | public function createSavepoint($name) |
|
409 | |||
410 | /** |
||
411 | * Releases an existing savepoint. |
||
412 | * @param string $name the savepoint name |
||
413 | */ |
||
414 | public function releaseSavepoint($name) |
||
418 | |||
419 | /** |
||
420 | * Rolls back to a previously created savepoint. |
||
421 | * @param string $name the savepoint name |
||
422 | */ |
||
423 | 3 | public function rollBackSavepoint($name) |
|
427 | |||
428 | /** |
||
429 | * Sets the isolation level of the current transaction. |
||
430 | * @param string $level The transaction isolation level to use for this transaction. |
||
431 | * This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
||
432 | * and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
||
433 | * after `SET TRANSACTION ISOLATION LEVEL`. |
||
434 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
435 | */ |
||
436 | 4 | public function setTransactionIsolationLevel($level) |
|
440 | |||
441 | /** |
||
442 | * Executes the INSERT command, returning primary key values. |
||
443 | * @param string $table the table that new rows will be inserted into. |
||
444 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
445 | * @return array primary key values or false if the command fails |
||
446 | * @since 2.0.4 |
||
447 | */ |
||
448 | 40 | public function insert($table, $columns) |
|
466 | |||
467 | /** |
||
468 | * Quotes a string value for use in a query. |
||
469 | * Note that if the parameter is not a string, it will be returned without change. |
||
470 | * @param string $str string to be quoted |
||
471 | * @return string the properly quoted string |
||
472 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
473 | */ |
||
474 | 389 | public function quoteValue($str) |
|
487 | |||
488 | /** |
||
489 | * Quotes a table name for use in a query. |
||
490 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
491 | * If the table name is already quoted or contains '(' or '{{', |
||
492 | * then this method will do nothing. |
||
493 | * @param string $name table name |
||
494 | * @return string the properly quoted table name |
||
495 | * @see quoteSimpleTableName() |
||
496 | */ |
||
497 | 580 | public function quoteTableName($name) |
|
513 | |||
514 | /** |
||
515 | * Quotes a column name for use in a query. |
||
516 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
517 | * If the column name is already quoted or contains '(', '[[' or '{{', |
||
518 | * then this method will do nothing. |
||
519 | * @param string $name column name |
||
520 | * @return string the properly quoted column name |
||
521 | * @see quoteSimpleColumnName() |
||
522 | */ |
||
523 | 651 | public function quoteColumnName($name) |
|
539 | |||
540 | /** |
||
541 | * Quotes a simple table name for use in a query. |
||
542 | * A simple table name should contain the table name only without any schema prefix. |
||
543 | * If the table name is already quoted, this method will do nothing. |
||
544 | * @param string $name table name |
||
545 | * @return string the properly quoted table name |
||
546 | */ |
||
547 | public function quoteSimpleTableName($name) |
||
551 | |||
552 | /** |
||
553 | * Quotes a simple column name for use in a query. |
||
554 | * A simple column name should contain the column name only without any prefix. |
||
555 | * If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
||
556 | * @param string $name column name |
||
557 | * @return string the properly quoted column name |
||
558 | */ |
||
559 | 185 | public function quoteSimpleColumnName($name) |
|
563 | |||
564 | /** |
||
565 | * Returns the actual name of a given table name. |
||
566 | * This method will strip off curly brackets from the given table name |
||
567 | * and replace the percentage character '%' with [[Connection::tablePrefix]]. |
||
568 | * @param string $name the table name to be converted |
||
569 | * @return string the real name of the given table name |
||
570 | */ |
||
571 | 359 | public function getRawTableName($name) |
|
581 | |||
582 | /** |
||
583 | * Extracts the PHP type from abstract DB type. |
||
584 | * @param ColumnSchema $column the column schema information |
||
585 | * @return string PHP type name |
||
586 | */ |
||
587 | 349 | protected function getColumnPhpType($column) |
|
611 | |||
612 | /** |
||
613 | * Converts a DB exception to a more concrete one if possible. |
||
614 | * |
||
615 | * @param \Exception $e |
||
616 | * @param string $rawSql SQL that produced exception |
||
617 | * @return Exception |
||
618 | */ |
||
619 | 19 | public function convertException(\Exception $e, $rawSql) |
|
635 | |||
636 | /** |
||
637 | * Returns a value indicating whether a SQL statement is for read purpose. |
||
638 | * @param string $sql the SQL statement |
||
639 | * @return boolean whether a SQL statement is for read purpose. |
||
640 | */ |
||
641 | 9 | public function isReadQuery($sql) |
|
646 | } |
||
647 |