@@ -48,7 +48,7 @@ class MysqliDb
48
48
49
49
/**
50
50
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE
51
- * @var string
51
+ * @var array
52
52
*/
53
53
protected $ _queryOptions = array ();
54
54
@@ -216,6 +216,9 @@ class MysqliDb
216
216
* @var string the name of a default (main) mysqli connection
217
217
*/
218
218
public $ defConnectionName = 'default ' ;
219
+
220
+ public $ autoReconnect = true ;
221
+ protected $ autoReconnectCount = 0 ;
219
222
220
223
/**
221
224
* @param string $host
@@ -414,6 +417,7 @@ protected function reset()
414
417
$ this ->_updateColumns = null ;
415
418
$ this ->_mapKey = null ;
416
419
$ this ->defConnectionName = 'default ' ;
420
+ $ this ->autoReconnectCount = 0 ;
417
421
return $ this ;
418
422
}
419
423
@@ -473,19 +477,23 @@ public function setPrefix($prefix = '')
473
477
* @param [[Type]] $query [[Description]]
474
478
*/
475
479
private function queryUnprepared ($ query )
476
- {
477
- // Execute query
478
- $ stmt = $ this ->mysqli ()->query ($ query );
479
-
480
- // Failed?
481
- if (!$ stmt ){
482
- throw new Exception ("Unprepared Query Failed, ERRNO: " .$ this ->mysqli ()->errno ." ( " .$ this ->mysqli ()->error .") " , $ this ->mysqli ()->errno );
483
- };
484
-
485
- // return stmt for future use
486
- return $ stmt ;
487
- }
488
-
480
+ {
481
+ // Execute query
482
+ $ stmt = $ this ->mysqli ()->query ($ query );
483
+
484
+ // Failed?
485
+ if ($ stmt !== false )
486
+ return $ stmt ;
487
+
488
+ if ($ this ->mysqli ()->errno === 2006 && $ this ->autoReconnect === true && $ this ->autoReconnectCount === 0 ) {
489
+ $ this ->connect ($ this ->defConnectionName );
490
+ $ this ->autoReconnectCount ++;
491
+ return $ this ->queryUnprepared ($ query );
492
+ }
493
+
494
+ throw new Exception (sprintf ('Unprepared Query Failed, ERRNO: %u (%s) ' , $ this ->mysqli ()->errno , $ this ->mysqli ()->error ), $ this ->mysqli ()->errno );
495
+ }
496
+
489
497
/**
490
498
* Execute raw SQL query.
491
499
*
@@ -811,7 +819,7 @@ public function replace($tableName, $insertData)
811
819
*
812
820
* @param string $tableName The name of the database table to work with.
813
821
*
814
- * @return array Contains the returned rows from the select query.
822
+ * @return bool
815
823
*/
816
824
public function has ($ tableName )
817
825
{
@@ -1887,13 +1895,21 @@ protected function _buildLimit($numRows)
1887
1895
*/
1888
1896
protected function _prepareQuery ()
1889
1897
{
1890
- if (!$ stmt = $ this ->mysqli ()->prepare ($ this ->_query )) {
1891
- $ msg = $ this ->mysqli ()->error . " query: " . $ this ->_query ;
1892
- $ num = $ this ->mysqli ()->errno ;
1893
- $ this ->reset ();
1894
- throw new Exception ($ msg , $ num );
1898
+ $ stmt = $ this ->mysqli ()->prepare ($ this ->_query );
1899
+
1900
+ if ($ stmt !== false )
1901
+ goto release;
1902
+
1903
+ if ($ this ->mysqli ()->errno === 2006 && $ this ->autoReconnect === true && $ this ->autoReconnectCount === 0 ) {
1904
+ $ this ->connect ($ this ->defConnectionName );
1905
+ $ this ->autoReconnectCount ++;
1906
+ return $ this ->_prepareQuery ();
1895
1907
}
1908
+
1909
+ $ this ->reset ();
1910
+ throw new Exception (sprintf ('%s query: %s ' , $ this ->mysqli ()->error , $ this ->_query ), $ this ->mysqli ()->errno );
1896
1911
1912
+ release:
1897
1913
if ($ this ->traceEnabled ) {
1898
1914
$ this ->traceStartQ = microtime (true );
1899
1915
}
@@ -2019,6 +2035,7 @@ public function getSubQuery()
2019
2035
* @param string $func Initial date
2020
2036
*
2021
2037
* @return string
2038
+ * @throws Exception
2022
2039
*/
2023
2040
public function interval ($ diff , $ func = "NOW() " )
2024
2041
{
@@ -2087,6 +2104,7 @@ public function inc($num = 1)
2087
2104
* @param int $num increment by int or float. 1 by default
2088
2105
*
2089
2106
* @return array
2107
+ * @throws Exception
2090
2108
*/
2091
2109
public function dec ($ num = 1 )
2092
2110
{
@@ -2296,7 +2314,7 @@ public function paginate ($table, $page, $fields = null) {
2296
2314
* @param string $whereProp The name of the database field.
2297
2315
* @param mixed $whereValue The value of the database field.
2298
2316
*
2299
- * @return dbWrapper
2317
+ * @return $this
2300
2318
*/
2301
2319
public function joinWhere ($ whereJoin , $ whereProp , $ whereValue = 'DBNULL ' , $ operator = '= ' , $ cond = 'AND ' )
2302
2320
{
0 commit comments