Skip to content

Instantly share code, notes, and snippets.

@rasulsh
Created October 7, 2024 14:39
Show Gist options
  • Save rasulsh/c4243acbdfc2b94991996227d823548a to your computer and use it in GitHub Desktop.
Save rasulsh/c4243acbdfc2b94991996227d823548a to your computer and use it in GitHub Desktop.
<?php
namespace DDB;
class MySQLi {
private $link;
public function __construct($hostname, $username, $password, $database, $port = '3306') {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->link = new \mysqli($hostname, $username, $password, $database, $port);
if ($this->link->connect_error) {
throw new \Exception('Error: Could not make a database link (' . $this->link->connect_errno . ') ' . $this->link->connect_error);
}
$this->link->set_charset("utf8");
$this->link->query("SET SQL_MODE = ''");
}
public function query($sql) {
try {
$query = $this->link->query($sql);
if ($query instanceof \mysqli_result) {
$data = array();
while ($row = $query->fetch_assoc()) {
$data[] = $row;
}
$result = new \stdClass();
$result->num_rows = $query->num_rows;
$result->row = isset($data[0]) ? $data[0] : array();
$result->rows = $data;
$query->close();
return $result;
} else {
return true;
}
} catch (\mysqli_sql_exception $e) {
throw new \Exception('Error: ' . $e->getMessage() . '<br />Error No: ' . $e->getCode() . '<br />' . $sql);
}
}
public function escape($value) {
return $this->link->real_escape_string($value);
}
public function countAffected() {
return $this->link->affected_rows;
}
public function getLastId() {
return $this->link->insert_id;
}
public function __destruct() {
$this->link->close();
}
}
class TransactionWrapper {
private $db;
private $registry;
private $originalAutocommit;
public function __construct($registry, $hostname, $username, $password, $database, $port = '3306') {
$this->db = new MySQLi($hostname, $username, $password, $database, $port);
$this->registry = $registry;
}
public function start() {
$this->originalAutocommit = $this->db->query("SELECT @@autocommit")->row['@@autocommit'];
$this->db->query("SET autocommit=0");
$this->db->query("START TRANSACTION");
}
public function commit() {
$this->db->query("COMMIT");
$this->db->query("SET autocommit=" . $this->originalAutocommit);
}
public function rollback() {
$this->db->query("ROLLBACK");
$this->db->query("SET autocommit=" . $this->originalAutocommit);
}
public function query($sql) {
return $this->db->query($sql);
}
public function escape($value) {
return $this->db->escape($value);
}
public function countAffected() {
return $this->db->countAffected();
}
public function getLastId() {
return $this->db->getLastId();
}
public function execute($callback) {
$this->start();
try {
$result = $callback($this);
$this->commit();
return $result;
} catch (\Exception $e) {
$this->rollback();
$this->logError($e->getMessage());
return false;
}
}
private function logError($message) {
$log = $this->registry->get('log');
$log->write('Transaction error: ' . $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment