Pheasant\Database\FilterChain::execute PHP Method

execute() public method

Executes the query through the internal filters and executor
public execute ( $sql, $executor ) : result
return result set of some sort
    public function execute($sql, $executor)
    {
        foreach ($this->_onquery as $callback) {
            $sql = call_user_func($callback, $sql);
        }
        try {
            $ts = count($this->_onresult) ? microtime(true) : NULL;
            $result = call_user_func($executor, $sql);
            foreach ($this->_onresult as $callback) {
                $result = call_user_func($callback, $sql, $result, $ts);
            }
            return $result;
        } catch (\Exception $e) {
            foreach ($this->_onerror as $callback) {
                call_user_func($callback, $e);
            }
            throw $e;
        }
    }

Usage Example

Example #1
0
 public function testCatchingErrors()
 {
     $connection = \Mockery::mock('\\Pheasant\\Database\\Mysqli\\Connection');
     $filter = new FilterChain();
     $exceptions = array();
     $filter->onError(function ($e) use(&$exceptions) {
         $exceptions[] = $e;
     });
     $connection->shouldReceive('execute')->andThrow(new \Exception('Eeeeek!'));
     try {
         $filter->execute('SELECT 1', function ($sql) use($connection) {
             $connection->execute($sql);
         });
         $this->fail('Exception expected');
     } catch (\Exception $e) {
         $this->assertEquals($e->getMessage(), 'Eeeeek!');
     }
     $this->assertEquals(count($exceptions), 1);
     $this->assertEquals($exceptions[0]->getMessage(), 'Eeeeek!');
 }