Exceptions
1 year ago
Connection.php
1 year ago
ConvertParameters.php
1 year ago
Driver.php
1 year ago
Result.php
2 months ago
Statement.php
1 year ago
index.php
1 year ago
Connection.php
140 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Doctrine\WPDB; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Exceptions\ConnectionException; |
| 9 | use MailPoet\Doctrine\WPDB\Exceptions\QueryException; |
| 10 | use MailPoetVendor\Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
| 11 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 12 | use mysqli; |
| 13 | use PDO; |
| 14 | use PDOException; |
| 15 | use Throwable; |
| 16 | use wpdb; |
| 17 | |
| 18 | /** |
| 19 | * @phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 20 | */ |
| 21 | class Connection implements ServerInfoAwareConnection { |
| 22 | public function __construct() { |
| 23 | global $wpdb; |
| 24 | if (!$wpdb instanceof wpdb) { |
| 25 | throw new ConnectionException('WPDB is not initialized.'); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public function prepare(string $sql): Statement { |
| 30 | return new Statement($this, $sql); |
| 31 | } |
| 32 | |
| 33 | public function query(string $sql): Result { |
| 34 | global $wpdb; |
| 35 | $value = $this->runQuery($sql); |
| 36 | $result = $wpdb->last_result; |
| 37 | return new Result($result, is_int($value) ? $value : 0); |
| 38 | } |
| 39 | |
| 40 | public function exec(string $sql): int { |
| 41 | global $wpdb; |
| 42 | $this->runQuery($sql); |
| 43 | return $wpdb->rows_affected; |
| 44 | } |
| 45 | |
| 46 | public function beginTransaction(): bool { |
| 47 | $this->runQuery('START TRANSACTION'); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | public function commit(): bool { |
| 52 | $this->runQuery('COMMIT'); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | public function rollBack(): bool { |
| 57 | $this->runQuery('ROLLBACK'); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Quotes a string for use in a query. |
| 63 | * The type hint parameter is not needed for WPDB (mysqli). |
| 64 | * See also Doctrine\DBAL\Driver\Mysqli\Connection::quote(). |
| 65 | * |
| 66 | * @param mixed $value |
| 67 | * @param int $type |
| 68 | */ |
| 69 | public function quote($value, $type = ParameterType::STRING): string { |
| 70 | global $wpdb; |
| 71 | return "'" . $wpdb->_escape($value) . "'"; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param string|null $name |
| 76 | */ |
| 77 | public function lastInsertId($name = null): int { |
| 78 | global $wpdb; |
| 79 | return $wpdb->insert_id; |
| 80 | } |
| 81 | |
| 82 | public function getServerVersion(): string { |
| 83 | global $wpdb; |
| 84 | return $wpdb->db_server_info(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * MySQL — returns an instance of mysqli. |
| 89 | * SQLite — returns an instance of PDO. |
| 90 | * |
| 91 | * @return mysqli|PDO|false|null |
| 92 | */ |
| 93 | public function getNativeConnection() { |
| 94 | global $wpdb; |
| 95 | |
| 96 | // WPDB keeps connection instance (mysqli) in a protected property $dbh. |
| 97 | // We can access it using a closure that is bound to the $wpdb instance. |
| 98 | $getDbh = function () { |
| 99 | return $this->dbh; // @phpstan-ignore-line -- PHPStan doesn't know the binding context |
| 100 | }; |
| 101 | $dbh = $getDbh->call($wpdb); |
| 102 | if (is_object($dbh) && method_exists($dbh, 'get_pdo')) { |
| 103 | return $dbh->get_pdo(); |
| 104 | } |
| 105 | return $getDbh->call($wpdb); |
| 106 | } |
| 107 | |
| 108 | public static function isSQLite(): bool { |
| 109 | return defined('DB_ENGINE') && DB_ENGINE === 'sqlite'; |
| 110 | } |
| 111 | |
| 112 | private function runQuery(string $sql) { |
| 113 | global $wpdb; |
| 114 | try { |
| 115 | $value = $wpdb->query($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 116 | } catch (Throwable $e) { |
| 117 | if ($e instanceof PDOException) { |
| 118 | throw new QueryException($e->getMessage(), $e->errorInfo[0] ?? null, $e->errorInfo[1] ?? 0); |
| 119 | } |
| 120 | throw new QueryException($e->getMessage(), null, 0, $e); |
| 121 | } |
| 122 | if ($value === false) { |
| 123 | $this->handleQueryError(); |
| 124 | } |
| 125 | return $value; |
| 126 | } |
| 127 | |
| 128 | private function handleQueryError(): void { |
| 129 | global $wpdb; |
| 130 | $nativeConnection = $this->getNativeConnection(); |
| 131 | if ($nativeConnection instanceof mysqli) { |
| 132 | throw new QueryException($wpdb->last_error, $nativeConnection->sqlstate, $nativeConnection->errno); |
| 133 | } elseif ($nativeConnection instanceof PDO) { |
| 134 | $info = $nativeConnection->errorInfo(); |
| 135 | throw new QueryException($wpdb->last_error, $info[0] ?? null, $info[1] ?? 0); |
| 136 | } |
| 137 | throw new QueryException($wpdb->last_error); |
| 138 | } |
| 139 | } |
| 140 |