commit
2d2ac98b3d
@ -12,67 +12,78 @@ class DBA {
|
|||||||
|
|
||||||
static public $dba = null;
|
static public $dba = null;
|
||||||
static public $dbtype = null;
|
static public $dbtype = null;
|
||||||
|
static public $scheme = 'mysql';
|
||||||
static public $logging = false;
|
static public $logging = false;
|
||||||
|
|
||||||
|
static public $install_script = 'install/schema_mysql.sql';
|
||||||
|
static public $null_date = '0001-01-01 00:00:00';
|
||||||
|
static public $utc_now = 'UTC_TIMESTAMP()';
|
||||||
|
static public $tquot = "`";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the database driver object.
|
* @brief Returns the database driver object.
|
||||||
*
|
*
|
||||||
* If available it will use PHP's mysqli otherwise mysql driver.
|
* @param string $server DB server name (or PDO dsn - e.g. mysqli:foobar.com;)
|
||||||
*
|
|
||||||
* @param string $server DB server name
|
|
||||||
* @param string $port DB port
|
* @param string $port DB port
|
||||||
* @param string $user DB username
|
* @param string $user DB username
|
||||||
* @param string $pass DB password
|
* @param string $pass DB password
|
||||||
* @param string $db database name
|
* @param string $db database name
|
||||||
* @param string $dbtype 0 for mysql, 1 for postgres
|
* @param string $dbtype 0 for mysql, 1 for postgres
|
||||||
* @param bool $install Defaults to false
|
* @param bool $install Defaults to false
|
||||||
* @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found.
|
* @return null|dba_driver A database driver object (dba_pdo) or null if no driver found.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static public function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
|
static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) {
|
||||||
|
|
||||||
self::$dba = null;
|
self::$dba = null;
|
||||||
|
|
||||||
self::$dbtype = intval($dbtype);
|
self::$dbtype = intval($dbtype);
|
||||||
$set_port = $port;
|
|
||||||
|
|
||||||
if(self::$dbtype == DBTYPE_POSTGRES) {
|
if(self::$dbtype == DBTYPE_POSTGRES) {
|
||||||
require_once('include/dba/dba_postgres.php');
|
if(!($port))
|
||||||
if(is_null($port)) $set_port = 5432;
|
$port = 5432;
|
||||||
self::$dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install);
|
|
||||||
|
self::$install_script = 'install/schema_postgres.sql';
|
||||||
|
self::$utc_now = "now() at time zone 'UTC'";
|
||||||
|
self::$tquot = '"';
|
||||||
|
self::$scheme = 'pgsql';
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
// Highly experimental at the present time.
|
// attempt to use the pdo driver compiled-in mysqli socket
|
||||||
// require_once('include/dba/dba_pdo.php');
|
// if using 'localhost' with no port configured.
|
||||||
// self::$dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install);
|
// If this is wrong you'll need to set the socket path specifically
|
||||||
// }
|
// using a server name of 'mysql:unix_socket=/socket/path', setting /socket/path
|
||||||
|
// as needed for your platform
|
||||||
|
|
||||||
if(class_exists('mysqli')) {
|
if((!($port)) && ($server !== 'localhost'))
|
||||||
if (is_null($port)) $set_port = ini_get("mysqli.default_port");
|
$port = 3306;
|
||||||
require_once('include/dba/dba_mysqli.php');
|
|
||||||
self::$dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Until we have a proper PDO driver, store the DB connection parameters for
|
require_once('include/dba/dba_pdo.php');
|
||||||
// plugins/addons which use PDO natively (such as cdav). This is wasteful as
|
self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install);
|
||||||
// it opens a separate connection to the DB, but saves a lot of effort re-writing
|
|
||||||
// third-party interfaces that are working and well tested.
|
|
||||||
|
|
||||||
|
|
||||||
if(is_object(self::$dba) && self::$dba->connected) {
|
if(is_object(self::$dba) && self::$dba->connected) {
|
||||||
if($server === 'localhost')
|
|
||||||
$port = $set_port;
|
if(strpbrk($server,':;')) {
|
||||||
$dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql')
|
$dsn = $server;
|
||||||
. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
|
}
|
||||||
. ';dbname=' . $db;
|
else {
|
||||||
self::$dba->pdo_set(array($dns,$user,$pass));
|
$dsn = self::$scheme . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port);
|
||||||
|
}
|
||||||
|
$dsn .= ';dbname=' . $db;
|
||||||
|
|
||||||
|
|
||||||
|
self::$dba->pdo_set(array($dsn,$user,$pass));
|
||||||
}
|
}
|
||||||
|
|
||||||
define('NULL_DATE', self::$dba->get_null_date());
|
define('NULL_DATE', self::$null_date);
|
||||||
define('ACTIVE_DBTYPE', self::$dbtype);
|
define('ACTIVE_DBTYPE', self::$dbtype);
|
||||||
define('TQUOT', self::$dba->get_table_quote());
|
define('TQUOT', self::$tquot);
|
||||||
return self::$dba;
|
return self::$dba;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,10 +97,6 @@ class DBA {
|
|||||||
*/
|
*/
|
||||||
abstract class dba_driver {
|
abstract class dba_driver {
|
||||||
// legacy behavior
|
// legacy behavior
|
||||||
const INSTALL_SCRIPT='install/schema_mysql.sql';
|
|
||||||
const NULL_DATE = '0001-01-01 00:00:00';
|
|
||||||
const UTC_NOW = 'UTC_TIMESTAMP()';
|
|
||||||
const TQUOT = "`";
|
|
||||||
|
|
||||||
protected $db;
|
protected $db;
|
||||||
protected $pdo = array();
|
protected $pdo = array();
|
||||||
@ -110,7 +117,7 @@ abstract class dba_driver {
|
|||||||
* @param string $db database name
|
* @param string $db database name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
abstract function connect($server, $port, $user, $pass, $db);
|
abstract function connect($server, $scheme, $port, $user, $pass, $db);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Perform a DB query with the SQL statement $sql.
|
* @brief Perform a DB query with the SQL statement $sql.
|
||||||
@ -144,31 +151,31 @@ abstract class dba_driver {
|
|||||||
*/
|
*/
|
||||||
abstract function getdriver();
|
abstract function getdriver();
|
||||||
|
|
||||||
function __construct($server, $port, $user,$pass,$db,$install = false) {
|
function __construct($server, $scheme, $port, $user,$pass,$db,$install = false) {
|
||||||
if(($install) && (! $this->install($server, $port, $user, $pass, $db))) {
|
if(($install) && (! $this->install($server, $scheme, $port, $user, $pass, $db))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->connect($server, $port, $user, $pass, $db);
|
$this->connect($server, $scheme, $port, $user, $pass, $db);
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_null_date() {
|
function get_null_date() {
|
||||||
return static::NULL_DATE;
|
return \DBA::$null_date;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_install_script() {
|
function get_install_script() {
|
||||||
return static::INSTALL_SCRIPT;
|
return \DBA::$install_script;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_table_quote() {
|
function get_table_quote() {
|
||||||
return static::TQUOT;
|
return \DBA::$tquot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function utcnow() {
|
function utcnow() {
|
||||||
return static::UTC_NOW;
|
return \DBA::$utc_now;
|
||||||
}
|
}
|
||||||
|
|
||||||
function install($server,$user,$pass,$db) {
|
function install($server,$scheme,$port,$user,$pass,$db) {
|
||||||
if (!(strlen($server) && strlen($user))){
|
if (!(strlen($server) && strlen($user))){
|
||||||
$this->connected = false;
|
$this->connected = false;
|
||||||
$this->db = null;
|
$this->db = null;
|
||||||
|
@ -5,7 +5,7 @@ require_once('include/dba/dba_driver.php');
|
|||||||
|
|
||||||
class dba_mysql extends dba_driver {
|
class dba_mysql extends dba_driver {
|
||||||
|
|
||||||
function connect($server, $port, $user,$pass,$db) {
|
function connect($server, $scheme, $port, $user,$pass,$db) {
|
||||||
$this->db = mysql_connect($server.":".$port,$user,$pass);
|
$this->db = mysql_connect($server.":".$port,$user,$pass);
|
||||||
if($this->db && mysql_select_db($db,$this->db)) {
|
if($this->db && mysql_select_db($db,$this->db)) {
|
||||||
$this->connected = true;
|
$this->connected = true;
|
||||||
|
@ -4,7 +4,7 @@ require_once('include/dba/dba_driver.php');
|
|||||||
|
|
||||||
class dba_mysqli extends dba_driver {
|
class dba_mysqli extends dba_driver {
|
||||||
|
|
||||||
function connect($server,$port,$user,$pass,$db) {
|
function connect($server,$scheme,$port,$user,$pass,$db) {
|
||||||
if($port)
|
if($port)
|
||||||
$this->db = new mysqli($server,$user,$pass,$db, $port);
|
$this->db = new mysqli($server,$user,$pass,$db, $port);
|
||||||
else
|
else
|
||||||
|
@ -7,16 +7,21 @@ class dba_pdo extends dba_driver {
|
|||||||
|
|
||||||
public $driver_dbtype = null;
|
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');
|
$this->driver_dbtype = $scheme;
|
||||||
$dns = $this->driver_dbtype
|
|
||||||
. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
|
|
||||||
. ';dbname=' . $db;
|
|
||||||
|
|
||||||
|
if(strpbrk($server,':;')) {
|
||||||
|
$dsn = $server;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$dsn = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port);
|
||||||
|
}
|
||||||
|
|
||||||
|
$dsn .= ';dbname=' . $db;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->db = new PDO($dns,$user,$pass);
|
$this->db = new PDO($dsn,$user,$pass);
|
||||||
$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
||||||
}
|
}
|
||||||
catch(PDOException $e) {
|
catch(PDOException $e) {
|
||||||
@ -27,6 +32,9 @@ class dba_pdo extends dba_driver {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($this->driver_dbtype === 'pgsql')
|
||||||
|
$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';");
|
||||||
|
|
||||||
$this->connected = true;
|
$this->connected = true;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -36,6 +44,12 @@ class dba_pdo extends dba_driver {
|
|||||||
if((! $this->db) || (! $this->connected))
|
if((! $this->db) || (! $this->connected))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if($this->driver_dbtype === 'pgsql') {
|
||||||
|
if(substr(rtrim($sql),-1,1) !== ';') {
|
||||||
|
$sql .= ';';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->error = '';
|
$this->error = '';
|
||||||
$select = ((stripos($sql,'select') === 0) ? true : false);
|
$select = ((stripos($sql,'select') === 0) ? true : false);
|
||||||
|
|
||||||
@ -88,6 +102,54 @@ class dba_pdo extends dba_driver {
|
|||||||
$this->connected = false;
|
$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') {
|
||||||
|
$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() {
|
function getdriver() {
|
||||||
return 'pdo';
|
return 'pdo';
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ class dba_postgres extends dba_driver {
|
|||||||
const UTC_NOW = "now() at time zone 'UTC'";
|
const UTC_NOW = "now() at time zone 'UTC'";
|
||||||
const TQUOT = '"';
|
const TQUOT = '"';
|
||||||
|
|
||||||
function connect($server,$port,$user,$pass,$db) {
|
function connect($server,$scheme,$port,$user,$pass,$db) {
|
||||||
if(!$port) $port = 5432;
|
if(!$port) $port = 5432;
|
||||||
$connstr = 'host=' . $server . ' port='.$port . ' user=' . $user . ' password=' . $pass . ' dbname='. $db;
|
$connstr = 'host=' . $server . ' port='.$port . ' user=' . $user . ' password=' . $pass . ' dbname='. $db;
|
||||||
$this->db = pg_connect($connstr);
|
$this->db = pg_connect($connstr);
|
||||||
|
Reference in New Issue
Block a user