From bd8ce38320b6a1280bf5788fa345a998efeebc45 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 9 May 2018 12:27:10 +0200 Subject: [PATCH] MySqliDriver: refactoring, uses OOP style --- src/Dibi/Drivers/MySqliDriver.php | 36 +++++++++++++++++++------------ src/Dibi/Drivers/MySqliResult.php | 14 ++++++------ tests/dibi/exceptions.mysql.phpt | 12 +++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index d7d083dd..231d68bb 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -57,7 +57,7 @@ class MySqliDriver implements Dibi\Driver } mysqli_report(MYSQLI_REPORT_OFF); - if (isset($config['resource'])) { + if (isset($config['resource']) && $config['resource'] instanceof \mysqli) { $this->connection = $config['resource']; } else { @@ -87,18 +87,26 @@ class MySqliDriver implements Dibi\Driver $this->connection = mysqli_init(); if (isset($config['options'])) { foreach ($config['options'] as $key => $value) { - mysqli_options($this->connection, $key, $value); + $this->connection->options($key, $value); } } - @mysqli_real_connect($this->connection, (empty($config['persistent']) ? '' : 'p:') . $config['host'], $config['username'], $config['password'], $config['database'] ?? '', $config['port'] ?? 0, $config['socket'], $config['flags'] ?? 0); // intentionally @ + @$this->connection->real_connect( // intentionally @ + (empty($config['persistent']) ? '' : 'p:') . $config['host'], + $config['username'], + $config['password'], + $config['database'] ?? '', + $config['port'] ?? 0, + $config['socket'], + $config['flags'] ?? 0 + ); - if ($errno = mysqli_connect_errno()) { - throw new Dibi\DriverException(mysqli_connect_error(), $errno); + if ($this->connection->connect_errno) { + throw new Dibi\DriverException($this->connection->connect_error, $this->connection->connect_errno); } } if (isset($config['charset'])) { - if (!@mysqli_set_charset($this->connection, $config['charset'])) { + if (!@$this->connection->set_charset($config['charset'])) { $this->query("SET NAMES '$config[charset]'"); } } @@ -120,7 +128,7 @@ class MySqliDriver implements Dibi\Driver */ public function disconnect(): void { - @mysqli_close($this->connection); // @ - connection can be already disconnected + @$this->connection->close(); // @ - connection can be already disconnected } @@ -130,12 +138,12 @@ class MySqliDriver implements Dibi\Driver */ public function query(string $sql): ?Dibi\ResultDriver { - $res = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ + $res = @$this->connection->query($sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ if ($code = mysqli_errno($this->connection)) { throw static::createException(mysqli_error($this->connection), $code, $sql); - } elseif (is_object($res)) { + } elseif ($res instanceof \mysqli_result) { return $this->createResultDriver($res); } return null; @@ -165,7 +173,7 @@ class MySqliDriver implements Dibi\Driver public function getInfo(): array { $res = []; - preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER); + preg_match_all('#(.+?): +(\d+) *#', $this->connection->info, $matches, PREG_SET_ORDER); if (preg_last_error()) { throw new Dibi\PcreException; } @@ -182,7 +190,7 @@ class MySqliDriver implements Dibi\Driver */ public function getAffectedRows(): ?int { - return mysqli_affected_rows($this->connection) === -1 ? null : mysqli_affected_rows($this->connection); + return $this->connection->affected_rows === -1 ? null : $this->connection->affected_rows; } @@ -191,7 +199,7 @@ class MySqliDriver implements Dibi\Driver */ public function getInsertId(?string $sequence): ?int { - return mysqli_insert_id($this->connection); + return $this->connection->insert_id; } @@ -260,13 +268,13 @@ class MySqliDriver implements Dibi\Driver */ public function escapeText(string $value): string { - return "'" . mysqli_real_escape_string($this->connection, $value) . "'"; + return "'" . $this->connection->escape_string($value) . "'"; } public function escapeBinary(string $value): string { - return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'"; + return "_binary'" . $this->connection->escape_string($value) . "'"; } diff --git a/src/Dibi/Drivers/MySqliResult.php b/src/Dibi/Drivers/MySqliResult.php index 298e5f96..d8c9bbd1 100644 --- a/src/Dibi/Drivers/MySqliResult.php +++ b/src/Dibi/Drivers/MySqliResult.php @@ -55,7 +55,7 @@ class MySqliResult implements Dibi\ResultDriver if (!$this->buffered) { throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.'); } - return mysqli_num_rows($this->resultSet); + return $this->resultSet->num_rows; } @@ -65,7 +65,9 @@ class MySqliResult implements Dibi\ResultDriver */ public function fetch(bool $assoc): ?array { - return mysqli_fetch_array($this->resultSet, $assoc ? MYSQLI_ASSOC : MYSQLI_NUM); + return $assoc + ? $this->resultSet->fetch_assoc() + : $this->resultSet->fetch_row(); } @@ -78,7 +80,7 @@ class MySqliResult implements Dibi\ResultDriver if (!$this->buffered) { throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.'); } - return mysqli_data_seek($this->resultSet, $row); + return $this->resultSet->data_seek($row); } @@ -87,7 +89,7 @@ class MySqliResult implements Dibi\ResultDriver */ public function free(): void { - mysqli_free_result($this->resultSet); + $this->resultSet->free(); } @@ -108,10 +110,10 @@ class MySqliResult implements Dibi\ResultDriver $types[MYSQLI_TYPE_TINY] = $types[MYSQLI_TYPE_SHORT] = $types[MYSQLI_TYPE_LONG] = 'INT'; } - $count = mysqli_num_fields($this->resultSet); + $count = $this->resultSet->field_count; $columns = []; for ($i = 0; $i < $count; $i++) { - $row = (array) mysqli_fetch_field_direct($this->resultSet, $i); + $row = (array) $this->resultSet->fetch_field_direct($i); $columns[] = [ 'name' => $row['name'], 'table' => $row['orgtable'], diff --git a/tests/dibi/exceptions.mysql.phpt b/tests/dibi/exceptions.mysql.phpt index dd2281d1..5b8bb9e7 100644 --- a/tests/dibi/exceptions.mysql.phpt +++ b/tests/dibi/exceptions.mysql.phpt @@ -15,6 +15,18 @@ $conn = new Dibi\Connection($config); $conn->loadFile(__DIR__ . "/data/$config[system].sql"); +$e = Assert::exception(function () use ($conn) { + new Dibi\Connection([ + 'driver' => 'mysqli', + 'host' => 'localhost', + 'username' => 'unknown', + 'password' => 'unknown', + ]); +}, Dibi\DriverException::class); + +Assert::null($e->getSql()); + + $e = Assert::exception(function () use ($conn) { $conn->query('SELECT'); }, Dibi\DriverException::class, '%a% error in your SQL syntax;%a%', 1064);