Merge pull request #3776 from annando/initialize-sql

Checks if the database variable is initialized
This commit is contained in:
Tobias Diekershoff 2017-10-11 07:14:46 +02:00 committed by GitHub
commit 57be4f047e

View file

@ -116,6 +116,22 @@ class dba {
self::$dbo = $this; self::$dbo = $this;
} }
/**
* @brief Checks if the database object is initialized
*
* This is a possible bugfix for something that doesn't occur for me.
* There seems to be situations, where the object isn't initialized.
*/
private static function initialize() {
if (!is_object(self::$dbo)) {
global $db;
self::$dbo = $db;
if (!is_object(self::$dbo)) {
die('Database is uninitialized!');
}
}
}
/** /**
* @brief Returns the MySQL server version string * @brief Returns the MySQL server version string
* *
@ -375,6 +391,8 @@ class dba {
* @return object statement object * @return object statement object
*/ */
public static function p($sql) { public static function p($sql) {
self::initialize();
$a = get_app(); $a = get_app();
$stamp1 = microtime(true); $stamp1 = microtime(true);
@ -585,6 +603,8 @@ class dba {
* @return boolean Was the query successfull? False is returned only if an error occurred * @return boolean Was the query successfull? False is returned only if an error occurred
*/ */
public static function e($sql) { public static function e($sql) {
self::initialize();
$a = get_app(); $a = get_app();
$stamp = microtime(true); $stamp = microtime(true);
@ -635,6 +655,8 @@ class dba {
* @return boolean Are there rows for that condition? * @return boolean Are there rows for that condition?
*/ */
public static function exists($table, $condition) { public static function exists($table, $condition) {
self::initialize();
if (empty($table)) { if (empty($table)) {
return false; return false;
} }
@ -669,6 +691,8 @@ class dba {
* @return array first row of query * @return array first row of query
*/ */
public static function fetch_first($sql) { public static function fetch_first($sql) {
self::initialize();
$params = self::getParam(func_get_args()); $params = self::getParam(func_get_args());
$stmt = self::p($sql, $params); $stmt = self::p($sql, $params);
@ -690,6 +714,8 @@ class dba {
* @return int Number of rows * @return int Number of rows
*/ */
public static function affected_rows() { public static function affected_rows() {
self::initialize();
return self::$dbo->affected_rows; return self::$dbo->affected_rows;
} }
@ -700,6 +726,8 @@ class dba {
* @return int Number of columns * @return int Number of columns
*/ */
public static function columnCount($stmt) { public static function columnCount($stmt) {
self::initialize();
if (!is_object($stmt)) { if (!is_object($stmt)) {
return 0; return 0;
} }
@ -720,6 +748,8 @@ class dba {
* @return int Number of rows * @return int Number of rows
*/ */
public static function num_rows($stmt) { public static function num_rows($stmt) {
self::initialize();
if (!is_object($stmt)) { if (!is_object($stmt)) {
return 0; return 0;
} }
@ -741,6 +771,8 @@ class dba {
* @return array current row * @return array current row
*/ */
public static function fetch($stmt) { public static function fetch($stmt) {
self::initialize();
if (!is_object($stmt)) { if (!is_object($stmt)) {
return false; return false;
} }
@ -795,6 +827,8 @@ class dba {
* @return boolean was the insert successfull? * @return boolean was the insert successfull?
*/ */
public static function insert($table, $param, $on_duplicate_update = false) { public static function insert($table, $param, $on_duplicate_update = false) {
self::initialize();
$sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (". $sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
substr(str_repeat("?, ", count($param)), 0, -2).")"; substr(str_repeat("?, ", count($param)), 0, -2).")";
@ -814,6 +848,8 @@ class dba {
* @return integer Last inserted id * @return integer Last inserted id
*/ */
public static function lastInsertId() { public static function lastInsertId() {
self::initialize();
switch (self::$dbo->driver) { switch (self::$dbo->driver) {
case 'pdo': case 'pdo':
$id = self::$dbo->db->lastInsertId(); $id = self::$dbo->db->lastInsertId();
@ -838,6 +874,8 @@ class dba {
* @return boolean was the lock successful? * @return boolean was the lock successful?
*/ */
public static function lock($table) { public static function lock($table) {
self::initialize();
// 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
self::e("SET autocommit=0"); self::e("SET autocommit=0");
$success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE"); $success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
@ -855,6 +893,8 @@ class dba {
* @return boolean was the unlock successful? * @return boolean was the unlock successful?
*/ */
public static function unlock() { public static function unlock() {
self::initialize();
// 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
self::e("COMMIT"); self::e("COMMIT");
$success = self::e("UNLOCK TABLES"); $success = self::e("UNLOCK TABLES");
@ -869,6 +909,8 @@ class dba {
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function transaction() { public static function transaction() {
self::initialize();
if (!self::e('COMMIT')) { if (!self::e('COMMIT')) {
return false; return false;
} }
@ -885,6 +927,8 @@ class dba {
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function commit() { public static function commit() {
self::initialize();
if (!self::e('COMMIT')) { if (!self::e('COMMIT')) {
return false; return false;
} }
@ -898,6 +942,8 @@ class dba {
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function rollback() { public static function rollback() {
self::initialize();
if (!self::e('ROLLBACK')) { if (!self::e('ROLLBACK')) {
return false; return false;
} }
@ -937,6 +983,7 @@ class dba {
* @return boolean|array was the delete successfull? When $in_process is set: deletion data * @return boolean|array was the delete successfull? When $in_process is set: deletion data
*/ */
public static function delete($table, $param, $in_process = false, &$callstack = array()) { public static function delete($table, $param, $in_process = false, &$callstack = array()) {
self::initialize();
$commands = array(); $commands = array();
@ -1100,6 +1147,7 @@ class dba {
* @return boolean was the update successfull? * @return boolean was the update successfull?
*/ */
public static function update($table, $fields, $condition, $old_fields = array()) { public static function update($table, $fields, $condition, $old_fields = array()) {
self::initialize();
$table = self::$dbo->escape($table); $table = self::$dbo->escape($table);
@ -1178,6 +1226,8 @@ class dba {
* $data = dba::select($table, $fields, $condition, $params); * $data = dba::select($table, $fields, $condition, $params);
*/ */
public static function select($table, $fields = array(), $condition = array(), $params = array()) { public static function select($table, $fields = array(), $condition = array(), $params = array()) {
self::initialize();
if ($table == '') { if ($table == '') {
return false; return false;
} }
@ -1245,6 +1295,8 @@ class dba {
* @return array Data array * @return array Data array
*/ */
public static function inArray($stmt, $do_close = true) { public static function inArray($stmt, $do_close = true) {
self::initialize();
if (is_bool($stmt)) { if (is_bool($stmt)) {
return $stmt; return $stmt;
} }
@ -1265,6 +1317,8 @@ class dba {
* @return string Error number (0 if no error) * @return string Error number (0 if no error)
*/ */
public static function errorNo() { public static function errorNo() {
self::initialize();
return self::$dbo->errorno; return self::$dbo->errorno;
} }
@ -1274,6 +1328,8 @@ class dba {
* @return string Error message ('' if no error) * @return string Error message ('' if no error)
*/ */
public static function errorMessage() { public static function errorMessage() {
self::initialize();
return self::$dbo->error; return self::$dbo->error;
} }
@ -1284,6 +1340,8 @@ class dba {
* @return boolean was the close successfull? * @return boolean was the close successfull?
*/ */
public static function close($stmt) { public static function close($stmt) {
self::initialize();
if (!is_object($stmt)) { if (!is_object($stmt)) {
return false; return false;
} }