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 |
||
| 16 | final class DB |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | public $query_count = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \mysqli |
||
| 26 | */ |
||
| 27 | private $link; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $connected = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $mysqlDefaultTimeFunctions; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $hostname = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $username = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $password = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $database = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $port = 3306; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $charset = 'utf8'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $socket = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $session_to_db = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $_in_transaction = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $_convert_null_to_empty_string = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $_ssl = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The path name to the key file |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $_clientkey; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The path name to the certificate file |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $_clientcert; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The path name to the certificate authority file |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | private $_cacert; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Debug |
||
| 117 | */ |
||
| 118 | private $_debug; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * __construct() |
||
| 122 | * |
||
| 123 | * @param string $hostname |
||
| 124 | * @param string $username |
||
| 125 | * @param string $password |
||
| 126 | * @param string $database |
||
| 127 | * @param int $port |
||
| 128 | * @param string $charset |
||
| 129 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 130 | * Use a empty string "" or false to disable it.</p> |
||
| 131 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 132 | * Use a empty string "" or false to disable it.</p> |
||
| 133 | * @param string $logger_class_name |
||
| 134 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 135 | * @param array $extra_config <p> |
||
| 136 | * 'session_to_db' => false|true<br> |
||
| 137 | * 'socket' => 'string (path)'<br> |
||
| 138 | * 'ssl' => 'bool'<br> |
||
| 139 | * 'clientkey' => 'string (path)'<br> |
||
| 140 | * 'clientcert' => 'string (path)'<br> |
||
| 141 | * 'cacert' => 'string (path)'<br> |
||
| 142 | * </p> |
||
| 143 | */ |
||
| 144 | 11 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Prevent the instance from being cloned. |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | private function __clone() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * __destruct |
||
| 207 | * |
||
| 208 | */ |
||
| 209 | public function __destruct() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param null|string $sql |
||
| 219 | * @param array $bindings |
||
| 220 | * |
||
| 221 | * @return bool|int|Result|DB <p> |
||
| 222 | * "DB" by "$sql" === null<br /> |
||
| 223 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 224 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 225 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 226 | * "true" by e.g. "DROP"-queries<br /> |
||
| 227 | * "false" on error |
||
| 228 | * </p> |
||
| 229 | */ |
||
| 230 | 2 | public function __invoke($sql = null, array $bindings = array()) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * __wakeup |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 2 | public function __wakeup() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Load the config from the constructor. |
||
| 247 | * |
||
| 248 | * @param string $hostname |
||
| 249 | * @param string $username |
||
| 250 | * @param string $password |
||
| 251 | * @param string $database |
||
| 252 | * @param int|string $port <p>default is (int)3306</p> |
||
| 253 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 254 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 255 | * Use a empty string "" or false to disable it.</p> |
||
| 256 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 257 | * Use a empty string "" or false to disable it.</p> |
||
| 258 | * @param string $logger_class_name |
||
| 259 | * @param string $logger_level |
||
| 260 | * @param array $extra_config <p> |
||
| 261 | * 'session_to_db' => false|true<br> |
||
| 262 | * 'socket' => 'string (path)'<br> |
||
| 263 | * 'ssl' => 'bool'<br> |
||
| 264 | * 'clientkey' => 'string (path)'<br> |
||
| 265 | * 'clientcert' => 'string (path)'<br> |
||
| 266 | * 'cacert' => 'string (path)'<br> |
||
| 267 | * </p> |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 11 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 347 | * |
||
| 348 | * @param array $arrayPair |
||
| 349 | * @param string $glue <p>This is the separator.</p> |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | * |
||
| 353 | * @internal |
||
| 354 | */ |
||
| 355 | 30 | public function _parseArrayPair($arrayPair, $glue = ',') |
|
| 501 | |||
| 502 | /** |
||
| 503 | * _parseQueryParams |
||
| 504 | * |
||
| 505 | * @param string $sql |
||
| 506 | * @param array $params |
||
| 507 | * |
||
| 508 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 509 | */ |
||
| 510 | 12 | private function _parseQueryParams($sql, array $params) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 544 | * |
||
| 545 | * @return int |
||
| 546 | */ |
||
| 547 | 12 | public function affected_rows() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Begins a transaction, by turning off auto commit. |
||
| 554 | * |
||
| 555 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 556 | */ |
||
| 557 | 6 | View Code Duplication | public function beginTransaction() |
| 574 | |||
| 575 | /** |
||
| 576 | * Clear the errors in "_debug->_errors". |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | 6 | public function clearErrors() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Closes a previously opened database connection. |
||
| 587 | */ |
||
| 588 | 2 | public function close() |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Commits the current transaction and end the transaction. |
||
| 599 | * |
||
| 600 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 601 | */ |
||
| 602 | 2 | View Code Duplication | public function commit() |
| 616 | |||
| 617 | /** |
||
| 618 | * Open a new connection to the MySQL server. |
||
| 619 | * |
||
| 620 | * @return bool |
||
| 621 | * |
||
| 622 | * @throws DBConnectException |
||
| 623 | */ |
||
| 624 | 10 | public function connect() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Execute a "delete"-query. |
||
| 702 | * |
||
| 703 | * @param string $table |
||
| 704 | * @param string|array $where |
||
| 705 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 706 | * |
||
| 707 | * @return false|int <p>false on error</p> |
||
| 708 | * |
||
| 709 | * * @throws QueryException |
||
| 710 | */ |
||
| 711 | 2 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 738 | |||
| 739 | /** |
||
| 740 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 741 | * |
||
| 742 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 743 | */ |
||
| 744 | 4 | View Code Duplication | public function endTransaction() |
| 764 | |||
| 765 | /** |
||
| 766 | * Get all errors from "$this->_errors". |
||
| 767 | * |
||
| 768 | * @return array|false <p>false === on errors</p> |
||
| 769 | */ |
||
| 770 | 4 | public function errors() |
|
| 776 | |||
| 777 | /** |
||
| 778 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 779 | * |
||
| 780 | * @param mixed $sql <p>The SQL string.</p> |
||
| 781 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 782 | * |
||
| 783 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 784 | */ |
||
| 785 | 13 | public function _parseQueryParamsByName($sql, array $params = array()) |
|
| 786 | { |
||
| 787 | // is there anything to parse? |
||
| 788 | View Code Duplication | if ( |
|
| 789 | 13 | strpos($sql, ':') === false |
|
| 790 | 13 | || |
|
| 791 | 11 | count($params) === 0 |
|
| 792 | 13 | ) { |
|
| 793 | 3 | return array('sql' => $sql, 'params' => $params); |
|
| 794 | } |
||
| 795 | |||
| 796 | 11 | $parseKey = md5(uniqid((string)mt_rand(), true)); |
|
| 797 | |||
| 798 | 11 | foreach ($params as $name => $value) { |
|
| 799 | 11 | $nameTmp = $name; |
|
| 800 | 11 | if (strpos($name, ':') === 0) { |
|
| 801 | 9 | $nameTmp = substr($name, 1); |
|
| 802 | 9 | } |
|
| 803 | |||
| 804 | 11 | $parseKeyInner = $nameTmp . '-' . $parseKey; |
|
| 805 | 11 | $sql = str_replace(':' . $nameTmp, $parseKeyInner, $sql); |
|
| 806 | 11 | } |
|
| 807 | |||
| 808 | 11 | foreach ($params as $name => $value) { |
|
| 809 | 11 | $nameTmp = $name; |
|
| 810 | 11 | if (strpos($name, ':') === 0) { |
|
| 811 | 9 | $nameTmp = substr($name, 1); |
|
| 812 | 9 | } |
|
| 813 | |||
| 814 | 11 | $parseKeyInner = $nameTmp . '-' . $parseKey; |
|
| 815 | 11 | $sqlBefore = $sql; |
|
| 816 | 11 | $secureParamValue = $this->secure($params[$name]); |
|
| 817 | |||
| 818 | 11 | while (strpos($sql, $parseKeyInner) !== false) { |
|
| 819 | 11 | $sql = UTF8::str_replace_first( |
|
| 820 | 11 | $parseKeyInner, |
|
| 821 | 11 | $secureParamValue, |
|
| 822 | $sql |
||
| 823 | 11 | ); |
|
| 824 | 11 | } |
|
| 825 | |||
| 826 | 11 | if ($sqlBefore !== $sql) { |
|
| 827 | 11 | unset($params[$name]); |
|
| 828 | 11 | } |
|
| 829 | 11 | } |
|
| 830 | |||
| 831 | 11 | return array('sql' => $sql, 'params' => $params); |
|
| 832 | } |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 836 | * |
||
| 837 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 838 | * int: int (don't change it)<br /> |
||
| 839 | * float: float (don't change it)<br /> |
||
| 840 | * null: null (don't change it)<br /> |
||
| 841 | * array: run escape() for every key => value<br /> |
||
| 842 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 843 | * @param bool $stripe_non_utf8 |
||
| 844 | * @param bool $html_entity_decode |
||
| 845 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 846 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 847 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 848 | * |
||
| 849 | * @return mixed |
||
| 850 | */ |
||
| 851 | 54 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 852 | { |
||
| 853 | 54 | if ($var === '') { |
|
| 854 | 2 | return ''; |
|
| 855 | } |
||
| 856 | |||
| 857 | 54 | if ($var === null) { |
|
| 858 | 2 | return null; |
|
| 859 | } |
||
| 860 | |||
| 861 | // save the current value as int (for later usage) |
||
| 862 | 54 | if (!is_object($var)) { |
|
| 863 | 54 | $varInt = (int)$var; |
|
| 864 | 54 | } |
|
| 865 | |||
| 866 | /** @noinspection TypeUnsafeComparisonInspection */ |
||
| 867 | if ( |
||
| 868 | 54 | is_int($var) |
|
| 869 | || |
||
| 870 | 51 | is_bool($var) |
|
| 871 | 51 | || |
|
| 872 | ( |
||
| 873 | 51 | isset($varInt, $var[0]) |
|
| 874 | 51 | && |
|
| 875 | 51 | $var[0] != '0' |
|
| 876 | 51 | && |
|
| 877 | "$varInt" == $var |
||
| 878 | 51 | ) |
|
| 879 | 54 | ) { |
|
| 880 | |||
| 881 | // "int" || int || bool |
||
| 882 | |||
| 883 | 32 | return (int)$var; |
|
| 884 | } |
||
| 885 | |||
| 886 | 51 | if (is_float($var)) { |
|
| 887 | |||
| 888 | // float |
||
| 889 | |||
| 890 | 5 | return $var; |
|
| 891 | } |
||
| 892 | |||
| 893 | 51 | if (is_array($var)) { |
|
| 894 | |||
| 895 | // array |
||
| 896 | |||
| 897 | 4 | if ($convert_array === null) { |
|
| 898 | 3 | return null; |
|
| 899 | } |
||
| 900 | |||
| 901 | 2 | $varCleaned = array(); |
|
| 902 | 2 | foreach ((array)$var as $key => $value) { |
|
| 903 | |||
| 904 | 2 | $key = $this->escape($key, $stripe_non_utf8, $html_entity_decode); |
|
| 905 | 2 | $value = $this->escape($value, $stripe_non_utf8, $html_entity_decode); |
|
| 906 | |||
| 907 | /** @noinspection OffsetOperationsInspection */ |
||
| 908 | 2 | $varCleaned[$key] = $value; |
|
| 909 | 2 | } |
|
| 910 | |||
| 911 | 2 | if ($convert_array === true) { |
|
| 912 | 1 | $varCleaned = implode(',', $varCleaned); |
|
| 913 | |||
| 914 | 1 | return $varCleaned; |
|
| 915 | } |
||
| 916 | |||
| 917 | 2 | return (array)$varCleaned; |
|
| 918 | } |
||
| 919 | |||
| 920 | if ( |
||
| 921 | 51 | is_string($var) |
|
| 922 | || |
||
| 923 | ( |
||
| 924 | 3 | is_object($var) |
|
| 925 | 3 | && |
|
| 926 | 3 | method_exists($var, '__toString') |
|
| 927 | 3 | ) |
|
| 928 | 51 | ) { |
|
| 929 | |||
| 930 | // "string" |
||
| 931 | |||
| 932 | 51 | $var = (string)$var; |
|
| 933 | |||
| 934 | 51 | if ($stripe_non_utf8 === true) { |
|
| 935 | 11 | $var = UTF8::cleanup($var); |
|
| 936 | 11 | } |
|
| 937 | |||
| 938 | 51 | if ($html_entity_decode === true) { |
|
| 939 | // use no-html-entity for db |
||
| 940 | 1 | $var = UTF8::html_entity_decode($var); |
|
| 941 | 1 | } |
|
| 942 | |||
| 943 | 51 | $var = get_magic_quotes_gpc() ? stripslashes($var) : $var; |
|
| 944 | |||
| 945 | 51 | $var = \mysqli_real_escape_string($this->getLink(), $var); |
|
| 946 | |||
| 947 | 51 | return (string)$var; |
|
| 948 | |||
| 949 | } |
||
| 950 | |||
| 951 | 3 | if ($var instanceof \DateTime) { |
|
| 952 | |||
| 953 | // "DateTime"-object |
||
| 954 | |||
| 955 | try { |
||
| 956 | 3 | return $this->escape($var->format('Y-m-d H:i:s'), false); |
|
| 957 | } catch (\Exception $e) { |
||
| 958 | return null; |
||
| 959 | } |
||
| 960 | |||
| 961 | } else { |
||
| 962 | 2 | return false; |
|
| 963 | } |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Execute select/insert/update/delete sql-queries. |
||
| 968 | * |
||
| 969 | * @param string $query <p>sql-query</p> |
||
| 970 | * @param bool $useCache <p>use cache?</p> |
||
| 971 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 972 | * @param DB $db optional <p>the database connection</p> |
||
| 973 | * |
||
| 974 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 975 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 976 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 977 | * "true" by e.g. "DROP"-queries<br /> |
||
| 978 | * "false" on error |
||
| 979 | * |
||
| 980 | * @throws QueryException |
||
| 981 | */ |
||
| 982 | 3 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
|
| 983 | { |
||
| 984 | // init |
||
| 985 | 3 | $cacheKey = null; |
|
| 986 | 3 | if (!$db) { |
|
| 987 | 3 | $db = self::getInstance(); |
|
| 988 | 3 | } |
|
| 989 | |||
| 990 | 3 | View Code Duplication | if ($useCache === true) { |
| 991 | 1 | $cache = new Cache(null, null, false, $useCache); |
|
| 992 | 1 | $cacheKey = 'sql-' . md5($query); |
|
| 993 | |||
| 994 | if ( |
||
| 995 | 1 | $cache->getCacheIsReady() === true |
|
| 996 | 1 | && |
|
| 997 | 1 | $cache->existsItem($cacheKey) |
|
| 998 | 1 | ) { |
|
| 999 | 1 | return $cache->getItem($cacheKey); |
|
| 1000 | } |
||
| 1001 | |||
| 1002 | 1 | } else { |
|
| 1003 | 3 | $cache = false; |
|
| 1004 | } |
||
| 1005 | |||
| 1006 | 3 | $result = $db->query($query); |
|
| 1007 | |||
| 1008 | 3 | if ($result instanceof Result) { |
|
| 1009 | |||
| 1010 | 1 | $return = $result->fetchAllArray(); |
|
| 1011 | |||
| 1012 | // save into the cache |
||
| 1013 | View Code Duplication | if ( |
|
| 1014 | $cacheKey !== null |
||
| 1015 | 1 | && |
|
| 1016 | $useCache === true |
||
| 1017 | 1 | && |
|
| 1018 | $cache instanceof Cache |
||
| 1019 | 1 | && |
|
| 1020 | 1 | $cache->getCacheIsReady() === true |
|
| 1021 | 1 | ) { |
|
| 1022 | 1 | $cache->setItem($cacheKey, $return, $cacheTTL); |
|
| 1023 | 1 | } |
|
| 1024 | |||
| 1025 | 1 | } else { |
|
| 1026 | 2 | $return = $result; |
|
| 1027 | } |
||
| 1028 | |||
| 1029 | 3 | return $return; |
|
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Get all table-names via "SHOW TABLES". |
||
| 1034 | * |
||
| 1035 | * @return array |
||
| 1036 | */ |
||
| 1037 | 1 | public function getAllTables() |
|
| 1038 | { |
||
| 1039 | 1 | $query = 'SHOW TABLES'; |
|
| 1040 | 1 | $result = $this->query($query); |
|
| 1041 | |||
| 1042 | 1 | return $result->fetchAllArray(); |
|
| 1043 | } |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * @return Debug |
||
| 1047 | */ |
||
| 1048 | 9 | public function getDebugger() |
|
| 1049 | { |
||
| 1050 | 9 | return $this->_debug; |
|
| 1051 | } |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Get errors from "$this->_errors". |
||
| 1055 | * |
||
| 1056 | * @return array |
||
| 1057 | */ |
||
| 1058 | 1 | public function getErrors() |
|
| 1059 | { |
||
| 1060 | 1 | return $this->_debug->getErrors(); |
|
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * getInstance() |
||
| 1065 | * |
||
| 1066 | * @param string $hostname |
||
| 1067 | * @param string $username |
||
| 1068 | * @param string $password |
||
| 1069 | * @param string $database |
||
| 1070 | * @param int|string $port <p>default is (int)3306</p> |
||
| 1071 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1072 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1073 | * Use a empty string "" or false to disable it.</p> |
||
| 1074 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1075 | * Use a empty string "" or false to disable it.</p> |
||
| 1076 | * @param string $logger_class_name |
||
| 1077 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1078 | * @param array $extra_config <p> |
||
| 1079 | * 'session_to_db' => false|true<br> |
||
| 1080 | * 'socket' => 'string (path)'<br> |
||
| 1081 | * 'ssl' => 'bool'<br> |
||
| 1082 | * 'clientkey' => 'string (path)'<br> |
||
| 1083 | * 'clientcert' => 'string (path)'<br> |
||
| 1084 | * 'cacert' => 'string (path)'<br> |
||
| 1085 | * </p> |
||
| 1086 | * |
||
| 1087 | * @return \voku\db\DB |
||
| 1088 | */ |
||
| 1089 | 97 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = true, $echo_on_error = true, $logger_class_name = '', $logger_level = '', $extra_config = array()) |
|
| 1090 | { |
||
| 1091 | /** |
||
| 1092 | * @var $instance DB[] |
||
| 1093 | */ |
||
| 1094 | 97 | static $instance = array(); |
|
| 1095 | |||
| 1096 | /** |
||
| 1097 | * @var $firstInstance DB |
||
| 1098 | */ |
||
| 1099 | 97 | static $firstInstance = null; |
|
| 1100 | |||
| 1101 | if ( |
||
| 1102 | 97 | $hostname . $username . $password . $database . $port . $charset == '' |
|
| 1103 | 97 | && |
|
| 1104 | 14 | null !== $firstInstance |
|
| 1105 | 97 | ) { |
|
| 1106 | 14 | return $firstInstance; |
|
| 1107 | } |
||
| 1108 | |||
| 1109 | 97 | $extra_config_string = ''; |
|
| 1110 | 97 | if (is_array($extra_config) === true) { |
|
| 1111 | 97 | foreach ($extra_config as $extra_config_key => $extra_config_value) { |
|
| 1112 | $extra_config_string .= $extra_config_key . (string)$extra_config_value; |
||
| 1113 | 97 | } |
|
| 1114 | 97 | } else { |
|
| 1115 | // only for backward compatibility |
||
| 1116 | $extra_config_string = (int)$extra_config; |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | 97 | $connection = md5( |
|
| 1120 | 97 | $hostname . $username . $password . $database . $port . $charset . (int)$exit_on_error . (int)$echo_on_error . $logger_class_name . $logger_level . $extra_config_string |
|
| 1121 | 97 | ); |
|
| 1122 | |||
| 1123 | 97 | if (!isset($instance[$connection])) { |
|
| 1124 | 11 | $instance[$connection] = new self( |
|
| 1125 | 11 | $hostname, |
|
| 1126 | 11 | $username, |
|
| 1127 | 11 | $password, |
|
| 1128 | 11 | $database, |
|
| 1129 | 11 | $port, |
|
| 1130 | 11 | $charset, |
|
| 1131 | 11 | $exit_on_error, |
|
| 1132 | 11 | $echo_on_error, |
|
| 1133 | 11 | $logger_class_name, |
|
| 1134 | 11 | $logger_level, |
|
| 1135 | $extra_config |
||
| 1136 | 11 | ); |
|
| 1137 | |||
| 1138 | 5 | if (null === $firstInstance) { |
|
| 1139 | 1 | $firstInstance = $instance[$connection]; |
|
| 1140 | 1 | } |
|
| 1141 | 5 | } |
|
| 1142 | |||
| 1143 | 97 | return $instance[$connection]; |
|
| 1144 | } |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1148 | * |
||
| 1149 | * @return \mysqli |
||
| 1150 | */ |
||
| 1151 | 55 | public function getLink() |
|
| 1152 | { |
||
| 1153 | 55 | return $this->link; |
|
| 1154 | } |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Get the current charset. |
||
| 1158 | * |
||
| 1159 | * @return string |
||
| 1160 | */ |
||
| 1161 | 1 | public function get_charset() |
|
| 1162 | { |
||
| 1163 | 1 | return $this->charset; |
|
| 1164 | } |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Check if we are in a transaction. |
||
| 1168 | * |
||
| 1169 | * @return bool |
||
| 1170 | */ |
||
| 1171 | public function inTransaction() |
||
| 1172 | { |
||
| 1173 | return $this->_in_transaction; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Execute a "insert"-query. |
||
| 1178 | * |
||
| 1179 | * @param string $table |
||
| 1180 | * @param array $data |
||
| 1181 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1182 | * |
||
| 1183 | * @return false|int <p>false on error</p> |
||
| 1184 | * |
||
| 1185 | * @throws QueryException |
||
| 1186 | */ |
||
| 1187 | 28 | public function insert($table, array $data = array(), $databaseName = null) |
|
| 1188 | { |
||
| 1189 | // init |
||
| 1190 | 28 | $table = trim($table); |
|
| 1191 | |||
| 1192 | 28 | if ($table === '') { |
|
| 1193 | 2 | $this->_debug->displayError('Invalid table name, table name in empty.', false); |
|
| 1194 | |||
| 1195 | 2 | return false; |
|
| 1196 | } |
||
| 1197 | |||
| 1198 | 27 | if (count($data) === 0) { |
|
| 1199 | 3 | $this->_debug->displayError('Invalid data for INSERT, data is empty.', false); |
|
| 1200 | |||
| 1201 | 3 | return false; |
|
| 1202 | } |
||
| 1203 | |||
| 1204 | 25 | $SET = $this->_parseArrayPair($data); |
|
| 1205 | |||
| 1206 | 25 | if ($databaseName) { |
|
| 1207 | $databaseName = $this->quote_string(trim($databaseName)) . '.'; |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | 25 | $sql = 'INSERT INTO ' . $databaseName . $this->quote_string($table) . " SET $SET;"; |
|
| 1211 | |||
| 1212 | 25 | return $this->query($sql); |
|
| 1213 | } |
||
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Returns the auto generated id used in the last query. |
||
| 1217 | * |
||
| 1218 | * @return int|string |
||
| 1219 | */ |
||
| 1220 | 57 | public function insert_id() |
|
| 1221 | { |
||
| 1222 | 57 | return \mysqli_insert_id($this->link); |
|
| 1223 | } |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Check if db-connection is ready. |
||
| 1227 | * |
||
| 1228 | * @return boolean |
||
| 1229 | */ |
||
| 1230 | 105 | public function isReady() |
|
| 1231 | { |
||
| 1232 | 105 | return $this->connected ? true : false; |
|
| 1233 | } |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Get the last sql-error. |
||
| 1237 | * |
||
| 1238 | * @return string|false <p>false === there was no error</p> |
||
| 1239 | */ |
||
| 1240 | 1 | public function lastError() |
|
| 1241 | { |
||
| 1242 | 1 | $errors = $this->_debug->getErrors(); |
|
| 1243 | |||
| 1244 | 1 | return count($errors) > 0 ? end($errors) : false; |
|
| 1245 | } |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Execute a sql-multi-query. |
||
| 1249 | * |
||
| 1250 | * @param string $sql |
||
| 1251 | * |
||
| 1252 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1253 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1254 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1255 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1256 | * |
||
| 1257 | * @throws QueryException |
||
| 1258 | */ |
||
| 1259 | 1 | public function multi_query($sql) |
|
| 1260 | { |
||
| 1261 | 1 | if (!$this->isReady()) { |
|
| 1262 | return false; |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | 1 | View Code Duplication | if (!$sql || $sql === '') { |
| 1266 | 1 | $this->_debug->displayError('Can not execute an empty query.', false); |
|
| 1267 | |||
| 1268 | 1 | return false; |
|
| 1269 | } |
||
| 1270 | |||
| 1271 | 1 | $query_start_time = microtime(true); |
|
| 1272 | 1 | $resultTmp = \mysqli_multi_query($this->link, $sql); |
|
| 1273 | 1 | $query_duration = microtime(true) - $query_start_time; |
|
| 1274 | |||
| 1275 | 1 | $this->_debug->logQuery($sql, $query_duration, 0); |
|
| 1276 | |||
| 1277 | 1 | $returnTheResult = false; |
|
| 1278 | 1 | $result = array(); |
|
| 1279 | |||
| 1280 | 1 | if ($resultTmp) { |
|
| 1281 | do { |
||
| 1282 | |||
| 1283 | 1 | $resultTmpInner = \mysqli_store_result($this->link); |
|
| 1284 | |||
| 1285 | 1 | if ($resultTmpInner instanceof \mysqli_result) { |
|
| 1286 | |||
| 1287 | 1 | $returnTheResult = true; |
|
| 1288 | 1 | $result[] = new Result($sql, $resultTmpInner); |
|
| 1289 | |||
| 1290 | 1 | } else { |
|
| 1291 | |||
| 1292 | // is the query successful |
||
| 1293 | 1 | if ($resultTmpInner === true || !\mysqli_errno($this->link)) { |
|
| 1294 | 1 | $result[] = true; |
|
| 1295 | 1 | } else { |
|
| 1296 | $result[] = false; |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | } |
||
| 1300 | |||
| 1301 | 1 | } while (\mysqli_more_results($this->link) === true ? \mysqli_next_result($this->link) : false); |
|
| 1302 | |||
| 1303 | 1 | } else { |
|
| 1304 | |||
| 1305 | // log the error query |
||
| 1306 | 1 | $this->_debug->logQuery($sql, $query_duration, 0, true); |
|
| 1307 | |||
| 1308 | 1 | return $this->queryErrorHandling(\mysqli_error($this->link), \mysqli_errno($this->link), $sql, false, true); |
|
| 1309 | } |
||
| 1310 | |||
| 1311 | // return the result only if there was a "SELECT"-query |
||
| 1312 | 1 | if ($returnTheResult === true) { |
|
| 1313 | 1 | return $result; |
|
| 1314 | } |
||
| 1315 | |||
| 1316 | if ( |
||
| 1317 | 1 | count($result) > 0 |
|
| 1318 | 1 | && |
|
| 1319 | 1 | in_array(false, $result, true) === false |
|
| 1320 | 1 | ) { |
|
| 1321 | 1 | return true; |
|
| 1322 | } |
||
| 1323 | |||
| 1324 | return false; |
||
| 1325 | } |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Pings a server connection, or tries to reconnect |
||
| 1329 | * if the connection has gone down. |
||
| 1330 | * |
||
| 1331 | * @return boolean |
||
| 1332 | */ |
||
| 1333 | 3 | public function ping() |
|
| 1334 | { |
||
| 1335 | if ( |
||
| 1336 | 3 | $this->link |
|
| 1337 | 3 | && |
|
| 1338 | 3 | $this->link instanceof \mysqli |
|
| 1339 | 3 | ) { |
|
| 1340 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 1341 | /** @noinspection UsageOfSilenceOperatorInspection */ |
||
| 1342 | 3 | return (bool)@\mysqli_ping($this->link); |
|
| 1343 | } |
||
| 1344 | |||
| 1345 | return false; |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1350 | * |
||
| 1351 | * @param string $query |
||
| 1352 | * |
||
| 1353 | * @return Prepare |
||
| 1354 | */ |
||
| 1355 | 2 | public function prepare($query) |
|
| 1356 | { |
||
| 1357 | 2 | return new Prepare($this, $query); |
|
| 1358 | } |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1362 | * |
||
| 1363 | * @param string $query |
||
| 1364 | * |
||
| 1365 | * @return mixed |
||
| 1366 | * @deprecated |
||
| 1367 | * @throws \Exception |
||
| 1368 | */ |
||
| 1369 | public static function qry($query) |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Execute a sql-query. |
||
| 1397 | * |
||
| 1398 | * @param string $sql <p>The sql query-string.</p> |
||
| 1399 | * |
||
| 1400 | * @param array|boolean $params <p> |
||
| 1401 | * "array" of sql-query-parameters<br/> |
||
| 1402 | * "false" if you don't need any parameter (default)<br/> |
||
| 1403 | * </p> |
||
| 1404 | * |
||
| 1405 | * @return bool|int|Result <p> |
||
| 1406 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1407 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1408 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1409 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1410 | * "false" on error |
||
| 1411 | * </p> |
||
| 1412 | * |
||
| 1413 | * @throws QueryException |
||
| 1414 | */ |
||
| 1415 | 95 | public function query($sql = '', $params = false) |
|
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Error-handling for the sql-query. |
||
| 1497 | * |
||
| 1498 | * @param string $errorMessage |
||
| 1499 | * @param int $errorNumber |
||
| 1500 | * @param string $sql |
||
| 1501 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1502 | * @param bool $sqlMultiQuery |
||
| 1503 | * |
||
| 1504 | * @throws QueryException |
||
| 1505 | * @throws DBGoneAwayException |
||
| 1506 | * |
||
| 1507 | * @return bool |
||
| 1508 | */ |
||
| 1509 | 13 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Quote && Escape e.g. a table name string. |
||
| 1557 | * |
||
| 1558 | * @param string $str |
||
| 1559 | * |
||
| 1560 | * @return string |
||
| 1561 | */ |
||
| 1562 | 36 | public function quote_string($str) |
|
| 1575 | |||
| 1576 | /** |
||
| 1577 | * Reconnect to the MySQL-Server. |
||
| 1578 | * |
||
| 1579 | * @param bool $checkViaPing |
||
| 1580 | * |
||
| 1581 | * @return bool |
||
| 1582 | */ |
||
| 1583 | 3 | public function reconnect($checkViaPing = false) |
|
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Execute a "replace"-query. |
||
| 1601 | * |
||
| 1602 | * @param string $table |
||
| 1603 | * @param array $data |
||
| 1604 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1605 | * |
||
| 1606 | * @return false|int <p>false on error</p> |
||
| 1607 | * |
||
| 1608 | * @throws QueryException |
||
| 1609 | */ |
||
| 1610 | 1 | public function replace($table, array $data = array(), $databaseName = null) |
|
| 1651 | |||
| 1652 | /** |
||
| 1653 | * Rollback in a transaction and end the transaction. |
||
| 1654 | * |
||
| 1655 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1656 | */ |
||
| 1657 | 4 | View Code Duplication | public function rollback() |
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1674 | * |
||
| 1675 | * <p> |
||
| 1676 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1677 | * 1. parse into "int" |
||
| 1678 | * </p><br /> |
||
| 1679 | * |
||
| 1680 | * <p> |
||
| 1681 | * <strong>float:</strong><br /> |
||
| 1682 | * 1. return "float" |
||
| 1683 | * </p><br /> |
||
| 1684 | * |
||
| 1685 | * <p> |
||
| 1686 | * <strong>string:</strong><br /> |
||
| 1687 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1688 | * 2. trim whitespace<br /> |
||
| 1689 | * 3. trim '<br /> |
||
| 1690 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1691 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1692 | * 6. add ' around the new string<br /> |
||
| 1693 | * </p><br /> |
||
| 1694 | * |
||
| 1695 | * <p> |
||
| 1696 | * <strong>array:</strong><br /> |
||
| 1697 | * 1. return null |
||
| 1698 | * </p><br /> |
||
| 1699 | * |
||
| 1700 | * <p> |
||
| 1701 | * <strong>object:</strong><br /> |
||
| 1702 | * 1. return false |
||
| 1703 | * </p><br /> |
||
| 1704 | * |
||
| 1705 | * <p> |
||
| 1706 | * <strong>null:</strong><br /> |
||
| 1707 | * 1. return null |
||
| 1708 | * </p> |
||
| 1709 | * |
||
| 1710 | * @param mixed $var |
||
| 1711 | * |
||
| 1712 | * @return mixed |
||
| 1713 | */ |
||
| 1714 | 43 | public function secure($var) |
|
| 1752 | |||
| 1753 | /** |
||
| 1754 | * Execute a "select"-query. |
||
| 1755 | * |
||
| 1756 | * @param string $table |
||
| 1757 | * @param string|array $where |
||
| 1758 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1759 | * |
||
| 1760 | * @return false|Result <p>false on error</p> |
||
| 1761 | * |
||
| 1762 | * @throws QueryException |
||
| 1763 | */ |
||
| 1764 | 24 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
| 1791 | |||
| 1792 | /** |
||
| 1793 | * Selects a different database than the one specified on construction. |
||
| 1794 | * |
||
| 1795 | * @param string $database <p>Database name to switch to.</p> |
||
| 1796 | * |
||
| 1797 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1798 | */ |
||
| 1799 | public function select_db($database) |
||
| 1807 | |||
| 1808 | /** |
||
| 1809 | * Set the current charset. |
||
| 1810 | * |
||
| 1811 | * @param string $charset |
||
| 1812 | * |
||
| 1813 | * @return bool |
||
| 1814 | */ |
||
| 1815 | 8 | public function set_charset($charset) |
|
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Set the option to convert null to "''" (empty string). |
||
| 1840 | * |
||
| 1841 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1842 | * |
||
| 1843 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 1844 | * |
||
| 1845 | * @param $bool |
||
| 1846 | * |
||
| 1847 | * @return $this |
||
| 1848 | */ |
||
| 1849 | public function set_convert_null_to_empty_string($bool) |
||
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Enables or disables internal report functions |
||
| 1858 | * |
||
| 1859 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1860 | * |
||
| 1861 | * @param int $flags <p> |
||
| 1862 | * <table> |
||
| 1863 | * Supported flags |
||
| 1864 | * <tr valign="top"> |
||
| 1865 | * <td>Name</td> |
||
| 1866 | * <td>Description</td> |
||
| 1867 | * </tr> |
||
| 1868 | * <tr valign="top"> |
||
| 1869 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1870 | * <td>Turns reporting off</td> |
||
| 1871 | * </tr> |
||
| 1872 | * <tr valign="top"> |
||
| 1873 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1874 | * <td>Report errors from mysqli function calls</td> |
||
| 1875 | * </tr> |
||
| 1876 | * <tr valign="top"> |
||
| 1877 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1878 | * <td> |
||
| 1879 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1880 | * instead of warnings |
||
| 1881 | * </td> |
||
| 1882 | * </tr> |
||
| 1883 | * <tr valign="top"> |
||
| 1884 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1885 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1886 | * </tr> |
||
| 1887 | * <tr valign="top"> |
||
| 1888 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1889 | * <td>Set all options (report all)</td> |
||
| 1890 | * </tr> |
||
| 1891 | * </table> |
||
| 1892 | * </p> |
||
| 1893 | * |
||
| 1894 | * @return bool |
||
| 1895 | */ |
||
| 1896 | public function set_mysqli_report($flags) |
||
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Show config errors by throw exceptions. |
||
| 1903 | * |
||
| 1904 | * @return bool |
||
| 1905 | * |
||
| 1906 | * @throws \InvalidArgumentException |
||
| 1907 | */ |
||
| 1908 | 11 | public function showConfigError() |
|
| 1936 | |||
| 1937 | /** |
||
| 1938 | * alias: "beginTransaction()" |
||
| 1939 | */ |
||
| 1940 | 1 | public function startTransaction() |
|
| 1944 | |||
| 1945 | /** |
||
| 1946 | * Execute a callback inside a transaction. |
||
| 1947 | * |
||
| 1948 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 1949 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 1950 | * |
||
| 1951 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1952 | */ |
||
| 1953 | 1 | public function transact($callback) |
|
| 1979 | |||
| 1980 | /** |
||
| 1981 | * Execute a "update"-query. |
||
| 1982 | * |
||
| 1983 | * @param string $table |
||
| 1984 | * @param array $data |
||
| 1985 | * @param array|string $where |
||
| 1986 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1987 | * |
||
| 1988 | * @return false|int <p>false on error</p> |
||
| 1989 | * |
||
| 1990 | * @throws QueryException |
||
| 1991 | */ |
||
| 1992 | 7 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
|
| 2027 | |||
| 2028 | /** |
||
| 2029 | * Determine if database table exists |
||
| 2030 | * |
||
| 2031 | * @param string $table |
||
| 2032 | * |
||
| 2033 | * @return bool |
||
| 2034 | */ |
||
| 2035 | 1 | public function table_exists($table) |
|
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Count number of rows found matching a specific query. |
||
| 2053 | * |
||
| 2054 | * @param string |
||
| 2055 | * |
||
| 2056 | * @return int |
||
| 2057 | */ |
||
| 2058 | 1 | public function num_rows($query) |
|
| 2072 | } |
||
| 2073 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.