Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 13 | class DB |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | public $query_count = 0; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $exit_on_error = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $echo_on_error = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $css_mysql_box_border = '3px solid red'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $css_mysql_box_bg = '#FFCCCC'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \mysqli |
||
| 43 | */ |
||
| 44 | protected $link = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $connected = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $mysqlDefaultTimeFunctions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $logger_class_name; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | * |
||
| 64 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 65 | */ |
||
| 66 | private $logger_level; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $hostname = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $username = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $password = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $database = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | private $port = 3306; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $charset = 'utf8'; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $socket = ''; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $_errors = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $session_to_db = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | private $_in_transaction = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * __construct() |
||
| 120 | * |
||
| 121 | * @param string $hostname |
||
| 122 | * @param string $username |
||
| 123 | * @param string $password |
||
| 124 | * @param string $database |
||
| 125 | * @param int $port |
||
| 126 | * @param string $charset |
||
| 127 | * @param boolean|string $exit_on_error use a empty string "" or false to disable it |
||
| 128 | * @param boolean|string $echo_on_error use a empty string "" or false to disable it |
||
| 129 | * @param string $logger_class_name |
||
| 130 | * @param string $logger_level 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 131 | * @param boolean|string $session_to_db use a empty string "" or false to disable it |
||
| 132 | */ |
||
| 133 | 10 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $session_to_db) |
|
| 134 | { |
||
| 135 | 10 | $this->connected = false; |
|
| 136 | |||
| 137 | 10 | $this->_loadConfig( |
|
| 138 | 10 | $hostname, |
|
| 139 | 10 | $username, |
|
| 140 | 10 | $password, |
|
| 141 | 10 | $database, |
|
| 142 | 10 | $port, |
|
| 143 | 10 | $charset, |
|
| 144 | 10 | $exit_on_error, |
|
| 145 | 10 | $echo_on_error, |
|
| 146 | 10 | $logger_class_name, |
|
| 147 | 10 | $logger_level, |
|
| 148 | $session_to_db |
||
| 149 | 10 | ); |
|
| 150 | |||
| 151 | 7 | $this->connect(); |
|
| 152 | |||
| 153 | 4 | $this->mysqlDefaultTimeFunctions = array( |
|
| 154 | // Returns the current date |
||
| 155 | 4 | 'CURDATE()', |
|
| 156 | // CURRENT_DATE | Synonyms for CURDATE() |
||
| 157 | 4 | 'CURRENT_DATE()', |
|
| 158 | // CURRENT_TIME | Synonyms for CURTIME() |
||
| 159 | 4 | 'CURRENT_TIME()', |
|
| 160 | // CURRENT_TIMESTAMP | Synonyms for NOW() |
||
| 161 | 4 | 'CURRENT_TIMESTAMP()', |
|
| 162 | // Returns the current time |
||
| 163 | 4 | 'CURTIME()', |
|
| 164 | // Synonym for NOW() |
||
| 165 | 4 | 'LOCALTIME()', |
|
| 166 | // Synonym for NOW() |
||
| 167 | 4 | 'LOCALTIMESTAMP()', |
|
| 168 | // Returns the current date and time |
||
| 169 | 4 | 'NOW()', |
|
| 170 | // Returns the time at which the function executes |
||
| 171 | 4 | 'SYSDATE()', |
|
| 172 | // Returns a UNIX timestamp |
||
| 173 | 4 | 'UNIX_TIMESTAMP()', |
|
| 174 | // Returns the current UTC date |
||
| 175 | 4 | 'UTC_DATE()', |
|
| 176 | // Returns the current UTC time |
||
| 177 | 4 | 'UTC_TIME()', |
|
| 178 | // Returns the current UTC date and time |
||
| 179 | 4 | 'UTC_TIMESTAMP()', |
|
| 180 | ); |
||
| 181 | 4 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * load the config |
||
| 185 | * |
||
| 186 | * @param string $hostname |
||
| 187 | * @param string $username |
||
| 188 | * @param string $password |
||
| 189 | * @param string $database |
||
| 190 | * @param int $port |
||
| 191 | * @param string $charset |
||
| 192 | * @param boolean|string $exit_on_error use a empty string "" or false to disable it |
||
| 193 | * @param boolean|string $echo_on_error use a empty string "" or false to disable it |
||
| 194 | * @param string $logger_class_name |
||
| 195 | * @param string $logger_level |
||
| 196 | * @param boolean|string $session_to_db use a empty string "" or false to disable it |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | 10 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $session_to_db) |
|
| 201 | { |
||
| 202 | 10 | $this->hostname = (string)$hostname; |
|
| 203 | 10 | $this->username = (string)$username; |
|
| 204 | 10 | $this->password = (string)$password; |
|
| 205 | 10 | $this->database = (string)$database; |
|
| 206 | |||
| 207 | 10 | if ($charset) { |
|
| 208 | 4 | $this->charset = (string)$charset; |
|
| 209 | 4 | } |
|
| 210 | |||
| 211 | 10 | if ($port) { |
|
| 212 | 4 | $this->port = (int)$port; |
|
| 213 | 4 | } else { |
|
| 214 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 215 | 7 | $this->port = @ini_get('mysqli.default_port'); |
|
| 216 | } |
||
| 217 | |||
| 218 | 10 | if (!$this->socket) { |
|
| 219 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 220 | 10 | $this->socket = @ini_get('mysqli.default_socket'); |
|
| 221 | 10 | } |
|
| 222 | |||
| 223 | 10 | if ($exit_on_error === true || $exit_on_error === false) { |
|
| 224 | 10 | $this->exit_on_error = (boolean)$exit_on_error; |
|
| 225 | 10 | } |
|
| 226 | |||
| 227 | 10 | if ($echo_on_error === true || $echo_on_error === false) { |
|
| 228 | 10 | $this->echo_on_error = (boolean)$echo_on_error; |
|
| 229 | 10 | } |
|
| 230 | |||
| 231 | 10 | $this->logger_class_name = (string)$logger_class_name; |
|
| 232 | 10 | $this->logger_level = (string)$logger_level; |
|
| 233 | |||
| 234 | 10 | $this->session_to_db = (boolean)$session_to_db; |
|
| 235 | |||
| 236 | 10 | return $this->showConfigError(); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * show config error and throw a exception |
||
| 241 | */ |
||
| 242 | 10 | public function showConfigError() |
|
| 243 | { |
||
| 244 | |||
| 245 | if ( |
||
| 246 | 10 | !$this->hostname |
|
| 247 | 10 | || |
|
| 248 | 9 | !$this->username |
|
| 249 | 9 | || |
|
| 250 | 8 | !$this->database |
|
| 251 | 10 | ) { |
|
| 252 | |||
| 253 | 3 | if (!$this->hostname) { |
|
| 254 | 1 | throw new \Exception('no-sql-hostname'); |
|
| 255 | } |
||
| 256 | |||
| 257 | 2 | if (!$this->username) { |
|
| 258 | 1 | throw new \Exception('no-sql-username'); |
|
| 259 | } |
||
| 260 | |||
| 261 | 1 | if (!$this->database) { |
|
| 262 | 1 | throw new \Exception('no-sql-database'); |
|
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | 7 | return true; |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * connect |
||
| 271 | * |
||
| 272 | * @return boolean |
||
| 273 | */ |
||
| 274 | 8 | public function connect() |
|
| 275 | { |
||
| 276 | 8 | if ($this->isReady()) { |
|
| 277 | 1 | return true; |
|
| 278 | } |
||
| 279 | |||
| 280 | 8 | mysqli_report(MYSQLI_REPORT_STRICT); |
|
| 281 | try { |
||
| 282 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 283 | 8 | $this->link = @mysqli_connect( |
|
| 284 | 8 | $this->hostname, |
|
| 285 | 8 | $this->username, |
|
| 286 | 8 | $this->password, |
|
| 287 | 8 | $this->database, |
|
| 288 | 8 | $this->port, |
|
| 289 | 8 | $this->socket |
|
| 290 | 8 | ); |
|
| 291 | 8 | } catch (\Exception $e) { |
|
| 292 | 3 | $this->_displayError('Error connecting to mysql server: ' . $e->getMessage(), true); |
|
| 293 | } |
||
| 294 | 5 | mysqli_report(MYSQLI_REPORT_OFF); |
|
| 295 | |||
| 296 | 5 | if (!$this->link) { |
|
| 297 | $this->_displayError('Error connecting to mysql server: ' . mysqli_connect_error(), true); |
||
| 298 | } else { |
||
| 299 | 5 | $this->set_charset($this->charset); |
|
| 300 | 5 | $this->connected = true; |
|
| 301 | } |
||
| 302 | |||
| 303 | 5 | return $this->isReady(); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * check if db-connection is ready |
||
| 308 | * |
||
| 309 | * @return boolean |
||
| 310 | */ |
||
| 311 | 29 | public function isReady() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * _displayError |
||
| 318 | * |
||
| 319 | * @param string $e |
||
| 320 | * @param null|boolean $force_exception_after_error |
||
| 321 | * |
||
| 322 | * @throws \Exception |
||
| 323 | */ |
||
| 324 | 18 | private function _displayError($e, $force_exception_after_error = null) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * try to get the file & line from the current sql-query |
||
| 371 | * |
||
| 372 | * @return array will return array['file'] and array['line'] |
||
| 373 | */ |
||
| 374 | 19 | private function getFileAndLineFromSql() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * wrapper for a "Logger"-Class |
||
| 415 | * |
||
| 416 | * @param string[] $log [method, text, type] e.g.: array('error', 'this is a error', 'sql') |
||
| 417 | */ |
||
| 418 | 20 | private function logger($log) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * check for developer |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | 18 | private function checkForDev() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * execute a sql-query and |
||
| 483 | * return the result-array for select-statements |
||
| 484 | * |
||
| 485 | * ----------------------- |
||
| 486 | * |
||
| 487 | * e.g.: |
||
| 488 | * $retcode = DB::qry("UPDATE user_extension |
||
| 489 | * SET |
||
| 490 | * user_name='?' |
||
| 491 | * WHERE user_uid_fk='?' |
||
| 492 | * ", |
||
| 493 | * $userName, |
||
| 494 | * (int)$uid |
||
| 495 | * ); |
||
| 496 | * |
||
| 497 | * ----------------------- |
||
| 498 | * |
||
| 499 | * @param $query |
||
| 500 | * |
||
| 501 | * @return array|bool|int|\voku\db\Result |
||
| 502 | * @deprecated |
||
| 503 | * @throws \Exception |
||
| 504 | */ |
||
| 505 | public static function qry($query) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * getInstance() |
||
| 538 | * |
||
| 539 | * @param string $hostname |
||
| 540 | * @param string $username |
||
| 541 | * @param string $password |
||
| 542 | * @param string $database |
||
| 543 | * @param string $port default is '3306' |
||
| 544 | * @param string $charset default is 'utf8', but if you need 4-byte chars, then your tables need |
||
| 545 | * the 'utf8mb4'-charset |
||
| 546 | * @param bool|string $exit_on_error use a empty string "" or false to disable it |
||
| 547 | * @param bool|string $echo_on_error use a empty string "" or false to disable it |
||
| 548 | * @param string $logger_class_name |
||
| 549 | * @param string $logger_level |
||
| 550 | * @param bool|string $session_to_db use a empty string "" or false to disable it |
||
| 551 | * |
||
| 552 | * @return \voku\db\DB |
||
| 553 | */ |
||
| 554 | 39 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $session_to_db = '') |
|
| 600 | |||
| 601 | /** |
||
| 602 | * run a sql-query |
||
| 603 | * |
||
| 604 | * @param string $sql sql-query string |
||
| 605 | * |
||
| 606 | * @param array|boolean $params a "array" of sql-query-parameters |
||
| 607 | * "false" if you don't need any parameter |
||
| 608 | * |
||
| 609 | * @return bool|int|Result "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 610 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 611 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 612 | * "true" by e.g. "DROP"-queries<br /> |
||
| 613 | * "false" on error |
||
| 614 | * |
||
| 615 | * @throws \Exception |
||
| 616 | */ |
||
| 617 | 22 | public function query($sql = '', $params = false) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * _parseQueryParams |
||
| 687 | * |
||
| 688 | * @param string $sql |
||
| 689 | * @param array $params |
||
| 690 | * |
||
| 691 | * @return string |
||
| 692 | */ |
||
| 693 | 1 | private function _parseQueryParams($sql, array $params) |
|
| 715 | |||
| 716 | /** |
||
| 717 | * secure |
||
| 718 | * |
||
| 719 | * @param mixed $var |
||
| 720 | * |
||
| 721 | * @return string | null |
||
| 722 | */ |
||
| 723 | 14 | public function secure($var) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * escape |
||
| 752 | * |
||
| 753 | * @param array|float|int|string|boolean $var boolean: convert into "integer"<br /> |
||
| 754 | * int: convert into "integer"<br /> |
||
| 755 | * float: convert into "float" and replace "," with "."<br /> |
||
| 756 | * array: run escape() for every key => value<br /> |
||
| 757 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 758 | * @param bool $stripe_non_utf8 |
||
| 759 | * @param bool $html_entity_decode |
||
| 760 | * @param bool $array_to_string |
||
| 761 | * |
||
| 762 | * @return array|bool|float|int|string |
||
| 763 | */ |
||
| 764 | 17 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = true, $array_to_string = false) |
|
| 823 | |||
| 824 | /** |
||
| 825 | * getLink |
||
| 826 | * |
||
| 827 | * @return \mysqli |
||
| 828 | */ |
||
| 829 | 17 | public function getLink() |
|
| 833 | |||
| 834 | /** |
||
| 835 | * _logQuery |
||
| 836 | * |
||
| 837 | * @param String $sql sql-query |
||
| 838 | * @param int $duration |
||
| 839 | * @param int $results result counter |
||
| 840 | * |
||
| 841 | * @return bool |
||
| 842 | */ |
||
| 843 | 21 | private function _logQuery($sql, $duration, $results) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * insert_id |
||
| 876 | * |
||
| 877 | * @return int|string |
||
| 878 | */ |
||
| 879 | 15 | public function insert_id() |
|
| 883 | |||
| 884 | /** |
||
| 885 | * affected_rows |
||
| 886 | * |
||
| 887 | * @return int |
||
| 888 | */ |
||
| 889 | 6 | public function affected_rows() |
|
| 893 | |||
| 894 | /** |
||
| 895 | * query error-handling |
||
| 896 | * |
||
| 897 | * @param string $errorMsg |
||
| 898 | * @param string $sql |
||
| 899 | * @param array|bool $sqlParams false if there wasn't any parameter |
||
| 900 | * |
||
| 901 | * @throws \Exception |
||
| 902 | */ |
||
| 903 | 9 | protected function queryErrorHandling($errorMsg, $sql, $sqlParams = false) |
|
| 929 | |||
| 930 | /** |
||
| 931 | * send a error mail to the admin / dev |
||
| 932 | * |
||
| 933 | * @param string $subject |
||
| 934 | * @param string $htmlBody |
||
| 935 | * @param int $priority |
||
| 936 | */ |
||
| 937 | 9 | private function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 968 | |||
| 969 | /** |
||
| 970 | * reconnect |
||
| 971 | * |
||
| 972 | * @param bool $checkViaPing |
||
| 973 | * |
||
| 974 | * @return bool |
||
| 975 | */ |
||
| 976 | 2 | public function reconnect($checkViaPing = false) |
|
| 991 | |||
| 992 | /** |
||
| 993 | * ping |
||
| 994 | * |
||
| 995 | * @return boolean |
||
| 996 | */ |
||
| 997 | 3 | public function ping() |
|
| 1010 | |||
| 1011 | /** |
||
| 1012 | * can handel select/insert/update/delete queries |
||
| 1013 | * |
||
| 1014 | * @param string $query sql-query |
||
| 1015 | * @param bool $useCache use cache? |
||
| 1016 | * @param int $cacheTTL cache-ttl in seconds |
||
| 1017 | * |
||
| 1018 | * @return bool|int|array "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1019 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1020 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1021 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1022 | * "false" on error |
||
| 1023 | * |
||
| 1024 | */ |
||
| 1025 | 3 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600) |
|
| 1069 | |||
| 1070 | /** |
||
| 1071 | * get charset |
||
| 1072 | * |
||
| 1073 | * @return string |
||
| 1074 | */ |
||
| 1075 | 1 | public function get_charset() |
|
| 1079 | |||
| 1080 | /** |
||
| 1081 | * set charset |
||
| 1082 | * |
||
| 1083 | * @param string $charset |
||
| 1084 | * |
||
| 1085 | * @return bool |
||
| 1086 | */ |
||
| 1087 | 6 | public function set_charset($charset) |
|
| 1099 | |||
| 1100 | /** |
||
| 1101 | * __wakeup |
||
| 1102 | * |
||
| 1103 | * @return void |
||
| 1104 | */ |
||
| 1105 | 1 | public function __wakeup() |
|
| 1109 | |||
| 1110 | /** |
||
| 1111 | * get the names of all tables |
||
| 1112 | * |
||
| 1113 | * @return array |
||
| 1114 | */ |
||
| 1115 | 1 | public function getAllTables() |
|
| 1122 | |||
| 1123 | /** |
||
| 1124 | * run a sql-multi-query |
||
| 1125 | * |
||
| 1126 | * @param string $sql |
||
| 1127 | * |
||
| 1128 | * @return bool|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1129 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1130 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1131 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1132 | * |
||
| 1133 | * @throws \Exception |
||
| 1134 | */ |
||
| 1135 | 1 | public function multi_query($sql) |
|
| 1203 | |||
| 1204 | /** |
||
| 1205 | * alias for "beginTransaction()" |
||
| 1206 | */ |
||
| 1207 | 1 | public function startTransaction() |
|
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Begins a transaction, by turning off auto commit |
||
| 1214 | * |
||
| 1215 | * @return boolean this will return true or false indicating success of transaction |
||
| 1216 | */ |
||
| 1217 | 4 | public function beginTransaction() |
|
| 1237 | |||
| 1238 | /** |
||
| 1239 | * clear errors |
||
| 1240 | * |
||
| 1241 | * @return bool |
||
| 1242 | */ |
||
| 1243 | 4 | public function clearErrors() |
|
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Check if in transaction |
||
| 1252 | * |
||
| 1253 | * @return boolean |
||
| 1254 | */ |
||
| 1255 | 4 | public function inTransaction() |
|
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Ends a transaction and commits if no errors, then ends autocommit |
||
| 1262 | * |
||
| 1263 | * @return boolean this will return true or false indicating success of transactions |
||
| 1264 | */ |
||
| 1265 | 2 | public function endTransaction() |
|
| 1281 | |||
| 1282 | /** |
||
| 1283 | * get all errors |
||
| 1284 | * |
||
| 1285 | * @return array false === on errors |
||
| 1286 | */ |
||
| 1287 | 2 | public function errors() |
|
| 1291 | |||
| 1292 | /** |
||
| 1293 | * rollback in a transaction |
||
| 1294 | */ |
||
| 1295 | 2 | public function rollback() |
|
| 1308 | |||
| 1309 | /** |
||
| 1310 | * insert |
||
| 1311 | * |
||
| 1312 | * @param string $table |
||
| 1313 | * @param array $data |
||
| 1314 | * |
||
| 1315 | * @return false|int false on error |
||
| 1316 | */ |
||
| 1317 | 14 | public function insert($table, $data = array()) |
|
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Parses arrays with value pairs and generates SQL to use in queries |
||
| 1342 | * |
||
| 1343 | * @param array $arrayPair |
||
| 1344 | * @param string $glue this is the separator |
||
| 1345 | * |
||
| 1346 | * @return string |
||
| 1347 | */ |
||
| 1348 | 12 | private function _parseArrayPair($arrayPair, $glue = ',') |
|
| 1457 | |||
| 1458 | /** |
||
| 1459 | * Quote && Escape e.g. a table name string |
||
| 1460 | * |
||
| 1461 | * @param string $str |
||
| 1462 | * |
||
| 1463 | * @return string |
||
| 1464 | */ |
||
| 1465 | 14 | public function quote_string($str) |
|
| 1469 | |||
| 1470 | /** |
||
| 1471 | * get errors |
||
| 1472 | * |
||
| 1473 | * @return array |
||
| 1474 | */ |
||
| 1475 | 1 | public function getErrors() |
|
| 1479 | |||
| 1480 | /** |
||
| 1481 | * replace |
||
| 1482 | * |
||
| 1483 | * @param string $table |
||
| 1484 | * @param array $data |
||
| 1485 | * |
||
| 1486 | * @return false|int false on error |
||
| 1487 | */ |
||
| 1488 | 1 | public function replace($table, $data = array()) |
|
| 1525 | |||
| 1526 | /** |
||
| 1527 | * update |
||
| 1528 | * |
||
| 1529 | * @param string $table |
||
| 1530 | * @param array $data |
||
| 1531 | * @param array|string $where |
||
| 1532 | * |
||
| 1533 | * @return false|int false on error |
||
| 1534 | */ |
||
| 1535 | 5 | public function update($table, $data = array(), $where = '1=1') |
|
| 1565 | |||
| 1566 | /** |
||
| 1567 | * delete |
||
| 1568 | * |
||
| 1569 | * @param string $table |
||
| 1570 | * @param string|array $where |
||
| 1571 | * |
||
| 1572 | * @return false|int false on error |
||
| 1573 | */ |
||
| 1574 | 1 | View Code Duplication | public function delete($table, $where) |
| 1597 | |||
| 1598 | /** |
||
| 1599 | * select |
||
| 1600 | * |
||
| 1601 | * @param string $table |
||
| 1602 | * @param string|array $where |
||
| 1603 | * |
||
| 1604 | * @return false|Result false on error |
||
| 1605 | */ |
||
| 1606 | 13 | View Code Duplication | public function select($table, $where = '1=1') |
| 1627 | |||
| 1628 | /** |
||
| 1629 | * get the last error |
||
| 1630 | * |
||
| 1631 | * @return string false on error |
||
| 1632 | */ |
||
| 1633 | 1 | public function lastError() |
|
| 1637 | |||
| 1638 | /** |
||
| 1639 | * __destruct |
||
| 1640 | * |
||
| 1641 | */ |
||
| 1642 | 1 | public function __destruct() |
|
| 1649 | |||
| 1650 | /** |
||
| 1651 | * close |
||
| 1652 | */ |
||
| 1653 | 3 | public function close() |
|
| 1661 | |||
| 1662 | /** |
||
| 1663 | * prevent the instance from being cloned |
||
| 1664 | * |
||
| 1665 | * @return void |
||
| 1666 | */ |
||
| 1667 | private function __clone() |
||
| 1670 | |||
| 1671 | } |
||
| 1672 |