mirror of
https://github.com/dg/dibi.git
synced 2025-08-09 15:47:23 +02:00
MySqliDriver: refactoring, uses OOP style
This commit is contained in:
@@ -57,7 +57,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
}
|
}
|
||||||
|
|
||||||
mysqli_report(MYSQLI_REPORT_OFF);
|
mysqli_report(MYSQLI_REPORT_OFF);
|
||||||
if (isset($config['resource'])) {
|
if (isset($config['resource']) && $config['resource'] instanceof \mysqli) {
|
||||||
$this->connection = $config['resource'];
|
$this->connection = $config['resource'];
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -87,18 +87,26 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
$this->connection = mysqli_init();
|
$this->connection = mysqli_init();
|
||||||
if (isset($config['options'])) {
|
if (isset($config['options'])) {
|
||||||
foreach ($config['options'] as $key => $value) {
|
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()) {
|
if ($this->connection->connect_errno) {
|
||||||
throw new Dibi\DriverException(mysqli_connect_error(), $errno);
|
throw new Dibi\DriverException($this->connection->connect_error, $this->connection->connect_errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($config['charset'])) {
|
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]'");
|
$this->query("SET NAMES '$config[charset]'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,7 +128,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
*/
|
*/
|
||||||
public function disconnect(): void
|
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
|
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)) {
|
if ($code = mysqli_errno($this->connection)) {
|
||||||
throw static::createException(mysqli_error($this->connection), $code, $sql);
|
throw static::createException(mysqli_error($this->connection), $code, $sql);
|
||||||
|
|
||||||
} elseif (is_object($res)) {
|
} elseif ($res instanceof \mysqli_result) {
|
||||||
return $this->createResultDriver($res);
|
return $this->createResultDriver($res);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -165,7 +173,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
public function getInfo(): array
|
public function getInfo(): array
|
||||||
{
|
{
|
||||||
$res = [];
|
$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()) {
|
if (preg_last_error()) {
|
||||||
throw new Dibi\PcreException;
|
throw new Dibi\PcreException;
|
||||||
}
|
}
|
||||||
@@ -182,7 +190,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
*/
|
*/
|
||||||
public function getAffectedRows(): ?int
|
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
|
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
|
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
|
public function escapeBinary(string $value): string
|
||||||
{
|
{
|
||||||
return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'";
|
return "_binary'" . $this->connection->escape_string($value) . "'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -55,7 +55,7 @@ class MySqliResult implements Dibi\ResultDriver
|
|||||||
if (!$this->buffered) {
|
if (!$this->buffered) {
|
||||||
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
|
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
|
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) {
|
if (!$this->buffered) {
|
||||||
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
|
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
|
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';
|
$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 = [];
|
$columns = [];
|
||||||
for ($i = 0; $i < $count; $i++) {
|
for ($i = 0; $i < $count; $i++) {
|
||||||
$row = (array) mysqli_fetch_field_direct($this->resultSet, $i);
|
$row = (array) $this->resultSet->fetch_field_direct($i);
|
||||||
$columns[] = [
|
$columns[] = [
|
||||||
'name' => $row['name'],
|
'name' => $row['name'],
|
||||||
'table' => $row['orgtable'],
|
'table' => $row['orgtable'],
|
||||||
|
@@ -15,6 +15,18 @@ $conn = new Dibi\Connection($config);
|
|||||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
$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) {
|
$e = Assert::exception(function () use ($conn) {
|
||||||
$conn->query('SELECT');
|
$conn->query('SELECT');
|
||||||
}, Dibi\DriverException::class, '%a% error in your SQL syntax;%a%', 1064);
|
}, Dibi\DriverException::class, '%a% error in your SQL syntax;%a%', 1064);
|
||||||
|
Reference in New Issue
Block a user