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 |
||
| 25 | class DB { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * This constant was added in SilverStripe 2.4 to indicate that SQL-queries |
||
| 29 | * should now use ANSI-compatible syntax. The most notable affect of this |
||
| 30 | * change is that table and field names should be escaped with double quotes |
||
| 31 | * and not backticks |
||
| 32 | */ |
||
| 33 | const USE_ANSI_SQL = true; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * The global database connection. |
||
| 38 | * @var SS_Database |
||
| 39 | */ |
||
| 40 | private static $connections = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The last SQL query run. |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public static $lastQuery; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Internal flag to keep track of when db connection was attempted. |
||
| 50 | */ |
||
| 51 | private static $connection_attempted = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set the global database connection. |
||
| 55 | * Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()} |
||
| 56 | * is called. |
||
| 57 | * |
||
| 58 | * @param SS_Database $connection The connecton object to set as the connection. |
||
| 59 | * @param string $name The name to give to this connection. If you omit this argument, the connection |
||
| 60 | * will be the default one used by the ORM. However, you can store other named connections to |
||
| 61 | * be accessed through DB::get_conn($name). This is useful when you have an application that |
||
| 62 | * needs to connect to more than one database. |
||
| 63 | */ |
||
| 64 | public static function set_conn(SS_Database $connection, $name = 'default') { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the global database connection. |
||
| 70 | * |
||
| 71 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 72 | * the default connection is returned. |
||
| 73 | * @return SS_Database |
||
| 74 | */ |
||
| 75 | public static function get_conn($name = 'default') { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @deprecated since version 4.0 Use DB::get_conn instead |
||
| 84 | */ |
||
| 85 | public static function getConn($name = 'default') { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Retrieves the schema manager for the current database |
||
| 92 | * |
||
| 93 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 94 | * the default connection is returned. |
||
| 95 | * @return DBSchemaManager |
||
| 96 | */ |
||
| 97 | public static function get_schema($name = 'default') { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Builds a sql query with the specified connection |
||
| 107 | * |
||
| 108 | * @param SQLExpression $expression The expression object to build from |
||
| 109 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 110 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 111 | * the default connection is returned. |
||
| 112 | * @return string The resulting SQL as a string |
||
| 113 | */ |
||
| 114 | public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default') { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Retrieves the connector object for the current database |
||
| 126 | * |
||
| 127 | * @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
||
| 128 | * the default connection is returned. |
||
| 129 | * @return DBConnector |
||
| 130 | */ |
||
| 131 | public static function get_connector($name = 'default') { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set an alternative database in a browser cookie, |
||
| 141 | * with the cookie lifetime set to the browser session. |
||
| 142 | * This is useful for integration testing on temporary databases. |
||
| 143 | * |
||
| 144 | * There is a strict naming convention for temporary databases to avoid abuse: |
||
| 145 | * <prefix> (default: 'ss_') + tmpdb + <7 digits> |
||
| 146 | * As an additional security measure, temporary databases will |
||
| 147 | * be ignored in "live" mode. |
||
| 148 | * |
||
| 149 | * Note that the database will be set on the next request. |
||
| 150 | * Set it to null to revert to the main database. |
||
| 151 | * @param string $name |
||
| 152 | */ |
||
| 153 | public static function set_alternative_database_name($name = null) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the name of the database in use |
||
| 192 | */ |
||
| 193 | public static function get_alternative_database_name() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Determines if the name is valid, as a security |
||
| 217 | * measure against setting arbitrary databases. |
||
| 218 | * |
||
| 219 | * @param String $name |
||
| 220 | * @return Boolean |
||
| 221 | */ |
||
| 222 | public static function valid_alternative_database_name($name) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Connect to a database. |
||
| 232 | * |
||
| 233 | * Given the database configuration, this method will create the correct |
||
| 234 | * subclass of {@link SS_Database}. |
||
| 235 | * |
||
| 236 | * @param array $databaseConfig A map of options. The 'type' is the name of the |
||
| 237 | * subclass of SS_Database to use. For the rest of the options, see the specific class. |
||
| 238 | * @param string $label identifier for the connection |
||
| 239 | * @return SS_Database |
||
| 240 | */ |
||
| 241 | public static function connect($databaseConfig, $label = 'default') { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns true if a database connection has been attempted. |
||
| 268 | * In particular, it lets the caller know if we're still so early in the execution pipeline that |
||
| 269 | * we haven't even tried to connect to the database yet. |
||
| 270 | */ |
||
| 271 | public static function connection_attempted() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Execute the given SQL query. |
||
| 277 | * @param string $sql The SQL query to execute |
||
| 278 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 279 | * @return SS_Query |
||
| 280 | */ |
||
| 281 | public static function query($sql, $errorLevel = E_USER_ERROR) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Helper function for generating a list of parameter placeholders for the |
||
| 289 | * given argument(s) |
||
| 290 | * |
||
| 291 | * @param array|integer $input An array of items needing placeholders, or a |
||
| 292 | * number to specify the number of placeholders |
||
| 293 | * @param string $join The string to join each placeholder together with |
||
| 294 | * @return string|null Either a list of placeholders, or null |
||
| 295 | */ |
||
| 296 | public static function placeholders($input, $join = ', ') { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Execute the given SQL parameterised query with the specified arguments |
||
| 310 | * |
||
| 311 | * @param string $sql The SQL query to execute. The ? character will denote parameters. |
||
| 312 | * @param array $parameters An ordered list of arguments. |
||
| 313 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 314 | * @return SS_Query |
||
| 315 | */ |
||
| 316 | public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Execute a complex manipulation on the database. |
||
| 324 | * A manipulation is an array of insert / or update sequences. The keys of the array are table names, |
||
| 325 | * and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update', |
||
| 326 | * and fields should be a map of field names to field values, including quotes. The field value can |
||
| 327 | * also be a SQL function or similar. |
||
| 328 | * |
||
| 329 | * Example: |
||
| 330 | * <code> |
||
| 331 | * array( |
||
| 332 | * // Command: insert |
||
| 333 | * "table name" => array( |
||
| 334 | * "command" => "insert", |
||
| 335 | * "fields" => array( |
||
| 336 | * "ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes |
||
| 337 | * "Created" => "now()", // alternatively, you can call DB functions |
||
| 338 | * "ID" => 234, |
||
| 339 | * ), |
||
| 340 | * "id" => 234 // an alternative to providing ID in the fields list |
||
| 341 | * ), |
||
| 342 | * |
||
| 343 | * // Command: update |
||
| 344 | * "other table" => array( |
||
| 345 | * "command" => "update", |
||
| 346 | * "fields" => array( |
||
| 347 | * "ClassName" => "'MyClass'", |
||
| 348 | * "LastEdited" => "now()", |
||
| 349 | * ), |
||
| 350 | * "where" => "ID = 234", |
||
| 351 | * "id" => 234 // an alternative to providing a where clause |
||
| 352 | * ), |
||
| 353 | * ) |
||
| 354 | * </code> |
||
| 355 | * |
||
| 356 | * You'll note that only one command on a given table can be called. |
||
| 357 | * That's a limitation of the system that's due to it being written for {@link DataObject::write()}, |
||
| 358 | * which needs to do a single write on a number of different tables. |
||
| 359 | * |
||
| 360 | * @todo Update this to support paramaterised queries |
||
| 361 | * |
||
| 362 | * @param array $manipulation |
||
| 363 | */ |
||
| 364 | public static function manipulate($manipulation) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the autogenerated ID from the previous INSERT query. |
||
| 371 | * |
||
| 372 | * @param string $table |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public static function get_generated_id($table) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Check if the connection to the database is active. |
||
| 381 | * |
||
| 382 | * @return boolean |
||
| 383 | */ |
||
| 384 | public static function is_active() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Create the database and connect to it. This can be called if the |
||
| 390 | * initial database connection is not successful because the database |
||
| 391 | * does not exist. |
||
| 392 | * |
||
| 393 | * @param string $database Name of database to create |
||
| 394 | * @return boolean Returns true if successful |
||
| 395 | */ |
||
| 396 | public static function create_database($database) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Create a new table. |
||
| 402 | * @param string $table The name of the table |
||
| 403 | * @param array$fields A map of field names to field types |
||
| 404 | * @param array $indexes A map of indexes |
||
| 405 | * @param array $options An map of additional options. The available keys are as follows: |
||
| 406 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" |
||
| 407 | * for MySQL. |
||
| 408 | * - 'temporary' - If true, then a temporary table will be created |
||
| 409 | * @param array $advancedOptions Advanced creation options |
||
| 410 | * @return string The table name generated. This may be different from the table name, for example with |
||
| 411 | * temporary tables. |
||
| 412 | */ |
||
| 413 | public static function create_table($table, $fields = null, $indexes = null, $options = null, |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Create a new field on a table. |
||
| 421 | * @param string $table Name of the table. |
||
| 422 | * @param string $field Name of the field to add. |
||
| 423 | * @param string $spec The field specification, eg 'INTEGER NOT NULL' |
||
| 424 | */ |
||
| 425 | public static function create_field($table, $field, $spec) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Generate the following table in the database, modifying whatever already exists |
||
| 431 | * as necessary. |
||
| 432 | * |
||
| 433 | * @param string $table The name of the table |
||
| 434 | * @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db |
||
| 435 | * @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index. |
||
| 436 | * The values of the array can be one of: |
||
| 437 | * - true: Create a single column index on the field named the same as the index. |
||
| 438 | * - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full |
||
| 439 | * control over the index. |
||
| 440 | * @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type |
||
| 441 | * @param string $options SQL statement to append to the CREATE TABLE call. |
||
| 442 | * @param array $extensions List of extensions |
||
| 443 | */ |
||
| 444 | public static function require_table($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Generate the given field on the table, modifying whatever already exists as necessary. |
||
| 452 | * |
||
| 453 | * @param string $table The table name. |
||
| 454 | * @param string $field The field name. |
||
| 455 | * @param string $spec The field specification. |
||
| 456 | */ |
||
| 457 | public static function require_field($table, $field, $spec) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Generate the given index in the database, modifying whatever already exists as necessary. |
||
| 463 | * |
||
| 464 | * @param string $table The table name. |
||
| 465 | * @param string $index The index name. |
||
| 466 | * @param string|boolean $spec The specification of the index. See requireTable() for more information. |
||
| 467 | */ |
||
| 468 | public static function require_index($table, $index, $spec) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
||
| 474 | * |
||
| 475 | * @param string $table The table name. |
||
| 476 | */ |
||
| 477 | public static function dont_require_table($table) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * See {@link SS_Database->dontRequireField()}. |
||
| 483 | * |
||
| 484 | * @param string $table The table name. |
||
| 485 | * @param string $fieldName The field name not to require |
||
| 486 | */ |
||
| 487 | public static function dont_require_field($table, $fieldName) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Checks a table's integrity and repairs it if necessary. |
||
| 493 | * |
||
| 494 | * @param string $table The name of the table. |
||
| 495 | * @return boolean Return true if the table has integrity after the method is complete. |
||
| 496 | */ |
||
| 497 | public static function check_and_repair_table($table) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Return the number of rows affected by the previous operation. |
||
| 503 | * |
||
| 504 | * @return integer The number of affected rows |
||
| 505 | */ |
||
| 506 | public static function affected_rows() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns a list of all tables in the database. |
||
| 512 | * The table names will be in lower case. |
||
| 513 | * |
||
| 514 | * @return array The list of tables |
||
| 515 | */ |
||
| 516 | public static function table_list() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get a list of all the fields for the given table. |
||
| 522 | * Returns a map of field name => field spec. |
||
| 523 | * |
||
| 524 | * @param string $table The table name. |
||
| 525 | * @return array The list of fields |
||
| 526 | */ |
||
| 527 | public static function field_list($table) { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Enable supression of database messages. |
||
| 533 | */ |
||
| 534 | public static function quiet() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Show a message about database alteration |
||
| 540 | * |
||
| 541 | * @param string $message to display |
||
| 542 | * @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
||
| 543 | */ |
||
| 544 | public static function alteration_message($message, $type = "") { |
||
| 547 | |||
| 548 | } |
||
| 549 |
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: