@@ -48,7 +48,7 @@ class MysqliDb
4848
4949 /**
5050 * The SQL query options required after SELECT, INSERT, UPDATE or DELETE
51- * @var string
51+ * @var array
5252 */
5353 protected $ _queryOptions = array ();
5454
@@ -216,6 +216,9 @@ class MysqliDb
216216 * @var string the name of a default (main) mysqli connection
217217 */
218218 public $ defConnectionName = 'default ' ;
219+
220+ public $ autoReconnect = true ;
221+ protected $ autoReconnectCount = 0 ;
219222
220223 /**
221224 * @param string $host
@@ -414,6 +417,7 @@ protected function reset()
414417 $ this ->_updateColumns = null ;
415418 $ this ->_mapKey = null ;
416419 $ this ->defConnectionName = 'default ' ;
420+ $ this ->autoReconnectCount = 0 ;
417421 return $ this ;
418422 }
419423
@@ -473,19 +477,23 @@ public function setPrefix($prefix = '')
473477 * @param [[Type]] $query [[Description]]
474478 */
475479 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+
489497 /**
490498 * Execute raw SQL query.
491499 *
@@ -811,7 +819,7 @@ public function replace($tableName, $insertData)
811819 *
812820 * @param string $tableName The name of the database table to work with.
813821 *
814- * @return array Contains the returned rows from the select query.
822+ * @return bool
815823 */
816824 public function has ($ tableName )
817825 {
@@ -1887,13 +1895,21 @@ protected function _buildLimit($numRows)
18871895 */
18881896 protected function _prepareQuery ()
18891897 {
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 ();
18951907 }
1908+
1909+ $ this ->reset ();
1910+ throw new Exception (sprintf ('%s query: %s ' , $ this ->mysqli ()->error , $ this ->_query ), $ this ->mysqli ()->errno );
18961911
1912+ release:
18971913 if ($ this ->traceEnabled ) {
18981914 $ this ->traceStartQ = microtime (true );
18991915 }
@@ -2019,6 +2035,7 @@ public function getSubQuery()
20192035 * @param string $func Initial date
20202036 *
20212037 * @return string
2038+ * @throws Exception
20222039 */
20232040 public function interval ($ diff , $ func = "NOW() " )
20242041 {
@@ -2087,6 +2104,7 @@ public function inc($num = 1)
20872104 * @param int $num increment by int or float. 1 by default
20882105 *
20892106 * @return array
2107+ * @throws Exception
20902108 */
20912109 public function dec ($ num = 1 )
20922110 {
@@ -2296,7 +2314,7 @@ public function paginate ($table, $page, $fields = null) {
22962314 * @param string $whereProp The name of the database field.
22972315 * @param mixed $whereValue The value of the database field.
22982316 *
2299- * @return dbWrapper
2317+ * @return $this
23002318 */
23012319 public function joinWhere ($ whereJoin , $ whereProp , $ whereValue = 'DBNULL ' , $ operator = '= ' , $ cond = 'AND ' )
23022320 {
0 commit comments