Skip to content

Commit 4dff067

Browse files
ilyagoryavbdr
authored andcommitted
Multiple connections (ThingEngineer#644)
* muliple mysqli connections * addConnection at constructor * increase vars * remove unused connection credentials; tableExists and getSubQuery use selected connection
1 parent 14544ff commit 4dff067

File tree

1 file changed

+103
-49
lines changed

1 file changed

+103
-49
lines changed

MysqliDb.php

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class MysqliDb
2929
public static $prefix = '';
3030

3131
/**
32-
* MySQLi instance
33-
* @var mysqli
32+
* MySQLi instances
33+
* @var mysqli[]
3434
*/
35-
protected $_mysqli;
35+
protected $_mysqli = [];
3636

3737
/**
3838
* The SQL query to be prepared and executed
@@ -131,18 +131,6 @@ class MysqliDb
131131
*/
132132
protected $_stmtErrno;
133133

134-
/**
135-
* Database credentials
136-
* @var string
137-
*/
138-
protected $host;
139-
protected $socket;
140-
protected $_username;
141-
protected $_password;
142-
protected $db;
143-
protected $port;
144-
protected $charset;
145-
146134
/**
147135
* Is Subquery object
148136
* @var bool
@@ -220,14 +208,23 @@ class MysqliDb
220208
*/
221209
public $totalPages = 0;
222210

211+
/**
212+
* @var array connections settings [profile_name=>[same_as_contruct_args]]
213+
*/
214+
protected $connectionsSettings = [];
215+
/**
216+
* @var string the name of a default (main) mysqli connection
217+
*/
218+
public $defConnectionName = 'default';
219+
223220
/**
224221
* @param string $host
225222
* @param string $username
226223
* @param string $password
227224
* @param string $db
228225
* @param int $port
229226
* @param string $charset
230-
* @params string $socket
227+
* @param string $socket
231228
*/
232229
public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8', $socket = null)
233230
{
@@ -239,21 +236,16 @@ public function __construct($host = null, $username = null, $password = null, $d
239236
$$key = $val;
240237
}
241238
}
242-
// if host were set as mysqli socket
243-
if (is_object($host)) {
244-
$this->_mysqli = $host;
245-
} else
246-
// in case of using socket & host not exists in config array
247-
if(is_string($host)) {
248-
$this->host = $host;
249-
}
250239

251-
$this->_username = $username;
252-
$this->_password = $password;
253-
$this->db = $db;
254-
$this->port = $port;
255-
$this->charset = $charset;
256-
$this->socket = $socket;
240+
$this->addConnection('default', [
241+
'host' => $host,
242+
'username' => $username,
243+
'password' => $password,
244+
'db' => $db,
245+
'port' => $port,
246+
'socket' => $socket,
247+
'charset' => $charset
248+
]);
257249

258250
if ($isSubQuery) {
259251
$this->isSubQuery = true;
@@ -269,43 +261,101 @@ public function __construct($host = null, $username = null, $password = null, $d
269261

270262
/**
271263
* A method to connect to the database
272-
*
264+
*
265+
* @param null|string $connectionName
273266
* @throws Exception
274267
* @return void
275268
*/
276-
public function connect()
269+
public function connect($connectionName)
277270
{
271+
if(!isset($this->connectionsSettings[$connectionName]))
272+
throw new Exception('Connection profile not set');
273+
274+
$pro = $this->connectionsSettings[$connectionName];
275+
$params = array_values($pro);
276+
$charset = array_pop($params);
277+
278278
if ($this->isSubQuery) {
279279
return;
280280
}
281281

282-
if (empty($this->host) && empty($this->socket)) {
282+
if (empty($pro['host']) && empty($pro['socket'])) {
283283
throw new Exception('MySQL host or socket is not set');
284284
}
285285

286-
$this->_mysqli = new mysqli($this->host, $this->_username, $this->_password, $this->db, $this->port, $this->socket);
286+
$mysqlic = new ReflectionClass('mysqli');
287+
$mysqli = $mysqlic->newInstanceArgs($params);
288+
289+
if ($mysqli->connect_error) {
290+
throw new Exception('Connect Error ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error, $mysqli->connect_errno);
291+
}
287292

288-
if ($this->_mysqli->connect_error) {
289-
throw new Exception('Connect Error ' . $this->_mysqli->connect_errno . ': ' . $this->_mysqli->connect_error, $this->_mysqli->connect_errno);
293+
if (!empty($charset)) {
294+
$mysqli->set_charset($charset);
290295
}
296+
$this->_mysqli[$connectionName] = $mysqli;
297+
}
291298

292-
if ($this->charset) {
293-
$this->_mysqli->set_charset($this->charset);
299+
public function disconnectAll()
300+
{
301+
foreach (array_keys($this->_mysqli) as $k) {
302+
$this->disconnect($k);
294303
}
295304
}
296305

306+
/**
307+
* Set the connection name to use in the next query
308+
* @param string $name
309+
* @return $this
310+
* @throws Exception
311+
*/
312+
public function connection($name)
313+
{
314+
if (!isset($this->connectionsSettings[$name]))
315+
throw new Exception('Connection ' . $name . ' was not added.');
316+
317+
$this->defConnectionName = $name;
318+
return $this;
319+
}
320+
297321
/**
298322
* A method to disconnect from the database
299323
*
324+
* @params string $connection connection name to disconnect
300325
* @throws Exception
301326
* @return void
302327
*/
303-
public function disconnect()
328+
public function disconnect($connection = 'default')
304329
{
305-
if (!$this->_mysqli)
330+
if (!isset($this->_mysqli[$connection]))
306331
return;
307-
$this->_mysqli->close();
308-
$this->_mysqli = null;
332+
333+
$this->_mysqli[$connection]->close();
334+
unset($this->_mysqli[$connection]);
335+
}
336+
337+
/**
338+
* Create & store at _mysqli new mysqli instance
339+
* @param string $name
340+
* @param array $params
341+
* @return $this
342+
*/
343+
public function addConnection($name, array $params)
344+
{
345+
$this->connectionsSettings[$name] = [];
346+
foreach (['host', 'username', 'password', 'db', 'port', 'socket', 'charset'] as $k) {
347+
$prm = isset($params[$k]) ? $params[$k] : null;
348+
349+
if ($k == 'host') {
350+
if (is_object($prm))
351+
$this->_mysqli[$name] = $prm;
352+
353+
if (!is_string($prm))
354+
$prm = null;
355+
}
356+
$this->connectionsSettings[$name][$k] = $prm;
357+
}
358+
return $this;
309359
}
310360

311361
/**
@@ -315,10 +365,10 @@ public function disconnect()
315365
*/
316366
public function mysqli()
317367
{
318-
if (!$this->_mysqli) {
319-
$this->connect();
368+
if (!isset($this->_mysqli[$this->defConnectionName])) {
369+
$this->connect($this->defConnectionName);
320370
}
321-
return $this->_mysqli;
371+
return $this->_mysqli[$this->defConnectionName];
322372
}
323373

324374
/**
@@ -363,6 +413,8 @@ protected function reset()
363413
$this->_lastInsertId = null;
364414
$this->_updateColumns = null;
365415
$this->_mapKey = null;
416+
$this->defConnectionName = 'default';
417+
return $this;
366418
}
367419

368420
/**
@@ -1831,6 +1883,7 @@ protected function _buildLimit($numRows)
18311883
* and throws an error if there was a problem.
18321884
*
18331885
* @return mysqli_stmt
1886+
* @throws Exception
18341887
*/
18351888
protected function _prepareQuery()
18361889
{
@@ -1919,7 +1972,7 @@ public function getLastQuery()
19191972
*/
19201973
public function getLastError()
19211974
{
1922-
if (!$this->_mysqli) {
1975+
if (!isset($this->_mysqli[$this->defConnectionName])) {
19231976
return "mysqli is null";
19241977
}
19251978
return trim($this->_stmtError . " " . $this->mysqli()->error);
@@ -1948,7 +2001,7 @@ public function getSubQuery()
19482001
array_shift($this->_bindParams);
19492002
$val = Array('query' => $this->_query,
19502003
'params' => $this->_bindParams,
1951-
'alias' => $this->host
2004+
'alias' => isset($this->connectionsSettings[$this->defConnectionName]) ? $this->connectionsSettings[$this->defConnectionName]['host'] : null
19522005
);
19532006
$this->reset();
19542007
return $val;
@@ -2088,7 +2141,7 @@ public static function subQuery($subQueryAlias = "")
20882141
public function copy()
20892142
{
20902143
$copy = unserialize(serialize($this));
2091-
$copy->_mysqli = null;
2144+
$copy->_mysqli = [];
20922145
return $copy;
20932146
}
20942147

@@ -2196,7 +2249,8 @@ public function tableExists($tables)
21962249

21972250
foreach ($tables as $i => $value)
21982251
$tables[$i] = self::$prefix . $value;
2199-
$this->where('table_schema', $this->db);
2252+
$db = isset($this->connectionsSettings[$this->defConnectionName]) ? $this->connectionsSettings[$this->defConnectionName]['db'] : null;
2253+
$this->where('table_schema', $db);
22002254
$this->where('table_name', $tables, 'in');
22012255
$this->get('information_schema.tables', $count);
22022256
return $this->count == $count;

0 commit comments

Comments
 (0)