Remove dependency to App in dba::connect
- Remove timing (can be done centrally) - Add charset parameter to dba::connect and dba::reconnect
This commit is contained in:
parent
e0ecbd0b95
commit
7d8c45dde8
1 changed files with 12 additions and 12 deletions
|
@ -29,21 +29,20 @@ class dba {
|
||||||
private static $db_user = '';
|
private static $db_user = '';
|
||||||
private static $db_pass = '';
|
private static $db_pass = '';
|
||||||
private static $db_name = '';
|
private static $db_name = '';
|
||||||
|
private static $db_charset = '';
|
||||||
|
|
||||||
public static function connect($serveraddr, $user, $pass, $db) {
|
public static function connect($serveraddr, $user, $pass, $db, $charset = null)
|
||||||
|
{
|
||||||
if (!is_null(self::$db) && self::connected()) {
|
if (!is_null(self::$db) && self::connected()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$a = get_app();
|
|
||||||
|
|
||||||
$stamp1 = microtime(true);
|
|
||||||
|
|
||||||
// We are storing these values for being able to perform a reconnect
|
// We are storing these values for being able to perform a reconnect
|
||||||
self::$db_serveraddr = $serveraddr;
|
self::$db_serveraddr = $serveraddr;
|
||||||
self::$db_user = $user;
|
self::$db_user = $user;
|
||||||
self::$db_pass = $pass;
|
self::$db_pass = $pass;
|
||||||
self::$db_name = $db;
|
self::$db_name = $db;
|
||||||
|
self::$db_charset = $charset;
|
||||||
|
|
||||||
$serveraddr = trim($serveraddr);
|
$serveraddr = trim($serveraddr);
|
||||||
|
|
||||||
|
@ -58,6 +57,7 @@ class dba {
|
||||||
$user = trim($user);
|
$user = trim($user);
|
||||||
$pass = trim($pass);
|
$pass = trim($pass);
|
||||||
$db = trim($db);
|
$db = trim($db);
|
||||||
|
$charset = trim($charset);
|
||||||
|
|
||||||
if (!(strlen($server) && strlen($user))) {
|
if (!(strlen($server) && strlen($user))) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -71,9 +71,10 @@ class dba {
|
||||||
$connect .= ";port=".$port;
|
$connect .= ";port=".$port;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($a->config["system"]["db_charset"])) {
|
if ($charset) {
|
||||||
$connect .= ";charset=".$a->config["system"]["db_charset"];
|
$connect .= ";charset=".$charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
self::$db = @new PDO($connect, $user, $pass);
|
self::$db = @new PDO($connect, $user, $pass);
|
||||||
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||||
|
@ -88,8 +89,8 @@ class dba {
|
||||||
if (!mysqli_connect_errno()) {
|
if (!mysqli_connect_errno()) {
|
||||||
self::$connected = true;
|
self::$connected = true;
|
||||||
|
|
||||||
if (isset($a->config["system"]["db_charset"])) {
|
if ($charset) {
|
||||||
self::$db->set_charset($a->config["system"]["db_charset"]);
|
self::$db->set_charset($charset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +100,6 @@ class dba {
|
||||||
self::$driver = null;
|
self::$driver = null;
|
||||||
self::$db = null;
|
self::$db = null;
|
||||||
}
|
}
|
||||||
$a->save_timestamp($stamp1, "network");
|
|
||||||
|
|
||||||
return self::$connected;
|
return self::$connected;
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ class dba {
|
||||||
public static function reconnect() {
|
public static function reconnect() {
|
||||||
self::disconnect();
|
self::disconnect();
|
||||||
|
|
||||||
$ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name);
|
$ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue