DB::disconnect() allows easy disconnecting from mysql

This commit is contained in:
Sergey Tsalkov
2013-03-19 12:02:47 -07:00
parent ce11e65070
commit e94f75fe2a
2 changed files with 15 additions and 1 deletions

View File

@@ -59,6 +59,7 @@ class DB {
} }
public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); } public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); }
public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); }
public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); } public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); }
public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); }
public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); }
@@ -153,7 +154,7 @@ class MeekroDB {
public function get() { public function get() {
$mysql = $this->internal_mysql; $mysql = $this->internal_mysql;
if ($mysql == null) { if (!($mysql instanceof MySQLi)) {
if (! $this->port) $this->port = ini_get('mysqli.default_port'); if (! $this->port) $this->port = ini_get('mysqli.default_port');
$this->current_db = $this->dbName; $this->current_db = $this->dbName;
$mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port);
@@ -170,6 +171,15 @@ class MeekroDB {
return $mysql; return $mysql;
} }
public function disconnect() {
$mysqli = $this->internal_mysql;
if ($mysqli instanceof MySQLi) {
if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id);
$mysqli->close();
}
$this->internal_mysql = null;
}
public function nonSQLError($message) { public function nonSQLError($message) {
if ($this->throw_exception_on_nonsql_error) { if ($this->throw_exception_on_nonsql_error) {
$e = new MeekroDBException($message); $e = new MeekroDBException($message);

View File

@@ -22,6 +22,10 @@ class BasicTest extends SimpleTest {
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting' `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting'
) ENGINE = InnoDB"); ) ENGINE = InnoDB");
$mysqli = DB::get();
DB::disconnect();
@$this->assert($mysqli->server_info === null);
} }
function test_1_5_empty_table() { function test_1_5_empty_table() {