Commit ea35ad41 authored by Alexander Butenko's avatar Alexander Butenko

Do not connect to mysql in __construct

parent dd69e990
...@@ -167,11 +167,6 @@ class MysqliDb ...@@ -167,11 +167,6 @@ class MysqliDb
$this->isSubQuery = true; $this->isSubQuery = true;
return; return;
} }
// for subqueries we do not need database connection and redefine root instance
if (!is_object ($host))
$this->connect();
$this->setPrefix(); $this->setPrefix();
self::$_instance = $this; self::$_instance = $this;
} }
...@@ -192,8 +187,19 @@ class MysqliDb ...@@ -192,8 +187,19 @@ class MysqliDb
or die('There was a problem connecting to the database'); or die('There was a problem connecting to the database');
if ($this->charset) if ($this->charset)
$this->_mysqli->set_charset ($this->charset); $this->mysqli()->set_charset ($this->charset);
}
/**
* A method to get mysqli object or create it in case needed
*/
public function mysqli ()
{
if (!$this->_mysqli)
$this->connect();
return $this->_mysqli;
} }
/** /**
* A method of returning the static instance to allow access to the * A method of returning the static instance to allow access to the
* instantiated object from within another class. * instantiated object from within another class.
...@@ -643,7 +649,7 @@ class MysqliDb ...@@ -643,7 +649,7 @@ class MysqliDb
*/ */
public function getInsertId() public function getInsertId()
{ {
return $this->_mysqli->insert_id; return $this->mysqli()->insert_id;
} }
/** /**
...@@ -655,7 +661,7 @@ class MysqliDb ...@@ -655,7 +661,7 @@ class MysqliDb
*/ */
public function escape($str) public function escape($str)
{ {
return $this->_mysqli->real_escape_string($str); return $this->mysqli()->real_escape_string($str);
} }
/** /**
...@@ -667,7 +673,7 @@ class MysqliDb ...@@ -667,7 +673,7 @@ class MysqliDb
* @return bool True if connection is up * @return bool True if connection is up
*/ */
public function ping() { public function ping() {
return $this->_mysqli->ping(); return $this->mysqli()->ping();
} }
/** /**
...@@ -876,11 +882,11 @@ class MysqliDb ...@@ -876,11 +882,11 @@ class MysqliDb
array_push($results, $x); array_push($results, $x);
} }
// stored procedures sometimes can return more then 1 resultset // stored procedures sometimes can return more then 1 resultset
if ($this->_mysqli->more_results()) if ($this->mysqli()->more_results())
$this->_mysqli->next_result(); $this->mysqli()->next_result();
if (in_array ('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) { if (in_array ('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) {
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS()'); $stmt = $this->mysqli()->query ('SELECT FOUND_ROWS()');
$totalCount = $stmt->fetch_row(); $totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0]; $this->totalCount = $totalCount[0];
} }
...@@ -1074,8 +1080,8 @@ class MysqliDb ...@@ -1074,8 +1080,8 @@ class MysqliDb
*/ */
protected function _prepareQuery() protected function _prepareQuery()
{ {
if (!$stmt = $this->_mysqli->prepare($this->_query)) { if (!$stmt = $this->mysqli()->prepare($this->_query)) {
trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR); trigger_error("Problem preparing query ($this->_query) " . $this->mysqli()->error, E_USER_ERROR);
} }
if ($this->traceEnabled) if ($this->traceEnabled)
$this->traceStartQ = microtime (true); $this->traceStartQ = microtime (true);
...@@ -1091,7 +1097,7 @@ class MysqliDb ...@@ -1091,7 +1097,7 @@ class MysqliDb
if (!$this->isSubQuery) if (!$this->isSubQuery)
return; return;
if ($this->_mysqli) if ($this->_mysqli)
$this->_mysqli->close(); $this->mysqli()->close();
} }
/** /**
...@@ -1151,7 +1157,9 @@ class MysqliDb ...@@ -1151,7 +1157,9 @@ class MysqliDb
* @return string * @return string
*/ */
public function getLastError () { public function getLastError () {
return trim ($this->_stmtError . " " . $this->_mysqli->error); if (!$this->_mysqli)
return "mysqli is null";
return trim ($this->_stmtError . " " . $this->mysqli()->error);
} }
/** /**
...@@ -1276,7 +1284,7 @@ class MysqliDb ...@@ -1276,7 +1284,7 @@ class MysqliDb
* @uses register_shutdown_function(array($this, "_transaction_shutdown_check")) * @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
*/ */
public function startTransaction () { public function startTransaction () {
$this->_mysqli->autocommit (false); $this->mysqli()->autocommit (false);
$this->_transaction_in_progress = true; $this->_transaction_in_progress = true;
register_shutdown_function (array ($this, "_transaction_status_check")); register_shutdown_function (array ($this, "_transaction_status_check"));
} }
...@@ -1288,9 +1296,9 @@ class MysqliDb ...@@ -1288,9 +1296,9 @@ class MysqliDb
* @uses mysqli->autocommit(true); * @uses mysqli->autocommit(true);
*/ */
public function commit () { public function commit () {
$this->_mysqli->commit (); $this->mysqli()->commit ();
$this->_transaction_in_progress = false; $this->_transaction_in_progress = false;
$this->_mysqli->autocommit (true); $this->mysqli()->autocommit (true);
} }
/** /**
...@@ -1300,9 +1308,9 @@ class MysqliDb ...@@ -1300,9 +1308,9 @@ class MysqliDb
* @uses mysqli->autocommit(true); * @uses mysqli->autocommit(true);
*/ */
public function rollback () { public function rollback () {
$this->_mysqli->rollback (); $this->mysqli()->rollback ();
$this->_transaction_in_progress = false; $this->_transaction_in_progress = false;
$this->_mysqli->autocommit (true); $this->mysqli()->autocommit (true);
} }
/** /**
......
...@@ -112,6 +112,12 @@ foreach ($tables as $name => $fields) { ...@@ -112,6 +112,12 @@ foreach ($tables as $name => $fields) {
createTable ($prefix.$name, $fields); createTable ($prefix.$name, $fields);
} }
if (!$db->ping()) {
echo "db is not up";
exit;
}
$str = $db->escape ("te'st");
// insert test with autoincrement // insert test with autoincrement
foreach ($data as $name => $datas) { foreach ($data as $name => $datas) {
foreach ($datas as $d) { foreach ($datas as $d) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment