Complex classes like DB 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 DB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class DB { |
||
10 | |||
11 | /** |
||
12 | * This constant was added in SilverStripe 2.4 to indicate that SQL-queries |
||
13 | * should now use ANSI-compatible syntax. The most notable affect of this |
||
14 | * change is that table and field names should be escaped with double quotes |
||
15 | * and not backticks |
||
16 | */ |
||
17 | const USE_ANSI_SQL = true; |
||
18 | |||
19 | |||
20 | /** |
||
21 | * The global database connection. |
||
22 | * @var SS_Database |
||
23 | */ |
||
24 | private static $connections = array(); |
||
25 | |||
26 | /** |
||
27 | * The last SQL query run. |
||
28 | * @var string |
||
29 | */ |
||
30 | public static $lastQuery; |
||
31 | |||
32 | /** |
||
33 | * Internal flag to keep track of when db connection was attempted. |
||
34 | */ |
||
35 | private static $connection_attempted = false; |
||
36 | |||
37 | /** |
||
38 | * Set the global database connection. |
||
39 | * Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()} |
||
40 | * is called. |
||
41 | * |
||
42 | * @param $connection The connecton object to set as the connection. |
||
43 | * @param $name The name to give to this connection. If you omit this argument, the connection |
||
44 | * will be the default one used by the ORM. However, you can store other named connections to |
||
45 | * be accessed through DB::get_conn($name). This is useful when you have an application that |
||
46 | * needs to connect to more than one database. |
||
47 | */ |
||
48 | public static function set_conn(SS_Database $connection, $name = 'default') { |
||
51 | |||
52 | /** |
||
53 | * @deprecated since version 4.0 Use DB::set_conn instead |
||
54 | */ |
||
55 | public static function setConn(SS_Database $connection, $name = 'default') { |
||
59 | |||
60 | /** |
||
61 | * Get the global database connection. |
||
62 | * |
||
63 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
64 | * the default connection is returned. |
||
65 | * @return SS_Database |
||
66 | */ |
||
67 | public static function get_conn($name = 'default') { |
||
72 | |||
73 | /** |
||
74 | * @deprecated since version 4.0 Use DB::get_conn instead |
||
75 | */ |
||
76 | public static function getConn($name = 'default') { |
||
80 | |||
81 | /** |
||
82 | * Retrieves the schema manager for the current database |
||
83 | * |
||
84 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
85 | * the default connection is returned. |
||
86 | * @return DBSchemaManager |
||
87 | */ |
||
88 | public static function get_schema($name = 'default') { |
||
92 | |||
93 | /** |
||
94 | * Builds a sql query with the specified connection |
||
95 | * |
||
96 | * @param SQLExpression $expression The expression object to build from |
||
97 | * @param array $parameters Out parameter for the resulting query parameters |
||
98 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
99 | * the default connection is returned. |
||
100 | * @return string The resulting SQL as a string |
||
101 | */ |
||
102 | public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default') { |
||
111 | |||
112 | /** |
||
113 | * Retrieves the connector object for the current database |
||
114 | * |
||
115 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
116 | * the default connection is returned. |
||
117 | * @return DBConnector |
||
118 | */ |
||
119 | public static function get_connector($name = 'default') { |
||
123 | |||
124 | /** |
||
125 | * Set an alternative database in a browser cookie, |
||
126 | * with the cookie lifetime set to the browser session. |
||
127 | * This is useful for integration testing on temporary databases. |
||
128 | * |
||
129 | * There is a strict naming convention for temporary databases to avoid abuse: |
||
130 | * <prefix> (default: 'ss_') + tmpdb + <7 digits> |
||
131 | * As an additional security measure, temporary databases will |
||
132 | * be ignored in "live" mode. |
||
133 | * |
||
134 | * Note that the database will be set on the next request. |
||
135 | * Set it to null to revert to the main database. |
||
136 | */ |
||
137 | public static function set_alternative_database_name($name = null) { |
||
173 | |||
174 | /** |
||
175 | * Get the name of the database in use |
||
176 | */ |
||
177 | public static function get_alternative_database_name() { |
||
198 | |||
199 | /** |
||
200 | * Determines if the name is valid, as a security |
||
201 | * measure against setting arbitrary databases. |
||
202 | * |
||
203 | * @param String $name |
||
204 | * @return Boolean |
||
205 | */ |
||
206 | public static function valid_alternative_database_name($name) { |
||
213 | |||
214 | /** |
||
215 | * Connect to a database. |
||
216 | * |
||
217 | * Given the database configuration, this method will create the correct |
||
218 | * subclass of {@link SS_Database}. |
||
219 | * |
||
220 | * @param array $database A map of options. The 'type' is the name of the subclass of SS_Database to use. For the |
||
221 | * rest of the options, see the specific class. |
||
222 | * @param string $name identifier for the connection |
||
223 | * |
||
224 | * @return SS_Database |
||
225 | */ |
||
226 | public static function connect($databaseConfig, $label = 'default') { |
||
250 | |||
251 | /** |
||
252 | * Returns true if a database connection has been attempted. |
||
253 | * In particular, it lets the caller know if we're still so early in the execution pipeline that |
||
254 | * we haven't even tried to connect to the database yet. |
||
255 | */ |
||
256 | public static function connection_attempted() { |
||
259 | |||
260 | /** |
||
261 | * @deprecated since version 4.0 DB::getConnect was never implemented and is obsolete |
||
262 | */ |
||
263 | public static function getConnect($parameters) { |
||
266 | |||
267 | /** |
||
268 | * Execute the given SQL query. |
||
269 | * @param string $sql The SQL query to execute |
||
270 | * @param int $errorLevel The level of error reporting to enable for the query |
||
271 | * @return SS_Query |
||
272 | */ |
||
273 | public static function query($sql, $errorLevel = E_USER_ERROR) { |
||
278 | |||
279 | /** |
||
280 | * Helper function for generating a list of parameter placeholders for the |
||
281 | * given argument(s) |
||
282 | * |
||
283 | * @param array|integer $input An array of items needing placeholders, or a |
||
284 | * number to specify the number of placeholders |
||
285 | * @param string $join The string to join each placeholder together with |
||
286 | * @return string|null Either a list of placeholders, or null |
||
287 | */ |
||
288 | public static function placeholders($input, $join = ', ') { |
||
299 | |||
300 | /** |
||
301 | * @param string $sql The parameterised query |
||
302 | * @param array $parameters The parameters to inject into the query |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public static function inline_parameters($sql, $parameters) { |
||
353 | |||
354 | /** |
||
355 | * Execute the given SQL parameterised query with the specified arguments |
||
356 | * |
||
357 | * @param string $sql The SQL query to execute. The ? character will denote parameters. |
||
358 | * @param array $parameters An ordered list of arguments. |
||
359 | * @param int $errorLevel The level of error reporting to enable for the query |
||
360 | * @return SS_Query |
||
361 | */ |
||
362 | public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR) { |
||
367 | |||
368 | /** |
||
369 | * Execute a complex manipulation on the database. |
||
370 | * A manipulation is an array of insert / or update sequences. The keys of the array are table names, |
||
371 | * and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update', |
||
372 | * and fields should be a map of field names to field values, including quotes. The field value can |
||
373 | * also be a SQL function or similar. |
||
374 | * |
||
375 | * Example: |
||
376 | * <code> |
||
377 | * array( |
||
378 | * // Command: insert |
||
379 | * "table name" => array( |
||
380 | * "command" => "insert", |
||
381 | * "fields" => array( |
||
382 | * "ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes |
||
383 | * "Created" => "now()", // alternatively, you can call DB functions |
||
384 | * "ID" => 234, |
||
385 | * ), |
||
386 | * "id" => 234 // an alternative to providing ID in the fields list |
||
387 | * ), |
||
388 | * |
||
389 | * // Command: update |
||
390 | * "other table" => array( |
||
391 | * "command" => "update", |
||
392 | * "fields" => array( |
||
393 | * "ClassName" => "'MyClass'", |
||
394 | * "LastEdited" => "now()", |
||
395 | * ), |
||
396 | * "where" => "ID = 234", |
||
397 | * "id" => 234 // an alternative to providing a where clause |
||
398 | * ), |
||
399 | * ) |
||
400 | * </code> |
||
401 | * |
||
402 | * You'll note that only one command on a given table can be called. |
||
403 | * That's a limitation of the system that's due to it being written for {@link DataObject::write()}, |
||
404 | * which needs to do a single write on a number of different tables. |
||
405 | * |
||
406 | * @todo Update this to support paramaterised queries |
||
407 | * |
||
408 | * @param array $manipulation |
||
409 | */ |
||
410 | public static function manipulate($manipulation) { |
||
414 | |||
415 | /** |
||
416 | * Get the autogenerated ID from the previous INSERT query. |
||
417 | * @return int |
||
418 | */ |
||
419 | public static function get_generated_id($table) { |
||
422 | |||
423 | /** |
||
424 | * @deprecated since version 4.0 Use DB::get_generated_id instead |
||
425 | */ |
||
426 | public static function getGeneratedID($table) { |
||
430 | |||
431 | /** |
||
432 | * Check if the connection to the database is active. |
||
433 | * |
||
434 | * @return boolean |
||
435 | */ |
||
436 | public static function is_active() { |
||
439 | |||
440 | /** |
||
441 | * @deprecated since version 4.0 Use DB::is_active instead |
||
442 | */ |
||
443 | public static function isActive() { |
||
447 | |||
448 | /** |
||
449 | * Create the database and connect to it. This can be called if the |
||
450 | * initial database connection is not successful because the database |
||
451 | * does not exist. |
||
452 | * |
||
453 | * @param string $database Name of database to create |
||
454 | * @return boolean Returns true if successful |
||
455 | */ |
||
456 | public static function create_database($database) { |
||
459 | |||
460 | /** |
||
461 | * @deprecated since version 4.0 Use DB::create_database instead |
||
462 | */ |
||
463 | public static function createDatabase($connect, $username, $password, $database) { |
||
467 | |||
468 | /** |
||
469 | * Create a new table. |
||
470 | * @param string $tableName The name of the table |
||
471 | * @param array$fields A map of field names to field types |
||
472 | * @param array $indexes A map of indexes |
||
473 | * @param array $options An map of additional options. The available keys are as follows: |
||
474 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" |
||
475 | * for MySQL. |
||
476 | * - 'temporary' - If true, then a temporary table will be created |
||
477 | * @return string The table name generated. This may be different from the table name, for example with |
||
478 | * temporary tables. |
||
479 | */ |
||
480 | public static function create_table($table, $fields = null, $indexes = null, $options = null, |
||
485 | |||
486 | /** |
||
487 | * @deprecated since version 4.0 Use DB::create_table instead |
||
488 | */ |
||
489 | public static function createTable($table, $fields = null, $indexes = null, $options = null) { |
||
493 | |||
494 | /** |
||
495 | * Create a new field on a table. |
||
496 | * @param string $table Name of the table. |
||
497 | * @param string $field Name of the field to add. |
||
498 | * @param string $spec The field specification, eg 'INTEGER NOT NULL' |
||
499 | */ |
||
500 | public static function create_field($table, $field, $spec) { |
||
503 | |||
504 | /** |
||
505 | * @deprecated since version 4.0 Use DB::create_field instead |
||
506 | */ |
||
507 | public static function createField($table, $field, $spec) { |
||
511 | |||
512 | /** |
||
513 | * Generate the following table in the database, modifying whatever already exists |
||
514 | * as necessary. |
||
515 | * |
||
516 | * @param string $table The name of the table |
||
517 | * @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db |
||
518 | * @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index. |
||
519 | * The values of the array can be one of: |
||
520 | * - true: Create a single column index on the field named the same as the index. |
||
521 | * - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full |
||
522 | * control over the index. |
||
523 | * @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type |
||
524 | * @param string $options SQL statement to append to the CREATE TABLE call. |
||
525 | * @param array $extensions List of extensions |
||
526 | */ |
||
527 | public static function require_table($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
||
533 | |||
534 | /** |
||
535 | * @deprecated since version 4.0 Use DB::require_table instead |
||
536 | */ |
||
537 | public static function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
||
543 | |||
544 | /** |
||
545 | * Generate the given field on the table, modifying whatever already exists as necessary. |
||
546 | * |
||
547 | * @param string $table The table name. |
||
548 | * @param string $field The field name. |
||
549 | * @param string $spec The field specification. |
||
550 | */ |
||
551 | public static function require_field($table, $field, $spec) { |
||
554 | |||
555 | /** |
||
556 | * @deprecated since version 4.0 Use DB::require_field instead |
||
557 | */ |
||
558 | public static function requireField($table, $field, $spec) { |
||
562 | |||
563 | /** |
||
564 | * Generate the given index in the database, modifying whatever already exists as necessary. |
||
565 | * |
||
566 | * @param string $table The table name. |
||
567 | * @param string $index The index name. |
||
568 | * @param string|boolean $spec The specification of the index. See requireTable() for more information. |
||
569 | */ |
||
570 | public static function require_index($table, $index, $spec) { |
||
573 | |||
574 | /** |
||
575 | * @deprecated since version 4.0 Use DB::require_index instead |
||
576 | */ |
||
577 | public static function requireIndex($table, $index, $spec) { |
||
581 | |||
582 | /** |
||
583 | * If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
||
584 | * |
||
585 | * @param string $table The table name. |
||
586 | */ |
||
587 | public static function dont_require_table($table) { |
||
590 | |||
591 | /** |
||
592 | * @deprecated since version 4.0 Use DB::dont_require_table instead |
||
593 | */ |
||
594 | public static function dontRequireTable($table) { |
||
598 | |||
599 | /** |
||
600 | * See {@link SS_Database->dontRequireField()}. |
||
601 | * |
||
602 | * @param string $table The table name. |
||
603 | * @param string $fieldName The field name not to require |
||
604 | */ |
||
605 | public static function dont_require_field($table, $fieldName) { |
||
608 | |||
609 | /** |
||
610 | * @deprecated since version 4.0 Use DB::dont_require_field instead |
||
611 | */ |
||
612 | public static function dontRequireField($table, $fieldName) { |
||
616 | |||
617 | /** |
||
618 | * Checks a table's integrity and repairs it if necessary. |
||
619 | * |
||
620 | * @param string $tableName The name of the table. |
||
621 | * @return boolean Return true if the table has integrity after the method is complete. |
||
622 | */ |
||
623 | public static function check_and_repair_table($table) { |
||
626 | |||
627 | /** |
||
628 | * @deprecated since version 4.0 Use DB::check_and_repair_table instead |
||
629 | */ |
||
630 | public static function checkAndRepairTable($table) { |
||
634 | |||
635 | /** |
||
636 | * Return the number of rows affected by the previous operation. |
||
637 | * |
||
638 | * @return integer The number of affected rows |
||
639 | */ |
||
640 | public static function affected_rows() { |
||
643 | |||
644 | /** |
||
645 | * @deprecated since version 4.0 Use DB::affected_rows instead |
||
646 | */ |
||
647 | public static function affectedRows() { |
||
651 | |||
652 | /** |
||
653 | * Returns a list of all tables in the database. |
||
654 | * The table names will be in lower case. |
||
655 | * |
||
656 | * @return array The list of tables |
||
657 | */ |
||
658 | public static function table_list() { |
||
661 | |||
662 | /** |
||
663 | * @deprecated since version 4.0 Use DB::table_list instead |
||
664 | */ |
||
665 | public static function tableList() { |
||
669 | |||
670 | /** |
||
671 | * Get a list of all the fields for the given table. |
||
672 | * Returns a map of field name => field spec. |
||
673 | * |
||
674 | * @param string $table The table name. |
||
675 | * @return array The list of fields |
||
676 | */ |
||
677 | public static function field_list($table) { |
||
680 | |||
681 | /** |
||
682 | * @deprecated since version 4.0 Use DB::field_list instead |
||
683 | */ |
||
684 | public static function fieldList($table) { |
||
688 | |||
689 | /** |
||
690 | * Enable supression of database messages. |
||
691 | */ |
||
692 | public static function quiet() { |
||
695 | |||
696 | /** |
||
697 | * Show a message about database alteration |
||
698 | * |
||
699 | * @param string $message to display |
||
700 | * @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
||
701 | */ |
||
702 | public static function alteration_message($message, $type = "") { |
||
705 | |||
706 | } |
||
707 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: