streams/include/dba/dba_pdo.php

191 lines
5.3 KiB
PHP
Raw Normal View History

<?php
2016-05-18 05:32:49 +00:00
2024-03-10 02:40:50 +00:00
use Code\Lib\Time;
require_once 'include/dba/dba_driver.php';
2016-05-18 05:32:49 +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;
$this->driver_dbtype = substr($dsn,0,strpos($dsn,':'));
}
else {
2021-12-02 23:02:31 +00:00
$dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? ';port=' . $port : '');
}
$dsn .= ';dbname=' . $db;
// allow folks to over-ride the client encoding by setting it explicitly
// in the dsn. By default everything we do is in utf8 and for mysql this
// requires specifying utf8mb4.
if ($this->driver_dbtype === 'mysql' && !strpos($dsn,'charset=')) {
$dsn .= ';charset=utf8mb4';
}
if ($this->driver_type === 'pgsql' && !strpos($dsn,'client_encoding')) {
$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) {
2021-12-02 23:02:31 +00:00
if (file_exists('dbfail.out')) {
2024-03-10 02:40:50 +00:00
file_put_contents('dbfail.out', Time::convert() . "\nConnect: " . $e->getMessage() . "\n", FILE_APPEND);
2021-12-02 23:02:31 +00:00
}
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 || stripos($sql, 'show') === 0 || stripos($sql, 'explain') === 0) ? true : false);
2021-12-02 23:02:31 +00:00
try {
$result = $this->db->query($sql, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$this->error = $e->getMessage();
// Filter error 1062 which happens on race conditions and should be inside a transaction
// and rolled back. Put the message in the regular logfile just in case it is not; but
// keep the dbfail.out file clean to discover new development issues and bad sql.
2021-12-02 23:02:31 +00:00
if ($this->error) {
db_logger('dba_pdo: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
if (file_exists('dbfail.out') && strpos($this->error, 'Duplicate entry') === false) {
2024-03-10 02:40:50 +00:00
file_put_contents('dbfail.out', Time::convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND);
2021-12-02 23:02:31 +00:00
}
}
}
if (!($select)) {
if ($this->debug) {
db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL, (($result) ? LOG_INFO : LOG_ERR));
}
return (bool)$result;
2021-12-02 23:02:31 +00:00
}
$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;
}
}
public function escapebin($str)
{
2022-02-28 21:17:27 +00:00
return $this->escape($str);
2021-12-02 23:02:31 +00:00
}
public function unescapebin($str)
{
2022-02-28 21:17:27 +00:00
return $str;
2021-12-02 23:02:31 +00:00
}
public function getdriver()
{
return 'pdo';
}
2018-01-18 22:46:49 +00:00
}