Exemplo n.º 1
0
 /**
  * Execute an SQL Statement and return result to be handled by calling function.
  *
  * @param	mixed PreparedStatement or KeyBasedPreparedStatement
  * @param	string Name of connection to be used
  * @param	resource $connection Connection resource handler
  * @return	mixed Resource if query executed successfully, otherwise false
  */
 public function executeInsert(PreparedStatement $ps, $name = 'default', &$con = NULL, $debug = self::DEBUG)
 {
     $retval = false;
     try {
         // Connect to database
         if (is_null($con)) {
             if (self::DEBUG) {
                 LoggerManager::error(__METHOD__ . ":: Retrieving New DB Connection for '" . $name . "'...");
             }
             $con = $this->getDatabaseConnection($name);
         }
         // Get the prepared query
         /* @var $sth PDOStatement */
         $sth = $ps->getPreparedStatement($con);
         if ($debug) {
             LoggerManager::error(__METHOD__ . " :: " . $sth);
         }
         // Execute the query
         $sth->execute();
         return $con->lastInsertId();
     } catch (MojaviException $e) {
         LoggerManager::fatal($e->printStackTrace(''));
         throw $e;
     } catch (PDOException $e) {
         ob_start();
         $sth->debugDumpParams();
         $stmt = ob_get_clean();
         $e = new MojaviException($e->getMessage());
         LoggerManager::fatal($sth->queryString);
         LoggerManager::fatal($stmt);
         LoggerManager::fatal($e->printStackTrace(''));
         throw $e;
     } catch (Exception $e) {
         $e = new MojaviException($e->getMessage());
         LoggerManager::fatal($e->printStackTrace(''));
         throw $e;
     }
 }
Exemplo n.º 2
0
 /**
  * Execute an SQL Statement and return result to be handled by calling function.
  *
  * @param	mixed PreparedStatement or KeyBasedPreparedStatement
  * @param	string Name of connection to be used
  * @param	resource $connection Connection resource handler
  * @return	mixed Resource if query executed successfully, otherwise false
  */
 public function executeInsert(PreparedStatement $ps, $name = 'default', $con = NULL, $debug = self::DEBUG)
 {
     $retval = false;
     try {
         // Connect to database
         if (is_null($con)) {
             if (self::DEBUG) {
                 LoggerManager::debug(__METHOD__ . ":: Retrieving New DB Connection for '" . $name . "'...");
             }
             $con = $this->getContext()->getDatabaseConnection($name);
         }
         // Get the prepared query
         /* @var $sth PDOStatement */
         $sth = $ps->getPreparedStatement($con);
         // Debug the query to the log
         if ($debug) {
             LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString());
         }
         // Execute the query
         $sth->execute();
         // Return the last insert id
         $retval = $con->lastInsertId();
     } catch (PDOException $e) {
         // If the MySQL server has gone away, try reconnecting, otherwise throw an exception
         if ($e->getMessage() == 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away') {
             try {
                 $this->getContext()->getDatabaseManager()->getDatabase($name)->shutdown();
                 // Connect to database
                 $con = $this->getContext()->getDatabaseConnection($name);
                 // Get the prepared query
                 /* @var $sth PDOStatement */
                 $sth = $ps->getPreparedStatement($con);
                 if ($debug) {
                     LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString());
                 }
                 // Execute the query
                 $sth->execute();
                 $retval = $sth;
             } catch (Exception $e) {
                 ob_start();
                 $sth->debugDumpParams();
                 $stmt = ob_get_clean();
                 $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString));
                 $e = new MojaviException($e->getMessage());
                 LoggerManager::fatal($sth->queryString);
                 LoggerManager::fatal($stmt);
                 LoggerManager::fatal($e->printStackTrace(''));
                 throw $e;
             }
         } else {
             if (strpos($e->getMessage(), 'Lock wait timeout exceeded; try restarting transaction') !== false) {
                 // If there was a lock on the transaction, then try it again before failing
                 try {
                     $this->getContext()->getDatabaseManager()->getDatabase($name)->shutdown();
                     // Connect to database
                     $con = $this->getContext()->getDatabaseConnection($name);
                     // Get the prepared query
                     /* @var $sth PDOStatement */
                     $sth = $ps->getPreparedStatement($con);
                     if ($debug) {
                         LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString());
                     }
                     // Execute the query
                     $sth->execute();
                     $retval = $sth;
                 } catch (Exception $e) {
                     ob_start();
                     $sth->debugDumpParams();
                     $stmt = ob_get_clean();
                     $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString));
                     $e = new MojaviException($e->getMessage());
                     LoggerManager::fatal($sth->queryString);
                     LoggerManager::fatal($stmt);
                     LoggerManager::fatal($e->printStackTrace(''));
                     throw $e;
                 }
             } else {
                 ob_start();
                 $sth->debugDumpParams();
                 $stmt = ob_get_clean();
                 $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString));
                 $e = new MojaviException($e->getMessage());
                 LoggerManager::fatal($sth->queryString);
                 LoggerManager::fatal($stmt);
                 LoggerManager::fatal($e->printStackTrace(''));
                 throw $e;
             }
         }
     } catch (MojaviException $e) {
         // Output Mojavi Exceptions to the log and throw the Exception
         $this->getErrors()->addError('error', new Error($e->getMessage()));
         LoggerManager::fatal($e->printStackTrace(''));
         throw $e;
     } catch (Exception $e) {
         // Output Normal Exceptions to the log and throw the Exception
         $this->getErrors()->addError('error', new Error($e->getMessage()));
         $e = new MojaviException($e->getMessage());
         LoggerManager::fatal($e->printStackTrace(''));
         throw $e;
     }
     return $retval;
 }