Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Database implements DBInterface |
||
| 15 | { |
||
| 16 | protected $db; |
||
| 17 | public $sql; |
||
| 18 | private $key; |
||
| 19 | |||
| 20 | protected $logLocation; |
||
| 21 | public $logErrors = true; |
||
| 22 | public $logQueries = false; |
||
| 23 | public $displayErrors = false; |
||
| 24 | |||
| 25 | protected $database; |
||
| 26 | protected $cacheEnabled = false; |
||
| 27 | protected $cacheObj; |
||
| 28 | protected $cacheValue; |
||
| 29 | protected $modified = false; |
||
| 30 | |||
| 31 | private $query; |
||
| 32 | public $values = []; |
||
| 33 | private $prepare = []; |
||
| 34 | |||
| 35 | private static $connectors = array( |
||
| 36 | 'cubrid' => 'cubrid:host=%s;port=%d;dbname=%s', |
||
| 37 | 'dblib' => 'dblib:host=%s:%d;dbname=%s', |
||
| 38 | 'mssql' => 'sqlsrv:Server=%s,%d;Database=%s', |
||
| 39 | 'mysql' => 'mysql:host=%s;port=%d;dbname=%s', |
||
| 40 | 'pgsql' => 'pgsql:host=%s;port=%d;dbname=%s', |
||
| 41 | 'sqlite' => 'sqlite::memory:' |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Connect to database using PDO connection |
||
| 46 | * @param string $hostname This should be the host of the database e.g. 'localhost' |
||
| 47 | * @param string $username This should be the username for the chosen database |
||
| 48 | * @param string $password This should be the password for the chosen database |
||
| 49 | * @param string $database This should be the database that you wish to connect to |
||
| 50 | * @param string|false $backuphost If you have a replication server set up put the hostname or IP address to use if the primary server goes down |
||
| 51 | * @param object|false $cache If you want to cache the queries with Memcache(d)/Redis/APC/XCache This should be the object else set to false |
||
| 52 | * @param boolean $persistent If you want a persistent database connection set to true |
||
| 53 | * @param string $type The type of connection that you wish to make can be 'mysql', 'cubrid', 'dblib', 'mssql', 'odbc', 'pgsql, or 'sqlite' |
||
| 54 | * @param int $port This should be the port number of the MySQL database connection |
||
| 55 | 1 | * @param string|false $logLocation This should be where you wish the logs to be stored leave as false if default location is adequate |
|
| 56 | 1 | * @param array $options Add any additional PDO connection options here |
|
| 57 | */ |
||
| 58 | 1 | public function __construct($hostname, $username, $password, $database, $backuphost = false, $cache = false, $persistent = false, $type = 'mysql', $port = 3306, $logLocation = false, $options = []) |
|
| 73 | |||
| 74 | 1 | /** |
|
| 75 | 1 | * Closes the PDO database connection when Database object unset |
|
| 76 | 1 | */ |
|
| 77 | public function __destruct() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Connect to the database using PDO connection |
||
| 84 | * @param string $username This should be the username for the chosen database |
||
| 85 | * @param string $password This should be the password for the chosen database |
||
| 86 | * @param string $database This should be the database that you wish to connect to |
||
| 87 | * @param string $hostname The host for the database |
||
| 88 | 2 | * @param boolean $persistent If you want a persistent database connection set to true |
|
| 89 | 2 | * @param string $type The type of connection that you wish to make can be 'mysql', 'cubrid', 'dblib', 'mssql', 'pgsql, or 'sqlite' |
|
| 90 | 2 | * @param int $port The port number to connect to the MySQL server |
|
| 91 | 2 | * @param array $options Add any additional PDO connection options here |
|
| 92 | 2 | */ |
|
| 93 | 2 | protected function connectToServer($username, $password, $database, $hostname, $persistent = false, $type = 'mysql', $port = 3306, $options = []) |
|
| 110 | 1 | ||
| 111 | /** |
||
| 112 | * Enables the caching and set the caching object to the one provided |
||
| 113 | * @param object $caching This should be class of the type of caching you are using |
||
| 114 | */ |
||
| 115 | public function setCaching($caching) |
||
| 123 | 1 | ||
| 124 | 1 | /** |
|
| 125 | 1 | * This query function is used for more advanced SQL queries for which non of the other methods fit |
|
| 126 | 1 | * @param string $sql This should be the SQL query which you wish to run |
|
| 127 | * @param array $variables This should be an array of values to execute as the values in a prepared statement |
||
| 128 | * @return array|boolean Returns array of results for the query that has just been run if select or returns true and false if executed successfully or not |
||
| 129 | */ |
||
| 130 | public function query($sql, $variables = [], $cache = true) |
||
| 157 | |||
| 158 | 3 | /** |
|
| 159 | 3 | * Returns a single record for a select query for the chosen table |
|
| 160 | 3 | * @param string $table This should be the table you wish to select the values from |
|
| 161 | 3 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
|
| 162 | 3 | * @param string|array $fields This should be the records you wish to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
|
| 163 | 2 | * @param array|string $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') |
|
| 164 | 3 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
|
| 165 | * @return array Returns a single table record as the standard array when running SQL queries |
||
| 166 | 3 | */ |
|
| 167 | public function select($table, $where = [], $fields = '*', $order = [], $cache = true) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns a multidimensional array of the results from the selected table given the given parameters |
||
| 174 | * @param string $table This should be the table you wish to select the values from |
||
| 175 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
| 176 | * @param string|array $fields This should be the records you wish to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
| 177 | * @param array|string $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') |
||
| 178 | * @param integer|array $limit The number of results you want to return 0 is default and returns all results, else should be formatted either as a standard integer or as an array as the start and end values e.g. array(0 => 150) |
||
| 179 | 2 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
|
| 180 | 2 | * @return array Returns a multidimensional array with the chosen fields from the table |
|
| 181 | 2 | */ |
|
| 182 | 2 | public function selectAll($table, $where = [], $fields = '*', $order = [], $limit = 0, $cache = true) |
|
| 198 | |||
| 199 | 2 | /** |
|
| 200 | 2 | * Returns a single column value for a given query |
|
| 201 | 2 | * @param string $table This should be the table you wish to select the values from |
|
| 202 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
| 203 | * @param array $fields This should be the records you wish to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
| 204 | * @param int $colNum This should be the column number you wish to get (starts at 0) |
||
| 205 | * @param array $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') so it can be done in both directions |
||
| 206 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
||
| 207 | * @return mixed If a result is found will return the value of the column given else will return false |
||
| 208 | */ |
||
| 209 | public function fetchColumn($table, $where = [], $fields = '*', $colNum = 0, $order = [], $cache = true) |
||
| 222 | |||
| 223 | /** |
||
| 224 | 2 | * Inserts into database using the prepared PDO statements |
|
| 225 | 2 | * @param string $table This should be the table you wish to insert the values into |
|
| 226 | 2 | * @param array $records This should be the field names and values in the format of array('fieldname' => 'value', 'fieldname2' => 'value2', etc.) |
|
| 227 | 2 | * @return boolean If data is inserted returns true else returns false |
|
| 228 | */ |
||
| 229 | public function insert($table, $records) |
||
| 237 | 1 | ||
| 238 | 1 | /** |
|
| 239 | 1 | * Updates values in a database using the provide variables |
|
| 240 | * @param string $table This should be the table you wish to update the values for |
||
| 241 | 1 | * @param array $records This should be the field names and new values in the format of array('fieldname' => 'newvalue', 'fieldname2' => 'newvalue2', etc.) |
|
| 242 | 1 | * @param array $where Should be the field names and values you wish to update in the form of an array e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
|
| 243 | 1 | * @param int $limit The number of results you want to return 0 is default and will update all results that match the query, else should be formatted as a standard integer |
|
| 244 | 1 | * @return boolean Returns true if update is successful else returns false |
|
| 245 | */ |
||
| 246 | 1 | public function update($table, $records, $where = [], $limit = 0) |
|
| 252 | |||
| 253 | /** |
||
| 254 | 1 | * Deletes records from the given table based on the variables given |
|
| 255 | * @param string $table This should be the table you wish to delete the records from |
||
| 256 | 1 | * @param array $where This should be an array of for the where statement |
|
| 257 | 1 | * @param int $limit The number of results you want to return 0 is default and will delete all results that match the query, else should be formatted as a standard integer |
|
| 258 | */ |
||
| 259 | public function delete($table, $where, $limit = 0) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Count the number of return results |
||
| 268 | * @param string $table The table you wish to count the result of |
||
| 269 | 8 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
|
| 270 | 8 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
|
| 271 | 8 | * @return int Returns the number of results |
|
| 272 | */ |
||
| 273 | public function count($table, $where = [], $cache = true) |
||
| 287 | |||
| 288 | /** |
||
| 289 | 1 | * Truncates a given table from the selected database so there are no values in the table |
|
| 290 | 1 | * @param string $table This should be the table you wish to truncate |
|
| 291 | * @return boolean If the table is emptied returns true else returns false |
||
| 292 | */ |
||
| 293 | public function truncate($table) |
||
| 303 | |||
| 304 | 1 | /** |
|
| 305 | 1 | * Returns the number of rows for the last query sent |
|
| 306 | * @return int Returns the number of rows for the last query |
||
| 307 | */ |
||
| 308 | public function numRows() |
||
| 315 | 1 | ||
| 316 | /** |
||
| 317 | 1 | * Returns the number of rows for the last query sent (Looks a the numRows() function just added incase of habbit) |
|
| 318 | 1 | * @return int Returns the number of rows for the last query |
|
| 319 | 1 | */ |
|
| 320 | public function rowCount() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the ID of the last record last inserted |
||
| 327 | * @param string $name This should be the name of the sequence object you wish to retrieve |
||
| 328 | 4 | * @return int|string Returns the last inserted ID of the last insert item if $name is null else returns string with sequenced object |
|
| 329 | 4 | */ |
|
| 330 | 4 | public function lastInsertId($name = null) |
|
| 334 | |||
| 335 | 4 | /** |
|
| 336 | * Checks to see if a connection has been made to the server |
||
| 337 | * @return boolean |
||
| 338 | 4 | */ |
|
| 339 | public function isConnected() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the server version information |
||
| 346 | */ |
||
| 347 | public function serverVersion() |
||
| 351 | |||
| 352 | /** |
||
| 353 | 1 | * Sets the location of the log files |
|
| 354 | 1 | * @param string $location This should be where you wish the logs to be stored |
|
| 355 | 1 | * @return $this |
|
| 356 | */ |
||
| 357 | public function setLogLocation($location = false) |
||
| 368 | 1 | ||
| 369 | 1 | /** |
|
| 370 | * Displays the error massage which occurs |
||
| 371 | 1 | * @param \Exception $error This should be an instance of Exception |
|
| 372 | */ |
||
| 373 | 5 | private function error($error) |
|
| 383 | |||
| 384 | 13 | /** |
|
| 385 | 13 | * Writes all queries to a log file |
|
| 386 | 13 | */ |
|
| 387 | public function writeQueryToLog() |
||
| 394 | 13 | ||
| 395 | /** |
||
| 396 | 3 | * Closes the PDO database connection by setting the connection to NULL |
|
| 397 | 3 | */ |
|
| 398 | 3 | public function closeDatabase() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Build the SQL query but doesn't execute it |
||
| 405 | * @param string $table This should be the table you wish to select the values from |
||
| 406 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
| 407 | * @param string|array $fields This should be the records you wish to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
| 408 | 9 | * @param array|string $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') so it can be done in both directions |
|
| 409 | 9 | * @param integer|array $limit The number of results you want to return 0 is default and returns all results, else should be formatted either as a standard integer or as an array as the start and end values e.g. array(0 => 150) |
|
| 410 | 8 | */ |
|
| 411 | 8 | protected function buildSelectQuery($table, $where = [], $fields = '*', $order = [], $limit = 0) |
|
| 426 | 1 | ||
| 427 | 1 | /** |
|
| 428 | 1 | * Execute the current query if no cache value is available |
|
| 429 | 1 | * @param boolean $cache If the cache should be checked for the checked for the values of the query set to true else set to false |
|
| 430 | 1 | * @return mixed If a cached value exists will be returned else if cache is not checked and query is executed will not return anything |
|
| 431 | 1 | */ |
|
| 432 | protected function executeQuery($cache = true) |
||
| 451 | 4 | ||
| 452 | 4 | /** |
|
| 453 | * This outputs the SQL where query based on a given array |
||
| 454 | 4 | * @param array $where This should be an array that you wish to create the where query for in the for array('field1' => 'test') or array('field1' => array('>=', 0)) |
|
| 455 | 4 | * @return string|false If the where query is an array will return the where string and set the values else returns false if no array sent |
|
| 456 | 4 | */ |
|
| 457 | 4 | public function where($where) |
|
| 470 | |||
| 471 | /** |
||
| 472 | 1 | * Sets the order sting for the SQL query based on an array or string |
|
| 473 | 1 | * @param array|string $order This should be either set to array('fieldname' => 'ASC/DESC') or RAND() |
|
| 474 | 1 | * @return string|false If the SQL query has an valid order by will return a string else returns false |
|
| 475 | 1 | */ |
|
| 476 | public function orderBy($order) |
||
| 493 | |||
| 494 | 2 | /** |
|
| 495 | * Build the field list for the query |
||
| 496 | * @param array $records This should be an array listing all of the fields |
||
| 497 | * @param boolean $insert If this is an insert statement should be set to true to create the correct amount of queries for the prepared statement |
||
| 498 | * @return string The fields list will be returned as a string to insert into the SQL query |
||
| 499 | */ |
||
| 500 | public function fields($records, $insert = false) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns the limit SQL for the current query as a string |
||
| 518 | * @param integer|array $limit This should either be set as an integer or should be set as an array with a start and end value |
||
| 519 | * @return string|false Will return the LIMIT string for the current query if it is valid else returns false |
||
| 520 | */ |
||
| 521 | public function limit($limit = 0) |
||
| 532 | |||
| 533 | 1 | ||
| 534 | 1 | /** |
|
| 535 | 1 | * Set the cache with a key and value |
|
| 536 | * @param string $key The unique key to store the value against |
||
| 537 | 1 | * @param mixed $value The value of the MYSQL query |
|
| 538 | */ |
||
| 539 | 2 | public function setCache($key, $value) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Get the results for a given key |
||
| 548 | * @param string $key The unique key to check for stored variables |
||
| 549 | * @return mixed Returned the cached results from |
||
| 550 | 12 | */ |
|
| 551 | 12 | public function getCache($key) |
|
| 559 | |||
| 560 | 12 | /** |
|
| 561 | * Clears the cache |
||
| 562 | */ |
||
| 563 | public function flushDB() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Format the where queries and set the prepared values |
||
| 570 | * @param string $field This should be the field name in the database |
||
| 571 | * @param mixed $value This should be the value which should either be a string or an array if it contains an operator |
||
| 572 | * @return string This should be the string to add to the SQL query |
||
| 573 | */ |
||
| 574 | protected function formatValues($field, $value) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Band values to use in the query |
||
| 598 | * @param array $values This should be the values being used in the query |
||
| 599 | */ |
||
| 600 | protected function bindValues($values) |
||
| 619 | } |
||
| 620 |