| Total Complexity | 41 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractPdoDatabase 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.
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 AbstractPdoDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AbstractPdoDatabase extends \WebServCo\Framework\AbstractLibrary |
||
| 11 | { |
||
| 12 | use \WebServCo\Framework\Traits\DatabaseTrait; |
||
| 13 | use \WebServCo\Framework\Traits\DatabaseAddQueryTrait; |
||
| 14 | |||
| 15 | protected \PDO $db; |
||
| 16 | protected \PDOStatement $stmt; |
||
| 17 | |||
| 18 | abstract protected function getDataSourceName(string $host, int $port, string $dbname): string; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param array<string,string|array<mixed>> $settings |
||
| 22 | */ |
||
| 23 | public function __construct(array $settings = []) |
||
| 24 | { |
||
| 25 | parent::__construct($settings); |
||
| 26 | |||
| 27 | try { |
||
| 28 | $dsn = $this->getDataSourceName( |
||
| 29 | Config::string('APP_DBMS_HOST'), |
||
| 30 | Config::int('APP_DBMS_PORT'), |
||
| 31 | Config::string('APP_DBMS_DBNAME'), |
||
| 32 | ); |
||
| 33 | $this->db = new \PDO( |
||
| 34 | $dsn, |
||
| 35 | Config::string('APP_DBMS_USERNAME'), |
||
| 36 | Config::string('APP_DBMS_PASSWD'), |
||
| 37 | [ |
||
| 38 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
||
| 39 | \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
||
| 40 | \PDO::ATTR_EMULATE_PREPARES => false, |
||
| 41 | \PDO::ATTR_PERSISTENT => false, |
||
| 42 | ], |
||
| 43 | ); |
||
| 44 | } catch (\Throwable $e) { // PDOException/RuntimeException/Exception |
||
| 45 | throw new DatabaseException($e->getMessage(), $e); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | public function affectedRows(): int |
||
| 50 | { |
||
| 51 | if (!($this->stmt instanceof \PDOStatement)) { |
||
|
|
|||
| 52 | throw new DatabaseException('No Statement object available.'); |
||
| 53 | } |
||
| 54 | return $this->stmt->rowCount(); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function escape(string $string): string |
||
| 58 | { |
||
| 59 | return $this->db->quote($string); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array<int,float|int|string> $params |
||
| 64 | * @return bool|int|string|null |
||
| 65 | */ |
||
| 66 | public function getColumn(string $query, array $params = [], int $columnNumber = 0) |
||
| 67 | { |
||
| 68 | $this->query($query, $params); |
||
| 69 | return $this->stmt->fetchColumn($columnNumber); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param array<int,float|int|string> $params |
||
| 74 | * @return array<string,float|int|string> |
||
| 75 | */ |
||
| 76 | public function getRow(string $query, array $params = []): array |
||
| 77 | { |
||
| 78 | $this->query($query, $params); |
||
| 79 | $result = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 80 | return $this->handleStatementReturn($result); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param array<int,float|int|string> $params |
||
| 85 | * @return array<int,array<string,float|int|string>> |
||
| 86 | */ |
||
| 87 | public function getRows(string $query, array $params = []): array |
||
| 88 | { |
||
| 89 | $this->query($query, $params); |
||
| 90 | $result = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 91 | return $this->handleStatementReturn($result); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get last inserted Id. |
||
| 96 | * |
||
| 97 | * https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id |
||
| 98 | * If you insert multiple rows using a single INSERT statement, |
||
| 99 | * LAST_INSERT_ID() returns the value generated for the first inserted row only. |
||
| 100 | * The reason for this is to make it possible to reproduce easily the same |
||
| 101 | * INSERT statement against some other server. |
||
| 102 | * |
||
| 103 | * PDO: |
||
| 104 | * Returns the ID of the last inserted row, or the last value from a sequence object, |
||
| 105 | * depending on the underlying driver. |
||
| 106 | * For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter. |
||
| 107 | */ |
||
| 108 | public function lastInsertId(string $name = ''): int |
||
| 111 | } |
||
| 112 | |||
| 113 | public function numRows(): int |
||
| 114 | { |
||
| 115 | if (!($this->stmt instanceof \PDOStatement)) { |
||
| 116 | throw new DatabaseException('No Statement object available.'); |
||
| 117 | } |
||
| 118 | return $this->stmt->rowCount(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array<int,float|int|string|null> $params |
||
| 123 | */ |
||
| 124 | public function query(string $query, array $params = []): \PDOStatement |
||
| 125 | { |
||
| 126 | if (!$query) { |
||
| 127 | throw new DatabaseException('No query specified.'); |
||
| 128 | } |
||
| 129 | |||
| 130 | try { |
||
| 131 | $this->stmt = $this->db->prepare($query); |
||
| 132 | if ($params) { |
||
| 133 | $this->bindParams($params); |
||
| 134 | } |
||
| 135 | $this->stmt->execute(); |
||
| 136 | return $this->stmt; |
||
| 137 | } catch (\Throwable $e) { // \PDOException, \RuntimeException |
||
| 138 | throw new DatabaseException($e->getMessage(), $e); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param mixed $value |
||
| 144 | */ |
||
| 145 | public function setAttribute(int $attribute, $value): bool |
||
| 146 | { |
||
| 147 | return $this->db->setAttribute($attribute, $value); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param array<int,array<int,mixed>> $queries |
||
| 152 | */ |
||
| 153 | public function transaction(array $queries): bool |
||
| 154 | { |
||
| 155 | try { |
||
| 156 | $this->db->beginTransaction(); |
||
| 157 | foreach ($queries as $item) { |
||
| 158 | if (!isset($item[0])) { |
||
| 159 | throw new DatabaseException('No query specified.'); |
||
| 160 | } |
||
| 161 | $params = $item[1] ?? []; |
||
| 162 | $this->query($item[0], $params); |
||
| 163 | } |
||
| 164 | $this->db->commit(); |
||
| 165 | return true; |
||
| 166 | } catch (\Throwable $e) { // DatabaseException, \PDOException, \RuntimeException |
||
| 167 | $this->db->rollBack(); |
||
| 168 | throw new DatabaseException($e->getMessage(), $e); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param array<mixed> $data |
||
| 174 | */ |
||
| 175 | protected function bindParams(array $data): bool |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function getDataType(string $variable): int |
||
| 199 | { |
||
| 200 | $type = \gettype($variable); |
||
| 201 | |||
| 202 | switch ($type) { |
||
| 203 | case 'NULL': |
||
| 204 | return \PDO::PARAM_NULL; |
||
| 205 | case 'integer': |
||
| 206 | return \PDO::PARAM_INT; |
||
| 207 | case 'boolean': |
||
| 208 | // causes data not to be inserted |
||
| 209 | //return \PDO::PARAM_BOOL; |
||
| 210 | case 'string': |
||
| 211 | case 'double': |
||
| 212 | case 'array': |
||
| 213 | case 'object': |
||
| 214 | case 'resource': |
||
| 215 | case 'resource (closed)': |
||
| 216 | case 'unknown type': |
||
| 217 | default: |
||
| 218 | return \PDO::PARAM_STR; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param mixed $param |
||
| 224 | */ |
||
| 225 | protected function validateParam($param): bool |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Make sure PDO Statement returns an array when there are no errors. |
||
| 235 | * |
||
| 236 | * "In all cases, false is returned on failure." |
||
| 237 | * However, false is also returned when there are no results. |
||
| 238 | * |
||
| 239 | * @param bool|array<mixed> $result |
||
| 240 | * @return array<mixed> |
||
| 241 | */ |
||
| 242 | protected function handleStatementReturn($result): array |
||
| 257 | } |
||
| 258 | } |
||
| 259 |