Rename DBA::get_db to DBA::getConnection

- Rename DBA::$db to DBA::$connection
This commit is contained in:
Hypolite Petovan 2018-07-20 22:11:11 -04:00
parent 415b982d39
commit cdd8e4fd9d
2 changed files with 51 additions and 51 deletions

View file

@ -23,7 +23,7 @@ class DBA
public static $connected = false; public static $connected = false;
private static $server_info = ''; private static $server_info = '';
private static $db; private static $connection;
private static $driver; private static $driver;
private static $error = false; private static $error = false;
private static $errorno = 0; private static $errorno = 0;
@ -39,7 +39,7 @@ class DBA
public static function connect($serveraddr, $user, $pass, $db, $charset = null) public static function connect($serveraddr, $user, $pass, $db, $charset = null)
{ {
if (!is_null(self::$db) && self::connected()) { if (!is_null(self::$connection) && self::connected()) {
return true; return true;
} }
@ -83,8 +83,8 @@ class DBA
} }
try { try {
self::$db = @new PDO($connect, $user, $pass); self::$connection = @new PDO($connect, $user, $pass);
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$connected = true; self::$connected = true;
} catch (PDOException $e) { } catch (PDOException $e) {
} }
@ -94,16 +94,16 @@ class DBA
self::$driver = 'mysqli'; self::$driver = 'mysqli';
if ($port > 0) { if ($port > 0) {
self::$db = @new mysqli($server, $user, $pass, $db, $port); self::$connection = @new mysqli($server, $user, $pass, $db, $port);
} else { } else {
self::$db = @new mysqli($server, $user, $pass, $db); self::$connection = @new mysqli($server, $user, $pass, $db);
} }
if (!mysqli_connect_errno()) { if (!mysqli_connect_errno()) {
self::$connected = true; self::$connected = true;
if ($charset) { if ($charset) {
self::$db->set_charset($charset); self::$connection->set_charset($charset);
} }
} }
} }
@ -111,7 +111,7 @@ class DBA
// No suitable SQL driver was found. // No suitable SQL driver was found.
if (!self::$connected) { if (!self::$connected) {
self::$driver = null; self::$driver = null;
self::$db = null; self::$connection = null;
} }
return self::$connected; return self::$connected;
@ -122,17 +122,17 @@ class DBA
*/ */
public static function disconnect() public static function disconnect()
{ {
if (is_null(self::$db)) { if (is_null(self::$connection)) {
return; return;
} }
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
self::$db = null; self::$connection = null;
break; break;
case 'mysqli': case 'mysqli':
self::$db->close(); self::$connection->close();
self::$db = null; self::$connection = null;
break; break;
} }
} }
@ -151,9 +151,9 @@ class DBA
* Return the database object. * Return the database object.
* @return PDO|mysqli * @return PDO|mysqli
*/ */
public static function get_db() public static function getConnection()
{ {
return self::$db; return self::$connection;
} }
/** /**
@ -168,10 +168,10 @@ class DBA
if (self::$server_info == '') { if (self::$server_info == '') {
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
self::$server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION); self::$server_info = self::$connection->getAttribute(PDO::ATTR_SERVER_VERSION);
break; break;
case 'mysqli': case 'mysqli':
self::$server_info = self::$db->server_info; self::$server_info = self::$connection->server_info;
break; break;
} }
} }
@ -249,16 +249,16 @@ class DBA
public static function escape($str) { public static function escape($str) {
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1); return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
case 'mysqli': case 'mysqli':
return @self::$db->real_escape_string($str); return @self::$connection->real_escape_string($str);
} }
} }
public static function connected() { public static function connected() {
$connected = false; $connected = false;
if (is_null(self::$db)) { if (is_null(self::$connection)) {
return false; return false;
} }
@ -271,7 +271,7 @@ class DBA
} }
break; break;
case 'mysqli': case 'mysqli':
$connected = self::$db->ping(); $connected = self::$connection->ping();
break; break;
} }
return $connected; return $connected;
@ -425,8 +425,8 @@ class DBA
case 'pdo': case 'pdo':
// If there are no arguments we use "query" // If there are no arguments we use "query"
if (count($args) == 0) { if (count($args) == 0) {
if (!$retval = self::$db->query($sql)) { if (!$retval = self::$connection->query($sql)) {
$errorInfo = self::$db->errorInfo(); $errorInfo = self::$connection->errorInfo();
self::$error = $errorInfo[2]; self::$error = $errorInfo[2];
self::$errorno = $errorInfo[1]; self::$errorno = $errorInfo[1];
$retval = false; $retval = false;
@ -436,8 +436,8 @@ class DBA
break; break;
} }
if (!$stmt = self::$db->prepare($sql)) { if (!$stmt = self::$connection->prepare($sql)) {
$errorInfo = self::$db->errorInfo(); $errorInfo = self::$connection->errorInfo();
self::$error = $errorInfo[2]; self::$error = $errorInfo[2];
self::$errorno = $errorInfo[1]; self::$errorno = $errorInfo[1];
$retval = false; $retval = false;
@ -471,22 +471,22 @@ class DBA
// The fallback routine is called as well when there are no arguments // The fallback routine is called as well when there are no arguments
if (!$can_be_prepared || (count($args) == 0)) { if (!$can_be_prepared || (count($args) == 0)) {
$retval = self::$db->query(self::replaceParameters($sql, $args)); $retval = self::$connection->query(self::replaceParameters($sql, $args));
if (self::$db->errno) { if (self::$connection->errno) {
self::$error = self::$db->error; self::$error = self::$connection->error;
self::$errorno = self::$db->errno; self::$errorno = self::$connection->errno;
$retval = false; $retval = false;
} else { } else {
if (isset($retval->num_rows)) { if (isset($retval->num_rows)) {
self::$affected_rows = $retval->num_rows; self::$affected_rows = $retval->num_rows;
} else { } else {
self::$affected_rows = self::$db->affected_rows; self::$affected_rows = self::$connection->affected_rows;
} }
} }
break; break;
} }
$stmt = self::$db->stmt_init(); $stmt = self::$connection->stmt_init();
if (!$stmt->prepare($sql)) { if (!$stmt->prepare($sql)) {
self::$error = $stmt->error; self::$error = $stmt->error;
@ -516,8 +516,8 @@ class DBA
} }
if (!$stmt->execute()) { if (!$stmt->execute()) {
self::$error = self::$db->error; self::$error = self::$connection->error;
self::$errorno = self::$db->errno; self::$errorno = self::$connection->errno;
$retval = false; $retval = false;
} else { } else {
$stmt->store_result(); $stmt->store_result();
@ -841,10 +841,10 @@ class DBA
public static function lastInsertId() { public static function lastInsertId() {
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
$id = self::$db->lastInsertId(); $id = self::$connection->lastInsertId();
break; break;
case 'mysqli': case 'mysqli':
$id = self::$db->insert_id; $id = self::$connection->insert_id;
break; break;
} }
return $id; return $id;
@ -863,22 +863,22 @@ class DBA
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
if (self::$driver == 'pdo') { if (self::$driver == 'pdo') {
self::e("SET autocommit=0"); self::e("SET autocommit=0");
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} else { } else {
self::$db->autocommit(false); self::$connection->autocommit(false);
} }
$success = self::e("LOCK TABLES `".self::escape($table)."` WRITE"); $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
if (self::$driver == 'pdo') { if (self::$driver == 'pdo') {
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} }
if (!$success) { if (!$success) {
if (self::$driver == 'pdo') { if (self::$driver == 'pdo') {
self::e("SET autocommit=1"); self::e("SET autocommit=1");
} else { } else {
self::$db->autocommit(true); self::$connection->autocommit(true);
} }
} else { } else {
self::$in_transaction = true; self::$in_transaction = true;
@ -896,16 +896,16 @@ class DBA
self::performCommit(); self::performCommit();
if (self::$driver == 'pdo') { if (self::$driver == 'pdo') {
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} }
$success = self::e("UNLOCK TABLES"); $success = self::e("UNLOCK TABLES");
if (self::$driver == 'pdo') { if (self::$driver == 'pdo') {
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::e("SET autocommit=1"); self::e("SET autocommit=1");
} else { } else {
self::$db->autocommit(true); self::$connection->autocommit(true);
} }
self::$in_transaction = false; self::$in_transaction = false;
@ -924,15 +924,15 @@ class DBA
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
if (self::$db->inTransaction()) { if (self::$connection->inTransaction()) {
break; break;
} }
if (!self::$db->beginTransaction()) { if (!self::$connection->beginTransaction()) {
return false; return false;
} }
break; break;
case 'mysqli': case 'mysqli':
if (!self::$db->begin_transaction()) { if (!self::$connection->begin_transaction()) {
return false; return false;
} }
break; break;
@ -946,12 +946,12 @@ class DBA
{ {
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
if (!self::$db->inTransaction()) { if (!self::$connection->inTransaction()) {
return true; return true;
} }
return self::$db->commit(); return self::$connection->commit();
case 'mysqli': case 'mysqli':
return self::$db->commit(); return self::$connection->commit();
} }
return true; return true;
} }
@ -979,14 +979,14 @@ class DBA
switch (self::$driver) { switch (self::$driver) {
case 'pdo': case 'pdo':
if (!self::$db->inTransaction()) { if (!self::$connection->inTransaction()) {
$ret = true; $ret = true;
break; break;
} }
$ret = self::$db->rollBack(); $ret = self::$connection->rollBack();
break; break;
case 'mysqli': case 'mysqli':
$ret = self::$db->rollback(); $ret = self::$connection->rollback();
break; break;
} }
self::$in_transaction = false; self::$in_transaction = false;

View file

@ -36,7 +36,7 @@ abstract class DatabaseTest extends TestCase
$this->markTestSkipped('Could not connect to the database.'); $this->markTestSkipped('Could not connect to the database.');
} }
return $this->createDefaultDBConnection(DBA::get_db(), getenv('MYSQL_DATABASE')); return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
} }
/** /**