From e94f75fe2a095b49b3a0436fb40aa57a70bdc678 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Tue, 19 Mar 2013 12:02:47 -0700 Subject: [PATCH] DB::disconnect() allows easy disconnecting from mysql --- db.class.php | 12 +++++++++++- simpletest/BasicTest.php | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/db.class.php b/db.class.php index cf72d56..99ac9ab 100644 --- a/db.class.php +++ b/db.class.php @@ -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 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 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); } @@ -153,7 +154,7 @@ class MeekroDB { public function get() { $mysql = $this->internal_mysql; - if ($mysql == null) { + if (!($mysql instanceof MySQLi)) { if (! $this->port) $this->port = ini_get('mysqli.default_port'); $this->current_db = $this->dbName; $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); @@ -170,6 +171,15 @@ class MeekroDB { 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) { if ($this->throw_exception_on_nonsql_error) { $e = new MeekroDBException($message); diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index f617331..9a5552a 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -22,6 +22,10 @@ class BasicTest extends SimpleTest { `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting' ) ENGINE = InnoDB"); + + $mysqli = DB::get(); + DB::disconnect(); + @$this->assert($mysqli->server_info === null); } function test_1_5_empty_table() {