2020-07-02 10:34:05 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Extend;
|
|
|
|
|
|
|
|
use PDO as PDONative;
|
|
|
|
use PDOStatement as PDOStatementNative;
|
|
|
|
|
2020-07-03 03:19:33 +12:00
|
|
|
class PDOStatement extends PDOStatementNative
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
|
|
|
/**
|
2020-07-02 21:02:43 +12:00
|
|
|
* @var PDO
|
2020-07-02 10:34:05 +12:00
|
|
|
*/
|
|
|
|
protected $pdo;
|
|
|
|
|
2020-07-05 07:18:37 +12:00
|
|
|
/**
|
|
|
|
* Params
|
|
|
|
*/
|
|
|
|
protected $params = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Values
|
|
|
|
*/
|
|
|
|
protected $values = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Columns
|
|
|
|
*/
|
|
|
|
protected $columns = [];
|
|
|
|
|
2020-07-02 10:34:05 +12:00
|
|
|
/**
|
|
|
|
* @var PDOStatementNative
|
|
|
|
*/
|
|
|
|
protected $PDOStatement;
|
|
|
|
|
2020-07-02 21:02:43 +12:00
|
|
|
public function __construct(PDO &$pdo, PDOStatementNative $PDOStatement)
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->pdo = &$pdo;
|
2020-07-02 10:34:05 +12:00
|
|
|
$this->PDOStatement = $PDOStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bindValue($parameter, $value, $data_type = PDONative::PARAM_STR)
|
|
|
|
{
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->values[$parameter] = ['value' => $value, 'data_type' => $data_type];
|
|
|
|
|
2020-07-02 10:34:05 +12:00
|
|
|
$result = $this->PDOStatement->bindValue($parameter, $value, $data_type);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bindParam($parameter, &$variable, $data_type = PDONative::PARAM_STR, $length = null, $driver_options = null)
|
|
|
|
{
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->params[$parameter] = ['value' => &$variable, 'data_type' => $data_type, 'length' => $length, 'driver_options' => $driver_options];
|
|
|
|
|
2020-07-02 10:34:05 +12:00
|
|
|
$result = $this->PDOStatement->bindParam($parameter, $variable, $data_type, $length, $driver_options);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null)
|
|
|
|
{
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->columns[$column] = ['param' => &$param, 'type' => $type, 'maxlen' => $maxlen, 'driverdata' => $driverdata];
|
|
|
|
|
2020-07-02 10:34:05 +12:00
|
|
|
$result = $this->PDOStatement->bindColumn($column, $param, $type, $maxlen, $driverdata);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($input_parameters = null)
|
|
|
|
{
|
2020-07-02 21:02:43 +12:00
|
|
|
try {
|
|
|
|
$result = $this->PDOStatement->execute($input_parameters);
|
|
|
|
} catch (\Throwable $th) {
|
2020-07-05 06:22:04 +12:00
|
|
|
$this->pdo = $this->pdo->reconnect();
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->PDOStatement = $this->pdo->prepare($this->PDOStatement->queryString, []);
|
|
|
|
|
2020-10-28 08:44:15 +13:00
|
|
|
foreach ($this->values as $key => $set) {
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->PDOStatement->bindValue($key, $set['value'], $set['data_type']);
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:44:15 +13:00
|
|
|
foreach ($this->params as $key => $set) {
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->PDOStatement->bindParam($key, $set['variable'], $set['data_type'], $set['length'], $set['driver_options']);
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:44:15 +13:00
|
|
|
foreach ($this->columns as $key => $set) {
|
2020-07-05 07:18:37 +12:00
|
|
|
$this->PDOStatement->bindColumn($key, $set['param'], $set['type'], $set['maxlen'], $set['driverdata']);
|
|
|
|
}
|
2020-07-05 06:22:04 +12:00
|
|
|
|
2020-07-02 21:02:43 +12:00
|
|
|
$result = $this->PDOStatement->execute($input_parameters);
|
|
|
|
}
|
2020-07-02 10:34:05 +12:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetch($fetch_style = PDONative::FETCH_ASSOC, $cursor_orientation = PDONative::FETCH_ORI_NEXT, $cursor_offset = 0)
|
|
|
|
{
|
|
|
|
$result = $this->PDOStatement->fetch($fetch_style, $cursor_orientation, $cursor_offset);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:34:27 +13:00
|
|
|
/**
|
|
|
|
* Fetch All
|
|
|
|
*
|
|
|
|
* @param int $fetch_style
|
|
|
|
* @param mixed $fetch_args
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2020-10-27 19:34:02 +13:00
|
|
|
public function fetchAll(int $fetch_style = PDO::FETCH_BOTH, mixed ...$fetch_args)
|
2020-07-02 10:34:05 +12:00
|
|
|
{
|
|
|
|
$result = $this->PDOStatement->fetchAll();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|