DboSource::_execute PHP Method

_execute() protected method

Executes given SQL statement.
protected _execute ( string $sql, array $params = [], array $prepareOptions = [] ) : mixed
$sql string SQL statement
$params array list of params to be bound to query
$prepareOptions array Options to be used in the prepare statement
return mixed PDOStatement if query executes with no problem, true as the result of a successful, false on error query returning no rows, such as a CREATE statement, false otherwise
    protected function _execute($sql, $params = array(), $prepareOptions = array())
    {
        $sql = trim($sql);
        if (preg_match('/^(?:CREATE|ALTER|DROP)\\s+(?:TABLE|INDEX)/i', $sql)) {
            $statements = array_filter(explode(';', $sql));
            if (count($statements) > 1) {
                $result = array_map(array($this, '_execute'), $statements);
                return array_search(false, $result) === false;
            }
        }
        try {
            $query = $this->_connection->prepare($sql, $prepareOptions);
            $query->setFetchMode(PDO::FETCH_LAZY);
            if (!$query->execute($params)) {
                $this->_results = $query;
                $query->closeCursor();
                return false;
            }
            if (!$query->columnCount()) {
                $query->closeCursor();
                if (!$query->rowCount()) {
                    return true;
                }
            }
            return $query;
        } catch (PDOException $e) {
            if (isset($query->queryString)) {
                $e->queryString = $query->queryString;
            } else {
                $e->queryString = $sql;
            }
            throw $e;
        }
    }

Usage Example

Example #1
0
 /**
  * Executes given SQL statement.
  *
  * @param string $sql SQL statement
  * @param array $params list of params to be bound to query (supported only in select)
  * @param array $prepareOptions Options to be used in the prepare statement
  * @return mixed PDOStatement if query executes with no problem, true as the result of a succesfull, false on error
  * query returning no rows, suchs as a CREATE statement, false otherwise
  */
 protected function _execute($sql, $params = array(), $prepareOptions = array())
 {
     $this->_lastAffected = false;
     if (strncasecmp($sql, 'SELECT', 6) == 0) {
         $prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
         return parent::_execute($sql, $params, $prepareOptions);
     }
     try {
         $this->_lastAffected = $this->_connection->exec($sql);
         if ($this->_lastAffected === false) {
             $this->_results = null;
             $error = $this->_connection->errorInfo();
             $this->error = $error[2];
             return false;
         }
         return true;
     } catch (PDOException $e) {
         if (isset($query->queryString)) {
             $e->queryString = $query->queryString;
         } else {
             $e->queryString = $sql;
         }
         throw $e;
     }
 }
All Usage Examples Of DboSource::_execute
DboSource