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 |
||
| 22 | class DB { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * This constant was added in SilverStripe 2.4 to indicate that SQL-queries |
||
| 26 | * should now use ANSI-compatible syntax. The most notable affect of this |
||
| 27 | * change is that table and field names should be escaped with double quotes |
||
| 28 | * and not backticks |
||
| 29 | */ |
||
| 30 | const USE_ANSI_SQL = true; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * The global database connection. |
||
| 35 | * @var Database |
||
| 36 | */ |
||
| 37 | private static $connections = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The last SQL query run. |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public static $lastQuery; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Internal flag to keep track of when db connection was attempted. |
||
| 47 | */ |
||
| 48 | private static $connection_attempted = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set the global database connection. |
||
| 52 | * Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()} |
||
| 53 | * is called. |
||
| 54 | * |
||
| 55 | * @param Database $connection The connecton object to set as the connection. |
||
| 56 | * @param string $name The name to give to this connection. If you omit this argument, the connection |
||
| 57 | * will be the default one used by the ORM. However, you can store other named connections to |
||
| 58 | * be accessed through DB::get_conn($name). This is useful when you have an application that |
||
| 59 | * needs to connect to more than one database. |
||
| 60 | */ |
||
| 61 | public static function set_conn(Database $connection, $name = 'default') { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the global database connection. |
||
| 67 | * |
||
| 68 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 69 | * the default connection is returned. |
||
| 70 | * @return Database |
||
| 71 | */ |
||
| 72 | public static function get_conn($name = 'default') { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @deprecated since version 4.0 Use DB::get_conn instead |
||
| 81 | * @todo PSR-2 standardisation will probably un-deprecate this |
||
| 82 | */ |
||
| 83 | public static function getConn($name = 'default') { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Retrieves the schema manager for the current database |
||
| 90 | * |
||
| 91 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 92 | * the default connection is returned. |
||
| 93 | * @return DBSchemaManager |
||
| 94 | */ |
||
| 95 | public static function get_schema($name = 'default') { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Builds a sql query with the specified connection |
||
| 105 | * |
||
| 106 | * @param SQLExpression $expression The expression object to build from |
||
| 107 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 108 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 109 | * the default connection is returned. |
||
| 110 | * @return string The resulting SQL as a string |
||
| 111 | */ |
||
| 112 | public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default') { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Retrieves the connector object for the current database |
||
| 124 | * |
||
| 125 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 126 | * the default connection is returned. |
||
| 127 | * @return DBConnector |
||
| 128 | */ |
||
| 129 | public static function get_connector($name = 'default') { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set an alternative database in a browser cookie, |
||
| 139 | * with the cookie lifetime set to the browser session. |
||
| 140 | * This is useful for integration testing on temporary databases. |
||
| 141 | * |
||
| 142 | * There is a strict naming convention for temporary databases to avoid abuse: |
||
| 143 | * <prefix> (default: 'ss_') + tmpdb + <7 digits> |
||
| 144 | * As an additional security measure, temporary databases will |
||
| 145 | * be ignored in "live" mode. |
||
| 146 | * |
||
| 147 | * Note that the database will be set on the next request. |
||
| 148 | * Set it to null to revert to the main database. |
||
| 149 | * @param string $name |
||
| 150 | */ |
||
| 151 | public static function set_alternative_database_name($name = null) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the name of the database in use |
||
| 190 | */ |
||
| 191 | public static function get_alternative_database_name() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Determines if the name is valid, as a security |
||
| 215 | * measure against setting arbitrary databases. |
||
| 216 | * |
||
| 217 | * @param String $name |
||
| 218 | * @return Boolean |
||
| 219 | */ |
||
| 220 | public static function valid_alternative_database_name($name) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Connect to a database. |
||
| 230 | * |
||
| 231 | * Given the database configuration, this method will create the correct |
||
| 232 | * subclass of {@link SS_Database}. |
||
| 233 | * |
||
| 234 | * @param array $databaseConfig A map of options. The 'type' is the name of the |
||
| 235 | * subclass of SS_Database to use. For the rest of the options, see the specific class. |
||
| 236 | * @param string $label identifier for the connection |
||
| 237 | * @return Database |
||
| 238 | */ |
||
| 239 | public static function connect($databaseConfig, $label = 'default') { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns true if a database connection has been attempted. |
||
| 266 | * In particular, it lets the caller know if we're still so early in the execution pipeline that |
||
| 267 | * we haven't even tried to connect to the database yet. |
||
| 268 | */ |
||
| 269 | public static function connection_attempted() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Execute the given SQL query. |
||
| 275 | * @param string $sql The SQL query to execute |
||
| 276 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 277 | * @return Query |
||
| 278 | */ |
||
| 279 | public static function query($sql, $errorLevel = E_USER_ERROR) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Helper function for generating a list of parameter placeholders for the |
||
| 287 | * given argument(s) |
||
| 288 | * |
||
| 289 | * @param array|integer $input An array of items needing placeholders, or a |
||
| 290 | * number to specify the number of placeholders |
||
| 291 | * @param string $join The string to join each placeholder together with |
||
| 292 | * @return string|null Either a list of placeholders, or null |
||
| 293 | */ |
||
| 294 | public static function placeholders($input, $join = ', ') { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $sql The parameterised query |
||
| 308 | * @param array $parameters The parameters to inject into the query |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public static function inline_parameters($sql, $parameters) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Execute the given SQL parameterised query with the specified arguments |
||
| 362 | * |
||
| 363 | * @param string $sql The SQL query to execute. The ? character will denote parameters. |
||
| 364 | * @param array $parameters An ordered list of arguments. |
||
| 365 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 366 | * @return Query |
||
| 367 | */ |
||
| 368 | public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Execute a complex manipulation on the database. |
||
| 376 | * A manipulation is an array of insert / or update sequences. The keys of the array are table names, |
||
| 377 | * and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update', |
||
| 378 | * and fields should be a map of field names to field values, including quotes. The field value can |
||
| 379 | * also be a SQL function or similar. |
||
| 380 | * |
||
| 381 | * Example: |
||
| 382 | * <code> |
||
| 383 | * array( |
||
| 384 | * // Command: insert |
||
| 385 | * "table name" => array( |
||
| 386 | * "command" => "insert", |
||
| 387 | * "fields" => array( |
||
| 388 | * "ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes |
||
| 389 | * "Created" => "now()", // alternatively, you can call DB functions |
||
| 390 | * "ID" => 234, |
||
| 391 | * ), |
||
| 392 | * "id" => 234 // an alternative to providing ID in the fields list |
||
| 393 | * ), |
||
| 394 | * |
||
| 395 | * // Command: update |
||
| 396 | * "other table" => array( |
||
| 397 | * "command" => "update", |
||
| 398 | * "fields" => array( |
||
| 399 | * "ClassName" => "'MyClass'", |
||
| 400 | * "LastEdited" => "now()", |
||
| 401 | * ), |
||
| 402 | * "where" => "ID = 234", |
||
| 403 | * "id" => 234 // an alternative to providing a where clause |
||
| 404 | * ), |
||
| 405 | * ) |
||
| 406 | * </code> |
||
| 407 | * |
||
| 408 | * You'll note that only one command on a given table can be called. |
||
| 409 | * That's a limitation of the system that's due to it being written for {@link DataObject::write()}, |
||
| 410 | * which needs to do a single write on a number of different tables. |
||
| 411 | * |
||
| 412 | * @todo Update this to support paramaterised queries |
||
| 413 | * |
||
| 414 | * @param array $manipulation |
||
| 415 | */ |
||
| 416 | public static function manipulate($manipulation) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get the autogenerated ID from the previous INSERT query. |
||
| 423 | * |
||
| 424 | * @param string $table |
||
| 425 | * @return int |
||
| 426 | */ |
||
| 427 | public static function get_generated_id($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 | * Create the database and connect to it. This can be called if the |
||
| 442 | * initial database connection is not successful because the database |
||
| 443 | * does not exist. |
||
| 444 | * |
||
| 445 | * @param string $database Name of database to create |
||
| 446 | * @return boolean Returns true if successful |
||
| 447 | */ |
||
| 448 | public static function create_database($database) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Create a new table. |
||
| 454 | * @param string $table The name of the table |
||
| 455 | * @param array$fields A map of field names to field types |
||
| 456 | * @param array $indexes A map of indexes |
||
| 457 | * @param array $options An map of additional options. The available keys are as follows: |
||
| 458 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" |
||
| 459 | * for MySQL. |
||
| 460 | * - 'temporary' - If true, then a temporary table will be created |
||
| 461 | * @param array $advancedOptions Advanced creation options |
||
| 462 | * @return string The table name generated. This may be different from the table name, for example with |
||
| 463 | * temporary tables. |
||
| 464 | */ |
||
| 465 | public static function create_table($table, $fields = null, $indexes = null, $options = null, |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Create a new field on a table. |
||
| 473 | * @param string $table Name of the table. |
||
| 474 | * @param string $field Name of the field to add. |
||
| 475 | * @param string $spec The field specification, eg 'INTEGER NOT NULL' |
||
| 476 | */ |
||
| 477 | public static function create_field($table, $field, $spec) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Generate the following table in the database, modifying whatever already exists |
||
| 483 | * as necessary. |
||
| 484 | * |
||
| 485 | * @param string $table The name of the table |
||
| 486 | * @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db |
||
| 487 | * @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index. |
||
| 488 | * The values of the array can be one of: |
||
| 489 | * - true: Create a single column index on the field named the same as the index. |
||
| 490 | * - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full |
||
| 491 | * control over the index. |
||
| 492 | * @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type |
||
| 493 | * @param string $options SQL statement to append to the CREATE TABLE call. |
||
| 494 | * @param array $extensions List of extensions |
||
| 495 | */ |
||
| 496 | public static function require_table($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Generate the given field on the table, modifying whatever already exists as necessary. |
||
| 504 | * |
||
| 505 | * @param string $table The table name. |
||
| 506 | * @param string $field The field name. |
||
| 507 | * @param string $spec The field specification. |
||
| 508 | */ |
||
| 509 | public static function require_field($table, $field, $spec) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Generate the given index in the database, modifying whatever already exists as necessary. |
||
| 515 | * |
||
| 516 | * @param string $table The table name. |
||
| 517 | * @param string $index The index name. |
||
| 518 | * @param string|boolean $spec The specification of the index. See requireTable() for more information. |
||
| 519 | */ |
||
| 520 | public static function require_index($table, $index, $spec) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
||
| 526 | * |
||
| 527 | * @param string $table The table name. |
||
| 528 | */ |
||
| 529 | public static function dont_require_table($table) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * See {@link SS_Database->dontRequireField()}. |
||
| 535 | * |
||
| 536 | * @param string $table The table name. |
||
| 537 | * @param string $fieldName The field name not to require |
||
| 538 | */ |
||
| 539 | public static function dont_require_field($table, $fieldName) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Checks a table's integrity and repairs it if necessary. |
||
| 545 | * |
||
| 546 | * @param string $table The name of the table. |
||
| 547 | * @return boolean Return true if the table has integrity after the method is complete. |
||
| 548 | */ |
||
| 549 | public static function check_and_repair_table($table) { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Return the number of rows affected by the previous operation. |
||
| 555 | * |
||
| 556 | * @return integer The number of affected rows |
||
| 557 | */ |
||
| 558 | public static function affected_rows() { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Returns a list of all tables in the database. |
||
| 564 | * The table names will be in lower case. |
||
| 565 | * |
||
| 566 | * @return array The list of tables |
||
| 567 | */ |
||
| 568 | public static function table_list() { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get a list of all the fields for the given table. |
||
| 574 | * Returns a map of field name => field spec. |
||
| 575 | * |
||
| 576 | * @param string $table The table name. |
||
| 577 | * @return array The list of fields |
||
| 578 | */ |
||
| 579 | public static function field_list($table) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Enable supression of database messages. |
||
| 585 | */ |
||
| 586 | public static function quiet() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Show a message about database alteration |
||
| 592 | * |
||
| 593 | * @param string $message to display |
||
| 594 | * @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
||
| 595 | */ |
||
| 596 | public static function alteration_message($message, $type = "") { |
||
| 599 | |||
| 600 | } |
||
| 601 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: