/**
  * Execute a statement
  *
  * @todo move to driver if not will only support MySQL
  *
  * @throws Exception\ForeignKeyException when insertion failed because of an invalid foreign key
  * @throws Exception\DuplicateEntryException when insertion failed because of an invalid foreign key
  * @throws Exception\NotNullException when insertion failed because a column cannot be null
  * @throws Exception\RuntimeException when insertion failed for another reason
  *
  * @param string|PreparableSqlInterface $sqlObject
  * @return \Zend\Db\Adapter\Driver\ResultInterface
  */
 protected function executeStatement($sqlObject)
 {
     if ($sqlObject instanceof PreparableSqlInterface) {
         $statement = $this->sql->prepareStatementForSqlObject($sqlObject);
     } elseif (is_string($sqlObject)) {
         $statement = $this->tableManager->getDbAdapter()->createStatement($sqlObject);
     } else {
         //@codeCoverageIgnoreStart
         throw new Exception\InvalidArgumentException(__METHOD__ . ': expects sqlObject to be string or PreparableInterface');
         //@codeCoverageIgnoreEnd
     }
     try {
         $result = $statement->execute();
     } catch (\Exception $e) {
         // In ZF2, PDO_Mysql and MySQLi return different exception,
         // attempt to normalize by catching one exception instead
         // of RuntimeException and InvalidQueryException
         $messages = array();
         $ex = $e;
         do {
             $messages[] = $ex->getMessage();
         } while ($ex = $ex->getPrevious());
         $message = join(', ', array_unique($messages));
         $lmsg = __METHOD__ . ':' . strtolower($message) . '(code:' . $e->getCode() . ')';
         if (strpos($lmsg, 'cannot be null') !== false) {
             // Integrity constraint violation: 1048 Column 'non_null_column' cannot be null
             $rex = new Exception\NotNullException(__METHOD__ . ': ' . $message, $e->getCode(), $e);
             throw $rex;
         } elseif (strpos($lmsg, 'duplicate entry') !== false) {
             $rex = new Exception\DuplicateEntryException(__METHOD__ . ': ' . $message, $e->getCode(), $e);
             throw $rex;
         } elseif (strpos($lmsg, 'constraint violation') !== false || strpos($lmsg, 'foreign key') !== false) {
             $rex = new Exception\ForeignKeyException(__METHOD__ . ': ' . $message, $e->getCode(), $e);
             throw $rex;
         } else {
             if ($sqlObject instanceof SqlInterface) {
                 $sql_string = $sqlObject->getSqlString($this->sql->getAdapter()->getPlatform());
             } else {
                 $sql_string = $sqlObject;
             }
             $iqex = new Exception\RuntimeException(__METHOD__ . ': ' . $message . "[{$sql_string}]", $e->getCode(), $e);
             throw $iqex;
         }
     }
     return $result;
 }
Exemple #2
0
 protected function adapter()
 {
     return $this->sql->getAdapter();
 }