2020-07-02 10:34:05 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Extend;
|
|
|
|
|
|
|
|
use PDO as PDONative;
|
|
|
|
|
2020-07-03 03:19:33 +12:00
|
|
|
class PDO extends PDONative
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var PDONative
|
|
|
|
*/
|
|
|
|
protected $pdo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $dsn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $username;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $passwd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $options;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create A Proxy PDO Object
|
|
|
|
*/
|
|
|
|
public function __construct($dsn, $username = null, $passwd = null, $options = null)
|
|
|
|
{
|
|
|
|
$this->dsn = $dsn;
|
|
|
|
$this->username = $username;
|
|
|
|
$this->passwd = $passwd;
|
|
|
|
$this->options = $options;
|
|
|
|
|
|
|
|
$this->pdo = new PDONative($dsn, $username, $passwd, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAttribute($attribute, $value)
|
|
|
|
{
|
|
|
|
return $this->pdo->setAttribute($attribute, $value);
|
|
|
|
}
|
|
|
|
|
2021-10-07 10:23:58 +13:00
|
|
|
public function prepare($statement, $driver_options = null)
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
2020-07-03 06:03:30 +12:00
|
|
|
return new PDOStatement($this, $this->pdo->prepare($statement, []));
|
2020-07-02 10:34:05 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function quote($string, $parameter_type = PDONative::PARAM_STR)
|
|
|
|
{
|
|
|
|
return $this->pdo->quote($string, $parameter_type);
|
|
|
|
}
|
|
|
|
|
2021-05-27 23:22:18 +12:00
|
|
|
public function beginTransaction()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$result = $this->pdo->beginTransaction();
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$this->pdo = $this->reconnect();
|
|
|
|
$result = $this->pdo->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rollBack()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$result = $this->pdo->rollBack();
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$this->pdo = $this->reconnect();
|
2021-06-12 08:39:00 +12:00
|
|
|
return false;
|
2021-05-27 23:22:18 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function commit()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$result = $this->pdo->commit();
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$this->pdo = $this->reconnect();
|
|
|
|
$result = $this->pdo->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:08:29 +13:00
|
|
|
public function reconnect(): PDONative
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
2020-07-03 22:24:09 +12:00
|
|
|
$this->pdo = new PDONative($this->dsn, $this->username, $this->passwd, $this->options);
|
2020-10-25 19:15:36 +13:00
|
|
|
|
|
|
|
echo '[PDO] MySQL connection restarted'.PHP_EOL;
|
2020-07-03 22:24:09 +12:00
|
|
|
|
|
|
|
// Connection settings
|
|
|
|
$this->pdo->setAttribute(PDONative::ATTR_DEFAULT_FETCH_MODE, PDONative::FETCH_ASSOC); // Return arrays
|
|
|
|
$this->pdo->setAttribute(PDONative::ATTR_ERRMODE, PDONative::ERRMODE_EXCEPTION); // Handle all errors with exceptions
|
|
|
|
|
|
|
|
return $this->pdo;
|
2020-07-02 10:34:05 +12:00
|
|
|
}
|
2021-10-07 10:23:58 +13:00
|
|
|
}
|