| Total Complexity | 43 |
| Total Lines | 190 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like PDOWrapper 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 PDOWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class PDOWrapper extends AbstractDbWrapper { |
||
| 18 | protected static $savepointsDrivers = [ 'pgsql' => true,'mysql' => true,'sqlite' => true ]; |
||
| 19 | private static $quotes = [ 'mysql' => '`','sqlite' => '"','pgsql' => '"' ]; |
||
| 20 | protected $transactionLevel = 0; |
||
| 21 | |||
| 22 | public function __construct($dbType = 'mysql') { |
||
| 23 | $this->quote = self::$quotes [$dbType] ?? ''; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function fetchAllColumn($statement, array $values = null, string $column = null) { |
||
| 27 | $result = false; |
||
| 28 | if ($statement->execute ( $values )) { |
||
| 29 | $result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column ); |
||
| 30 | } |
||
| 31 | $statement->closeCursor (); |
||
| 32 | return $result; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function lastInsertId() { |
||
| 36 | return $this->dbInstance->lastInsertId (); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function fetchAll($statement, array $values = null, $mode = null) { |
||
| 40 | $result = false; |
||
| 41 | if ($statement->execute ( $values )) { |
||
| 42 | $result = $statement->fetchAll ( $mode ); |
||
| 43 | } |
||
| 44 | $statement->closeCursor (); |
||
| 45 | return $result; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function fetchOne($statement, array $values = null, $mode = null) { |
||
| 49 | $result = false; |
||
| 50 | if ($statement->execute ( $values )) { |
||
| 51 | $result = $statement->fetch ( $mode ); |
||
| 52 | } |
||
| 53 | $statement->closeCursor (); |
||
| 54 | return $result; |
||
| 55 | } |
||
| 56 | |||
| 57 | public static function getAvailableDrivers() { |
||
| 58 | return \PDO::getAvailableDrivers (); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function prepareStatement(string $sql) { |
||
| 62 | return $this->dbInstance->prepare ( $sql ); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function fetchColumn($statement, array $values = null, int $columnNumber = null) { |
||
| 66 | if ($statement->execute ( $values )) { |
||
| 67 | return $statement->fetchColumn ( $columnNumber ); |
||
| 68 | } |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function getStatement($sql) { |
||
| 73 | $st = $this->dbInstance->prepare ( $sql ); |
||
| 74 | $st->setFetchMode ( \PDO::FETCH_ASSOC ); |
||
| 75 | return $st; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function execute($sql) { |
||
| 79 | return $this->dbInstance->exec ( $sql ); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) { |
||
| 83 | $options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; |
||
| 84 | $this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options ); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') { |
||
| 88 | return $dbType . ':dbname=' . $dbName . ';host=' . $serverName . ';charset=UTF8;port=' . $port; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function bindValueFromStatement($statement, $parameter, $value) { |
||
| 92 | return $statement->bindValue ( ":" . $parameter, $value ); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function query(string $sql) { |
||
| 96 | return $this->dbInstance->query ( $sql ); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function queryAll(string $sql, int $fetchStyle = null) { |
||
| 100 | return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle ); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function queryColumn(string $sql, int $columnNumber = null) { |
||
| 104 | return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber ); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function executeStatement($statement, array $values = null) { |
||
| 108 | return $statement->execute ( $values ); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getTablesName() { |
||
| 112 | $query = $this->dbInstance->query ( 'SHOW TABLES' ); |
||
| 113 | return $query->fetchAll ( \PDO::FETCH_COLUMN ); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function statementRowCount($statement) { |
||
| 117 | return $statement->rowCount (); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function inTransaction() { |
||
| 121 | return $this->dbInstance->inTransaction (); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function commit() { |
||
| 125 | return $this->dbInstance->commit (); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function rollBack() { |
||
| 129 | return $this->dbInstance->rollBack (); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function beginTransaction() { |
||
| 133 | return $this->dbInstance->beginTransaction (); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function savePoint($level) { |
||
| 137 | $this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level ); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function releasePoint($level) { |
||
| 141 | $this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level ); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function rollbackPoint($level) { |
||
| 145 | $this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function nestable() { |
||
| 149 | return isset ( self::$savepointsDrivers [$this->dbInstance->getAttribute ( \PDO::ATTR_DRIVER_NAME )] ); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function ping() { |
||
| 153 | return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) )); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getPrimaryKeys($tableName) { |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getForeignKeys($tableName, $pkName, $dbName = null) { |
||
| 167 | $recordset = $this->dbInstance->query ( "SELECT * |
||
| 168 | FROM |
||
| 169 | information_schema.KEY_COLUMN_USAGE |
||
| 170 | WHERE |
||
| 171 | REFERENCED_TABLE_NAME = '" . $tableName . "' |
||
| 172 | AND REFERENCED_COLUMN_NAME = '" . $pkName . "' |
||
| 173 | AND TABLE_SCHEMA = '" . $dbName . "';" ); |
||
| 174 | return $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getFieldsInfos($tableName) { |
||
| 178 | $fieldsInfos = array (); |
||
| 179 | $recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" ); |
||
| 180 | $fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
||
| 181 | foreach ( $fields as $field ) { |
||
| 182 | $fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ]; |
||
| 183 | } |
||
| 184 | return $fieldsInfos; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function pool() { |
||
| 189 | } |
||
| 190 | |||
| 191 | public function freePool($db) { |
||
| 192 | throw new DBException ( 'PDO does not accept connection pooling' ); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function setPool($pool) { |
||
| 196 | throw new DBException ( 'PDO does not accept connection pooling' ); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function _optPrepareAndExecute($sql, array $values = null) { |
||
| 207 | } |
||
| 208 | } |