2017-12-07 23:26:00 +00:00
|
|
|
<?php
|
2016-05-18 05:32:49 +00:00
|
|
|
|
2017-12-07 23:26:00 +00:00
|
|
|
require_once 'include/dba/dba_driver.php';
|
2016-05-18 05:32:49 +00:00
|
|
|
|
2017-12-07 23:26:00 +00:00
|
|
|
/**
|
|
|
|
* @brief PDO based database driver.
|
|
|
|
*
|
|
|
|
*/
|
2021-12-02 23:02:31 +00:00
|
|
|
class dba_pdo extends dba_driver
|
|
|
|
{
|
|
|
|
|
|
|
|
public $driver_dbtype = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @see dba_driver::connect()
|
|
|
|
*/
|
|
|
|
public function connect($server, $scheme, $port, $user, $pass, $db)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->driver_dbtype = $scheme;
|
|
|
|
|
|
|
|
if (strpbrk($server, ':;')) {
|
|
|
|
$dsn = $server;
|
|
|
|
} else {
|
|
|
|
$dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? ';port=' . $port : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$dsn .= ';dbname=' . $db;
|
2022-02-08 23:35:15 +00:00
|
|
|
if ($this->driver_dbtype === 'mysql') {
|
|
|
|
$dsn .= ';charset=utf8mb4';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dsn .= ";options='--client_encoding=UTF8'";
|
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
try {
|
|
|
|
$this->db = new PDO($dsn, $user, $pass);
|
|
|
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
if (file_exists('dbfail.out')) {
|
|
|
|
file_put_contents('dbfail.out', datetime_convert() . "\nConnect: " . $e->getMessage() . "\n", FILE_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
2021-12-02 23:02:31 +00:00
|
|
|
$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';");
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
$this->connected = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @return bool|array|PDOStatement
|
|
|
|
* - \b false if not connected or PDOException occured on query
|
|
|
|
* - \b array with results on a SELECT query
|
|
|
|
* - \b PDOStatement on a non SELECT SQL query
|
|
|
|
* @see dba_driver::q()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function q($sql)
|
|
|
|
{
|
2021-12-03 03:01:39 +00:00
|
|
|
if ((!$this->db) || (!$this->connected)) {
|
2021-12-02 23:02:31 +00:00
|
|
|
return false;
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
|
|
|
if (substr(rtrim($sql), -1, 1) !== ';') {
|
|
|
|
$sql .= ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = null;
|
|
|
|
$this->error = '';
|
|
|
|
$select = ((stripos($sql, 'select') === 0) ? true : false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = $this->db->query($sql, PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->error = $e->getMessage();
|
|
|
|
if ($this->error) {
|
|
|
|
db_logger('dba_pdo: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
|
|
|
|
if (file_exists('dbfail.out')) {
|
|
|
|
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($select)) {
|
|
|
|
if ($this->debug) {
|
|
|
|
db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL, (($result) ? LOG_INFO : LOG_ERR));
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = [];
|
|
|
|
if ($result) {
|
|
|
|
foreach ($result as $x) {
|
|
|
|
$r[] = $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->debug) {
|
|
|
|
db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returned ' . count($r) . ' results.', LOGGER_NORMAL, LOG_INFO);
|
|
|
|
if (intval($this->debug) > 1) {
|
|
|
|
db_logger('dba_pdo: ' . printable(print_r($r, true)), LOGGER_NORMAL, LOG_INFO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (($this->error) ? false : $r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function escape($str)
|
|
|
|
{
|
|
|
|
if ($this->db && $this->connected) {
|
|
|
|
return substr(substr(@$this->db->quote($str), 1), 0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close()
|
|
|
|
{
|
2021-12-03 03:01:39 +00:00
|
|
|
if ($this->db) {
|
2021-12-02 23:02:31 +00:00
|
|
|
$this->db = null;
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
$this->connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function concat($fld, $sep)
|
|
|
|
{
|
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
|
|
|
return 'string_agg(' . $fld . ',\'' . $sep . '\')';
|
|
|
|
} else {
|
|
|
|
return 'GROUP_CONCAT(DISTINCT ' . $fld . ' SEPARATOR \'' . $sep . '\')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function use_index($str)
|
|
|
|
{
|
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return 'USE INDEX( ' . $str . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quote_interval($txt)
|
|
|
|
{
|
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
|
|
|
return "'$txt'";
|
|
|
|
} else {
|
|
|
|
return $txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These two functions assume that postgres standard_conforming_strings is set to off;
|
|
|
|
// which we perform during DB open.
|
|
|
|
|
|
|
|
public function escapebin($str)
|
|
|
|
{
|
|
|
|
if ($this->driver_dbtype === 'pgsql') {
|
|
|
|
return "\\\\x" . bin2hex($str);
|
|
|
|
} else {
|
|
|
|
return $this->escape($str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unescapebin($str)
|
|
|
|
{
|
|
|
|
if (gettype($str) === 'resource') {
|
|
|
|
$x = '';
|
|
|
|
while (!feof($str)) {
|
|
|
|
$x .= fread($str, 8192);
|
|
|
|
}
|
|
|
|
if (substr($x, 0, 2) === '\\x') {
|
|
|
|
$x = hex2bin(substr($x, 2));
|
|
|
|
}
|
|
|
|
return $x;
|
|
|
|
} else {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getdriver()
|
|
|
|
{
|
|
|
|
return 'pdo';
|
|
|
|
}
|
2018-01-18 22:46:49 +00:00
|
|
|
}
|