diff options
Diffstat (limited to 'include/dba/dba_pdo.php')
-rwxr-xr-x | include/dba/dba_pdo.php | 84 |
1 files changed, 73 insertions, 11 deletions
diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 7255a2b66..f76e6cdd7 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -7,16 +7,21 @@ class dba_pdo extends dba_driver { public $driver_dbtype = null; - function connect($server,$port,$user,$pass,$db) { + function connect($server,$scheme,$port,$user,$pass,$db) { - $this->driver_dbtype = 'mysql'; // (($dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql'); - $dns = $this->driver_dbtype - . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) - . ';dbname=' . $db; + $this->driver_dbtype = $scheme; + if(strpbrk($server,':;')) { + $dsn = $server; + } + else { + $dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? '' : ';port=' . $port); + } + + $dsn .= ';dbname=' . $db; try { - $this->db = new PDO($dns,$user,$pass); + $this->db = new PDO($dsn,$user,$pass); $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { @@ -27,6 +32,9 @@ class dba_pdo extends dba_driver { return false; } + if($this->driver_dbtype === 'pgsql') + $this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';"); + $this->connected = true; return true; @@ -36,17 +44,23 @@ class dba_pdo extends dba_driver { if((! $this->db) || (! $this->connected)) return false; + if($this->driver_dbtype === 'pgsql') { + if(substr(rtrim($sql),-1,1) !== ';') { + $sql .= ';'; + } + } + $this->error = ''; $select = ((stripos($sql,'select') === 0) ? true : false); try { - $result = $this->db->query($sql); + $result = $this->db->query($sql, PDO::FETCH_ASSOC); } catch(PDOException $e) { $this->error = $e->getMessage(); if($this->error) { - db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR); + 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); } @@ -55,13 +69,13 @@ class dba_pdo extends dba_driver { if(!($select)) { if($this->debug) { - db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR)); + db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR)); } return $result; } if($this->debug) { - db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); + db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); } $r = array(); @@ -73,7 +87,7 @@ class dba_pdo extends dba_driver { db_logger('dba_pdo: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO); } } - return $r; + return (($this->error) ? false : $r); } function escape($str) { @@ -88,6 +102,54 @@ class dba_pdo extends dba_driver { $this->connected = false; } + function concat($fld,$sep) { + if($this->driver_dbtype === 'pgsql') { + return 'string_agg(' . $fld . ',\'' . $sep . '\')'; + } + else { + return 'GROUP_CONCAT(DISTINCT ' . $fld . ' SEPARATOR \'' . $sep . '\')'; + } + } + + 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. + + function escapebin($str) { + if($this->driver_dbtype === 'pgsql') { + return "\\\\x" . bin2hex($str); + } + else { + return $this->escape($str); + } + } + + function unescapebin($str) { + if($this->driver_dbtype === 'pgsql' && (! is_null($str))) { + $x = ''; + while(! feof($str)) { + $x .= fread($str,8192); + } + if(substr($x,0,2) === '\\x') { + $x = hex2bin(substr($x,2)); + } + return $x; + + } + else { + return $str; + } + } + + function getdriver() { return 'pdo'; } |