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 | * @var string column schema class |
||
79 | * @since 2.0.11 |
||
80 | */ |
||
81 | public $columnSchemaClass = 'yii\db\ColumnSchema'; |
||
82 | |||
83 | /** |
||
84 | * @var array list of ALL schema names in the database, except system schemas |
||
85 | */ |
||
86 | private $_schemaNames; |
||
87 | /** |
||
88 | * @var array list of ALL table names in the database |
||
89 | */ |
||
90 | private $_tableNames = []; |
||
91 | /** |
||
92 | * @var array list of loaded table metadata (table name => TableSchema) |
||
93 | */ |
||
94 | private $_tables = []; |
||
95 | /** |
||
96 | * @var QueryBuilder the query builder for this database |
||
97 | */ |
||
98 | private $_builder; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * @return \yii\db\ColumnSchema |
||
103 | * @throws \yii\base\InvalidConfigException |
||
104 | */ |
||
105 | protected function createColumnSchema() |
||
109 | |||
110 | /** |
||
111 | * Loads the metadata for the specified table. |
||
112 | * @param string $name table name |
||
113 | * @return null|TableSchema DBMS-dependent table metadata, null if the table does not exist. |
||
114 | */ |
||
115 | abstract protected function loadTableSchema($name); |
||
116 | |||
117 | /** |
||
118 | * Obtains the metadata for the named table. |
||
119 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
120 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
121 | * @return null|TableSchema table metadata. Null if the named table does not exist. |
||
122 | */ |
||
123 | public function getTableSchema($name, $refresh = false) |
||
154 | |||
155 | /** |
||
156 | * Returns the cache key for the specified table name. |
||
157 | * @param string $name the table name |
||
158 | * @return mixed the cache key |
||
159 | */ |
||
160 | protected function getCacheKey($name) |
||
169 | |||
170 | /** |
||
171 | * Returns the cache tag name. |
||
172 | * This allows [[refresh()]] to invalidate all cached table schemas. |
||
173 | * @return string the cache tag name |
||
174 | */ |
||
175 | protected function getCacheTag() |
||
183 | |||
184 | /** |
||
185 | * Returns the metadata for all tables in the database. |
||
186 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
187 | * @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||
188 | * cached data may be returned if available. |
||
189 | * @return TableSchema[] the metadata for all tables in the database. |
||
190 | * Each array element is an instance of [[TableSchema]] or its child class. |
||
191 | */ |
||
192 | public function getTableSchemas($schema = '', $refresh = false) |
||
206 | |||
207 | /** |
||
208 | * Returns all schema names in the database, except system schemas. |
||
209 | * @param bool $refresh whether to fetch the latest available schema names. If this is false, |
||
210 | * schema names fetched previously (if available) will be returned. |
||
211 | * @return string[] all schema names in the database, except system schemas. |
||
212 | * @since 2.0.4 |
||
213 | */ |
||
214 | public function getSchemaNames($refresh = false) |
||
222 | |||
223 | /** |
||
224 | * Returns all table names in the database. |
||
225 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||
226 | * If not empty, the returned table names will be prefixed with the schema name. |
||
227 | * @param bool $refresh whether to fetch the latest available table names. If this is false, |
||
228 | * table names fetched previously (if available) will be returned. |
||
229 | * @return string[] all table names in the database. |
||
230 | */ |
||
231 | public function getTableNames($schema = '', $refresh = false) |
||
239 | |||
240 | /** |
||
241 | * @return QueryBuilder the query builder for this connection. |
||
242 | */ |
||
243 | public function getQueryBuilder() |
||
251 | |||
252 | /** |
||
253 | * Determines the PDO type for the given PHP data value. |
||
254 | * @param mixed $data the data whose PDO type is to be determined |
||
255 | * @return int the PDO type |
||
256 | * @see http://www.php.net/manual/en/pdo.constants.php |
||
257 | */ |
||
258 | public function getPdoType($data) |
||
272 | |||
273 | /** |
||
274 | * Refreshes the schema. |
||
275 | * This method cleans up all cached table schemas so that they can be re-created later |
||
276 | * to reflect the database schema change. |
||
277 | */ |
||
278 | public function refresh() |
||
288 | |||
289 | /** |
||
290 | * Refreshes the particular table schema. |
||
291 | * This method cleans up cached table schema so that it can be re-created later |
||
292 | * to reflect the database schema change. |
||
293 | * @param string $name table name. |
||
294 | * @since 2.0.6 |
||
295 | */ |
||
296 | public function refreshTableSchema($name) |
||
306 | |||
307 | /** |
||
308 | * Creates a query builder for the database. |
||
309 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
310 | * @return QueryBuilder query builder instance |
||
311 | */ |
||
312 | public function createQueryBuilder() |
||
316 | |||
317 | /** |
||
318 | * Create a column schema builder instance giving the type and value precision. |
||
319 | * |
||
320 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
321 | * |
||
322 | * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
||
323 | * @param int|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
||
324 | * @return ColumnSchemaBuilder column schema builder instance |
||
325 | * @since 2.0.6 |
||
326 | */ |
||
327 | public function createColumnSchemaBuilder($type, $length = null) |
||
331 | |||
332 | /** |
||
333 | * Returns all schema names in the database, including the default one but not system schemas. |
||
334 | * This method should be overridden by child classes in order to support this feature |
||
335 | * because the default implementation simply throws an exception. |
||
336 | * @return array all schema names in the database, except system schemas |
||
337 | * @throws NotSupportedException if this method is called |
||
338 | * @since 2.0.4 |
||
339 | */ |
||
340 | protected function findSchemaNames() |
||
344 | |||
345 | /** |
||
346 | * Returns all table names in the database. |
||
347 | * This method should be overridden by child classes in order to support this feature |
||
348 | * because the default implementation simply throws an exception. |
||
349 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
350 | * @return array all table names in the database. The names have NO schema name prefix. |
||
351 | * @throws NotSupportedException if this method is called |
||
352 | */ |
||
353 | protected function findTableNames($schema = '') |
||
357 | |||
358 | /** |
||
359 | * Returns all unique indexes for the given table. |
||
360 | * Each array element is of the following structure: |
||
361 | * |
||
362 | * ```php |
||
363 | * [ |
||
364 | * 'IndexName1' => ['col1' [, ...]], |
||
365 | * 'IndexName2' => ['col2' [, ...]], |
||
366 | * ] |
||
367 | * ``` |
||
368 | * |
||
369 | * This method should be overridden by child classes in order to support this feature |
||
370 | * because the default implementation simply throws an exception |
||
371 | * @param TableSchema $table the table metadata |
||
372 | * @return array all unique indexes for the given table. |
||
373 | * @throws NotSupportedException if this method is called |
||
374 | */ |
||
375 | public function findUniqueIndexes($table) |
||
379 | |||
380 | /** |
||
381 | * Returns the ID of the last inserted row or sequence value. |
||
382 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
383 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
384 | * @throws InvalidCallException if the DB connection is not active |
||
385 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
386 | */ |
||
387 | public function getLastInsertID($sequenceName = '') |
||
395 | |||
396 | /** |
||
397 | * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
398 | */ |
||
399 | public function supportsSavepoint() |
||
403 | |||
404 | /** |
||
405 | * Creates a new savepoint. |
||
406 | * @param string $name the savepoint name |
||
407 | */ |
||
408 | public function createSavepoint($name) |
||
412 | |||
413 | /** |
||
414 | * Releases an existing savepoint. |
||
415 | * @param string $name the savepoint name |
||
416 | */ |
||
417 | public function releaseSavepoint($name) |
||
421 | |||
422 | /** |
||
423 | * Rolls back to a previously created savepoint. |
||
424 | * @param string $name the savepoint name |
||
425 | */ |
||
426 | public function rollBackSavepoint($name) |
||
430 | |||
431 | /** |
||
432 | * Sets the isolation level of the current transaction. |
||
433 | * @param string $level The transaction isolation level to use for this transaction. |
||
434 | * This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
||
435 | * and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
||
436 | * after `SET TRANSACTION ISOLATION LEVEL`. |
||
437 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
438 | */ |
||
439 | public function setTransactionIsolationLevel($level) |
||
443 | |||
444 | /** |
||
445 | * Executes the INSERT command, returning primary key values. |
||
446 | * @param string $table the table that new rows will be inserted into. |
||
447 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
448 | * @return array|false primary key values or false if the command fails |
||
449 | * @since 2.0.4 |
||
450 | */ |
||
451 | public function insert($table, $columns) |
||
469 | |||
470 | /** |
||
471 | * Quotes a string value for use in a query. |
||
472 | * Note that if the parameter is not a string, it will be returned without change. |
||
473 | * @param string $str string to be quoted |
||
474 | * @return string the properly quoted string |
||
475 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
476 | */ |
||
477 | public function quoteValue($str) |
||
490 | |||
491 | /** |
||
492 | * Quotes a table name for use in a query. |
||
493 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
494 | * If the table name is already quoted or contains '(' or '{{', |
||
495 | * then this method will do nothing. |
||
496 | * @param string $name table name |
||
497 | * @return string the properly quoted table name |
||
498 | * @see quoteSimpleTableName() |
||
499 | */ |
||
500 | public function quoteTableName($name) |
||
516 | |||
517 | /** |
||
518 | * Quotes a column name for use in a query. |
||
519 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
520 | * If the column name is already quoted or contains '(', '[[' or '{{', |
||
521 | * then this method will do nothing. |
||
522 | * @param string $name column name |
||
523 | * @return string the properly quoted column name |
||
524 | * @see quoteSimpleColumnName() |
||
525 | */ |
||
526 | public function quoteColumnName($name) |
||
542 | |||
543 | /** |
||
544 | * Quotes a simple table name for use in a query. |
||
545 | * A simple table name should contain the table name only without any schema prefix. |
||
546 | * If the table name is already quoted, this method will do nothing. |
||
547 | * @param string $name table name |
||
548 | * @return string the properly quoted table name |
||
549 | */ |
||
550 | public function quoteSimpleTableName($name) |
||
554 | |||
555 | /** |
||
556 | * Quotes a simple column name for use in a query. |
||
557 | * A simple column name should contain the column name only without any prefix. |
||
558 | * If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
||
559 | * @param string $name column name |
||
560 | * @return string the properly quoted column name |
||
561 | */ |
||
562 | public function quoteSimpleColumnName($name) |
||
566 | |||
567 | /** |
||
568 | * Returns the actual name of a given table name. |
||
569 | * This method will strip off curly brackets from the given table name |
||
570 | * and replace the percentage character '%' with [[Connection::tablePrefix]]. |
||
571 | * @param string $name the table name to be converted |
||
572 | * @return string the real name of the given table name |
||
573 | */ |
||
574 | public function getRawTableName($name) |
||
584 | |||
585 | /** |
||
586 | * Extracts the PHP type from abstract DB type. |
||
587 | * @param ColumnSchema $column the column schema information |
||
588 | * @return string PHP type name |
||
589 | */ |
||
590 | protected function getColumnPhpType($column) |
||
614 | |||
615 | /** |
||
616 | * Converts a DB exception to a more concrete one if possible. |
||
617 | * |
||
618 | * @param \Exception $e |
||
619 | * @param string $rawSql SQL that produced exception |
||
620 | * @return Exception |
||
621 | */ |
||
622 | public function convertException(\Exception $e, $rawSql) |
||
638 | |||
639 | /** |
||
640 | * Returns a value indicating whether a SQL statement is for read purpose. |
||
641 | * @param string $sql the SQL statement |
||
642 | * @return bool whether a SQL statement is for read purpose. |
||
643 | */ |
||
644 | public function isReadQuery($sql) |
||
649 | } |
||
650 |