Commit 4dff067a authored by Ilya Goryachev's avatar Ilya Goryachev Committed by Alexander Butenko

Multiple connections (#644)

* muliple mysqli connections

* addConnection at constructor

* increase vars

* remove unused connection credentials; tableExists and getSubQuery use selected connection
parent 14544ff7
...@@ -29,10 +29,10 @@ class MysqliDb ...@@ -29,10 +29,10 @@ class MysqliDb
public static $prefix = ''; public static $prefix = '';
/** /**
* MySQLi instance * MySQLi instances
* @var mysqli * @var mysqli[]
*/ */
protected $_mysqli; protected $_mysqli = [];
/** /**
* The SQL query to be prepared and executed * The SQL query to be prepared and executed
...@@ -131,18 +131,6 @@ class MysqliDb ...@@ -131,18 +131,6 @@ class MysqliDb
*/ */
protected $_stmtErrno; protected $_stmtErrno;
/**
* Database credentials
* @var string
*/
protected $host;
protected $socket;
protected $_username;
protected $_password;
protected $db;
protected $port;
protected $charset;
/** /**
* Is Subquery object * Is Subquery object
* @var bool * @var bool
...@@ -220,6 +208,15 @@ class MysqliDb ...@@ -220,6 +208,15 @@ class MysqliDb
*/ */
public $totalPages = 0; public $totalPages = 0;
/**
* @var array connections settings [profile_name=>[same_as_contruct_args]]
*/
protected $connectionsSettings = [];
/**
* @var string the name of a default (main) mysqli connection
*/
public $defConnectionName = 'default';
/** /**
* @param string $host * @param string $host
* @param string $username * @param string $username
...@@ -227,7 +224,7 @@ class MysqliDb ...@@ -227,7 +224,7 @@ class MysqliDb
* @param string $db * @param string $db
* @param int $port * @param int $port
* @param string $charset * @param string $charset
* @params string $socket * @param string $socket
*/ */
public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8', $socket = null) public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8', $socket = null)
{ {
...@@ -239,21 +236,16 @@ class MysqliDb ...@@ -239,21 +236,16 @@ class MysqliDb
$$key = $val; $$key = $val;
} }
} }
// if host were set as mysqli socket
if (is_object($host)) {
$this->_mysqli = $host;
} else
// in case of using socket & host not exists in config array
if(is_string($host)) {
$this->host = $host;
}
$this->_username = $username; $this->addConnection('default', [
$this->_password = $password; 'host' => $host,
$this->db = $db; 'username' => $username,
$this->port = $port; 'password' => $password,
$this->charset = $charset; 'db' => $db,
$this->socket = $socket; 'port' => $port,
'socket' => $socket,
'charset' => $charset
]);
if ($isSubQuery) { if ($isSubQuery) {
$this->isSubQuery = true; $this->isSubQuery = true;
...@@ -269,43 +261,101 @@ class MysqliDb ...@@ -269,43 +261,101 @@ class MysqliDb
/** /**
* A method to connect to the database * A method to connect to the database
* *
* @param null|string $connectionName
* @throws Exception * @throws Exception
* @return void * @return void
*/ */
public function connect() public function connect($connectionName)
{ {
if(!isset($this->connectionsSettings[$connectionName]))
throw new Exception('Connection profile not set');
$pro = $this->connectionsSettings[$connectionName];
$params = array_values($pro);
$charset = array_pop($params);
if ($this->isSubQuery) { if ($this->isSubQuery) {
return; return;
} }
if (empty($this->host) && empty($this->socket)) { if (empty($pro['host']) && empty($pro['socket'])) {
throw new Exception('MySQL host or socket is not set'); throw new Exception('MySQL host or socket is not set');
} }
$this->_mysqli = new mysqli($this->host, $this->_username, $this->_password, $this->db, $this->port, $this->socket); $mysqlic = new ReflectionClass('mysqli');
$mysqli = $mysqlic->newInstanceArgs($params);
if ($mysqli->connect_error) {
throw new Exception('Connect Error ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error, $mysqli->connect_errno);
}
if ($this->_mysqli->connect_error) { if (!empty($charset)) {
throw new Exception('Connect Error ' . $this->_mysqli->connect_errno . ': ' . $this->_mysqli->connect_error, $this->_mysqli->connect_errno); $mysqli->set_charset($charset);
} }
$this->_mysqli[$connectionName] = $mysqli;
}
if ($this->charset) { public function disconnectAll()
$this->_mysqli->set_charset($this->charset); {
foreach (array_keys($this->_mysqli) as $k) {
$this->disconnect($k);
} }
} }
/**
* Set the connection name to use in the next query
* @param string $name
* @return $this
* @throws Exception
*/
public function connection($name)
{
if (!isset($this->connectionsSettings[$name]))
throw new Exception('Connection ' . $name . ' was not added.');
$this->defConnectionName = $name;
return $this;
}
/** /**
* A method to disconnect from the database * A method to disconnect from the database
* *
* @params string $connection connection name to disconnect
* @throws Exception * @throws Exception
* @return void * @return void
*/ */
public function disconnect() public function disconnect($connection = 'default')
{ {
if (!$this->_mysqli) if (!isset($this->_mysqli[$connection]))
return; return;
$this->_mysqli->close();
$this->_mysqli = null; $this->_mysqli[$connection]->close();
unset($this->_mysqli[$connection]);
}
/**
* Create & store at _mysqli new mysqli instance
* @param string $name
* @param array $params
* @return $this
*/
public function addConnection($name, array $params)
{
$this->connectionsSettings[$name] = [];
foreach (['host', 'username', 'password', 'db', 'port', 'socket', 'charset'] as $k) {
$prm = isset($params[$k]) ? $params[$k] : null;
if ($k == 'host') {
if (is_object($prm))
$this->_mysqli[$name] = $prm;
if (!is_string($prm))
$prm = null;
}
$this->connectionsSettings[$name][$k] = $prm;
}
return $this;
} }
/** /**
...@@ -315,10 +365,10 @@ class MysqliDb ...@@ -315,10 +365,10 @@ class MysqliDb
*/ */
public function mysqli() public function mysqli()
{ {
if (!$this->_mysqli) { if (!isset($this->_mysqli[$this->defConnectionName])) {
$this->connect(); $this->connect($this->defConnectionName);
} }
return $this->_mysqli; return $this->_mysqli[$this->defConnectionName];
} }
/** /**
...@@ -363,6 +413,8 @@ class MysqliDb ...@@ -363,6 +413,8 @@ class MysqliDb
$this->_lastInsertId = null; $this->_lastInsertId = null;
$this->_updateColumns = null; $this->_updateColumns = null;
$this->_mapKey = null; $this->_mapKey = null;
$this->defConnectionName = 'default';
return $this;
} }
/** /**
...@@ -1831,6 +1883,7 @@ class MysqliDb ...@@ -1831,6 +1883,7 @@ class MysqliDb
* and throws an error if there was a problem. * and throws an error if there was a problem.
* *
* @return mysqli_stmt * @return mysqli_stmt
* @throws Exception
*/ */
protected function _prepareQuery() protected function _prepareQuery()
{ {
...@@ -1919,7 +1972,7 @@ class MysqliDb ...@@ -1919,7 +1972,7 @@ class MysqliDb
*/ */
public function getLastError() public function getLastError()
{ {
if (!$this->_mysqli) { if (!isset($this->_mysqli[$this->defConnectionName])) {
return "mysqli is null"; return "mysqli is null";
} }
return trim($this->_stmtError . " " . $this->mysqli()->error); return trim($this->_stmtError . " " . $this->mysqli()->error);
...@@ -1948,7 +2001,7 @@ class MysqliDb ...@@ -1948,7 +2001,7 @@ class MysqliDb
array_shift($this->_bindParams); array_shift($this->_bindParams);
$val = Array('query' => $this->_query, $val = Array('query' => $this->_query,
'params' => $this->_bindParams, 'params' => $this->_bindParams,
'alias' => $this->host 'alias' => isset($this->connectionsSettings[$this->defConnectionName]) ? $this->connectionsSettings[$this->defConnectionName]['host'] : null
); );
$this->reset(); $this->reset();
return $val; return $val;
...@@ -2088,7 +2141,7 @@ class MysqliDb ...@@ -2088,7 +2141,7 @@ class MysqliDb
public function copy() public function copy()
{ {
$copy = unserialize(serialize($this)); $copy = unserialize(serialize($this));
$copy->_mysqli = null; $copy->_mysqli = [];
return $copy; return $copy;
} }
...@@ -2196,7 +2249,8 @@ class MysqliDb ...@@ -2196,7 +2249,8 @@ class MysqliDb
foreach ($tables as $i => $value) foreach ($tables as $i => $value)
$tables[$i] = self::$prefix . $value; $tables[$i] = self::$prefix . $value;
$this->where('table_schema', $this->db); $db = isset($this->connectionsSettings[$this->defConnectionName]) ? $this->connectionsSettings[$this->defConnectionName]['db'] : null;
$this->where('table_schema', $db);
$this->where('table_name', $tables, 'in'); $this->where('table_name', $tables, 'in');
$this->get('information_schema.tables', $count); $this->get('information_schema.tables', $count);
return $this->count == $count; return $this->count == $count;
......
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