diff options
author | redmatrix <git@macgirvin.com> | 2016-05-17 22:32:49 -0700 |
---|---|---|
committer | redmatrix <git@macgirvin.com> | 2016-05-17 22:32:49 -0700 |
commit | 16f79b70e4e3fd572de4d3fc938802fa8c536bca (patch) | |
tree | 4abf49f0f9d3bc458743d227e9930e51938cb35a /include | |
parent | 43c2b22fca908cb5c7bb740855cbd9d04e085d62 (diff) | |
download | volse-hubzilla-16f79b70e4e3fd572de4d3fc938802fa8c536bca.tar.gz volse-hubzilla-16f79b70e4e3fd572de4d3fc938802fa8c536bca.tar.bz2 volse-hubzilla-16f79b70e4e3fd572de4d3fc938802fa8c536bca.zip |
experimental PDO DBA driver
Diffstat (limited to 'include')
-rwxr-xr-x | include/dba/dba_driver.php | 15 | ||||
-rwxr-xr-x | include/dba/dba_pdo.php | 95 |
2 files changed, 106 insertions, 4 deletions
diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 4a1752672..498bfffa7 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -32,16 +32,23 @@ function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) { if(is_null($port)) $set_port = 5432; $dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install); } else { +// require_once('include/dba/dba_pdo.php'); +// $dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install); +// } + if(class_exists('mysqli')) { if (is_null($port)) $set_port = ini_get("mysqli.default_port"); require_once('include/dba/dba_mysqli.php'); $dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install); - } else { - if (is_null($port)) $set_port = "3306"; - require_once('include/dba/dba_mysql.php'); - $dba = new dba_mysql($server, $set_port,$user,$pass,$db,$install); } } + +// else { +// if (is_null($port)) $set_port = "3306"; +// require_once('include/dba/dba_mysql.php'); +// $dba = new dba_mysql($server, $set_port,$user,$pass,$db,$install); +// } +// } if(is_object($dba) && $dba->connected) { $dns = (($dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php new file mode 100755 index 000000000..7255a2b66 --- /dev/null +++ b/include/dba/dba_pdo.php @@ -0,0 +1,95 @@ +<?php /** @file */ + +require_once('include/dba/dba_driver.php'); + +class dba_pdo extends dba_driver { + + + public $driver_dbtype = null; + + function connect($server,$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; + + + try { + $this->db = new PDO($dns,$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; + } + + $this->connected = true; + return true; + + } + + function q($sql) { + if((! $this->db) || (! $this->connected)) + return false; + + $this->error = ''; + $select = ((stripos($sql,'select') === 0) ? true : false); + + try { + $result = $this->db->query($sql); + } + catch(PDOException $e) { + + $this->error = $e->getMessage(); + if($this->error) { + db_logger('dba_mysqli: 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_mysqli: 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); + } + + $r = array(); + if($result) { + foreach($result as $x) { + $r[] = $x; + } + if($this->debug) { + db_logger('dba_pdo: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO); + } + } + return $r; + } + + function escape($str) { + if($this->db && $this->connected) { + return substr(substr(@$this->db->quote($str),1),0,-1); + } + } + + function close() { + if($this->db) + $this->db = null; + $this->connected = false; + } + + function getdriver() { + return 'pdo'; + } + +}
\ No newline at end of file |