@@ -29,10 +29,10 @@ class MysqliDb
29
29
public static $ prefix = '' ;
30
30
31
31
/**
32
- * MySQLi instance
33
- * @var mysqli
32
+ * MySQLi instances
33
+ * @var mysqli[]
34
34
*/
35
- protected $ _mysqli ;
35
+ protected $ _mysqli = [] ;
36
36
37
37
/**
38
38
* The SQL query to be prepared and executed
@@ -131,18 +131,6 @@ class MysqliDb
131
131
*/
132
132
protected $ _stmtErrno ;
133
133
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
-
146
134
/**
147
135
* Is Subquery object
148
136
* @var bool
@@ -220,14 +208,23 @@ class MysqliDb
220
208
*/
221
209
public $ totalPages = 0 ;
222
210
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
+
223
220
/**
224
221
* @param string $host
225
222
* @param string $username
226
223
* @param string $password
227
224
* @param string $db
228
225
* @param int $port
229
226
* @param string $charset
230
- * @params string $socket
227
+ * @param string $socket
231
228
*/
232
229
public function __construct ($ host = null , $ username = null , $ password = null , $ db = null , $ port = null , $ charset = 'utf8 ' , $ socket = null )
233
230
{
@@ -239,21 +236,16 @@ public function __construct($host = null, $username = null, $password = null, $d
239
236
$ $ key = $ val ;
240
237
}
241
238
}
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
- }
250
239
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
+ ]);
257
249
258
250
if ($ isSubQuery ) {
259
251
$ this ->isSubQuery = true ;
@@ -269,43 +261,101 @@ public function __construct($host = null, $username = null, $password = null, $d
269
261
270
262
/**
271
263
* A method to connect to the database
272
- *
264
+ *
265
+ * @param null|string $connectionName
273
266
* @throws Exception
274
267
* @return void
275
268
*/
276
- public function connect ()
269
+ public function connect ($ connectionName )
277
270
{
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
+
278
278
if ($ this ->isSubQuery ) {
279
279
return ;
280
280
}
281
281
282
- if (empty ($ this -> host ) && empty ($ this -> socket )) {
282
+ if (empty ($ pro [ ' host ' ] ) && empty ($ pro [ ' socket ' ] )) {
283
283
throw new Exception ('MySQL host or socket is not set ' );
284
284
}
285
285
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
+ }
287
292
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 );
290
295
}
296
+ $ this ->_mysqli [$ connectionName ] = $ mysqli ;
297
+ }
291
298
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 );
294
303
}
295
304
}
296
305
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
+
297
321
/**
298
322
* A method to disconnect from the database
299
323
*
324
+ * @params string $connection connection name to disconnect
300
325
* @throws Exception
301
326
* @return void
302
327
*/
303
- public function disconnect ()
328
+ public function disconnect ($ connection = ' default ' )
304
329
{
305
- if (!$ this ->_mysqli )
330
+ if (!isset ( $ this ->_mysqli [ $ connection ]) )
306
331
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 ;
309
359
}
310
360
311
361
/**
@@ -315,10 +365,10 @@ public function disconnect()
315
365
*/
316
366
public function mysqli ()
317
367
{
318
- if (!$ this ->_mysqli ) {
319
- $ this ->connect ();
368
+ if (!isset ( $ this ->_mysqli [ $ this -> defConnectionName ]) ) {
369
+ $ this ->connect ($ this -> defConnectionName );
320
370
}
321
- return $ this ->_mysqli ;
371
+ return $ this ->_mysqli [ $ this -> defConnectionName ] ;
322
372
}
323
373
324
374
/**
@@ -363,6 +413,8 @@ protected function reset()
363
413
$ this ->_lastInsertId = null ;
364
414
$ this ->_updateColumns = null ;
365
415
$ this ->_mapKey = null ;
416
+ $ this ->defConnectionName = 'default ' ;
417
+ return $ this ;
366
418
}
367
419
368
420
/**
@@ -1831,6 +1883,7 @@ protected function _buildLimit($numRows)
1831
1883
* and throws an error if there was a problem.
1832
1884
*
1833
1885
* @return mysqli_stmt
1886
+ * @throws Exception
1834
1887
*/
1835
1888
protected function _prepareQuery ()
1836
1889
{
@@ -1919,7 +1972,7 @@ public function getLastQuery()
1919
1972
*/
1920
1973
public function getLastError ()
1921
1974
{
1922
- if (!$ this ->_mysqli ) {
1975
+ if (!isset ( $ this ->_mysqli [ $ this -> defConnectionName ]) ) {
1923
1976
return "mysqli is null " ;
1924
1977
}
1925
1978
return trim ($ this ->_stmtError . " " . $ this ->mysqli ()->error );
@@ -1948,7 +2001,7 @@ public function getSubQuery()
1948
2001
array_shift ($ this ->_bindParams );
1949
2002
$ val = Array ('query ' => $ this ->_query ,
1950
2003
'params ' => $ this ->_bindParams ,
1951
- 'alias ' => $ this ->host
2004
+ 'alias ' => isset ( $ this ->connectionsSettings [ $ this -> defConnectionName ]) ? $ this -> connectionsSettings [ $ this -> defConnectionName ][ ' host ' ] : null
1952
2005
);
1953
2006
$ this ->reset ();
1954
2007
return $ val ;
@@ -2088,7 +2141,7 @@ public static function subQuery($subQueryAlias = "")
2088
2141
public function copy ()
2089
2142
{
2090
2143
$ copy = unserialize (serialize ($ this ));
2091
- $ copy ->_mysqli = null ;
2144
+ $ copy ->_mysqli = [] ;
2092
2145
return $ copy ;
2093
2146
}
2094
2147
@@ -2196,7 +2249,8 @@ public function tableExists($tables)
2196
2249
2197
2250
foreach ($ tables as $ i => $ value )
2198
2251
$ 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 );
2200
2254
$ this ->where ('table_name ' , $ tables , 'in ' );
2201
2255
$ this ->get ('information_schema.tables ' , $ count );
2202
2256
return $ this ->count == $ count ;
0 commit comments