mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
Add reconnectable statement
This commit is contained in:
52
lib/classes/connection/pdoStatementReconnectable.php
Normal file
52
lib/classes/connection/pdoStatementReconnectable.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2014 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
class connection_pdoStatementReconnectable implements \connection_statement
|
||||
{
|
||||
protected $queryString;
|
||||
protected $statement;
|
||||
protected $conn;
|
||||
|
||||
public function __construct(\connection_statement $statement, \connection_pdo $conn)
|
||||
{
|
||||
$this->statement = $statement;
|
||||
$this->conn = $conn;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getQueryString()
|
||||
{
|
||||
return $this->statement->getQueryString();
|
||||
}
|
||||
|
||||
public function execute($params = array())
|
||||
{
|
||||
try {
|
||||
return $this->statement->execute($params);
|
||||
} catch (\Exception $e) {
|
||||
$unreachable = ($e->getCode() === 2006) || (false !== strpos($e->getMessage(), 'MySQL server has gone away')) || (false !== strpos($e->getMessage(), 'errno=32 Broken pipe'));
|
||||
if (!$unreachable) {
|
||||
throw $e;
|
||||
}
|
||||
$this->conn->connect();
|
||||
}
|
||||
// retry query with update statement
|
||||
$this->statement = $this->conn->prepare($this->getQueryString());
|
||||
|
||||
return $this->statement->execute($params);
|
||||
}
|
||||
|
||||
public function __call($function_name, $parameters)
|
||||
{
|
||||
return call_user_func_array(array($this->statement, $function_name), $parameters);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user