Completed
Push — master ( 3de6ee...8c311e )
by Ivan
07:17
created

Statement::execute()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
cc 6
nc 24
nop 2
1
<?php
2
3
namespace vakata\database\driver\postgre;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\StatementInterface;
8
use \vakata\database\ResultInterface;
9
10
class Statement implements StatementInterface
11
{
12
    protected $statement;
13
    protected $driver;
14
15
    public function __construct(string $statement, $driver)
16
    {
17
        $this->statement = $statement;
18
        $this->driver = $driver;
19
    }
20
    public function execute(array $data = [], bool $buff = true) : ResultInterface
21
    {
22
        if (!is_array($data)) {
23
            $data = array();
24
        }
25
        $temp = (is_array($data) && count($data)) ?
26
            \pg_query_params($this->driver, $this->statement, $data) :
27
            \pg_query_params($this->driver, $this->statement, array());
28
        if (!$temp) {
29
            throw new DBException('Could not execute query : '.\pg_last_error($this->driver).' <'.$this->statement.'>');
30
        }
31
        $aff = 0;
32
        if (preg_match('@^\s*(INSERT|UPDATE|DELETE)\s+@i', $this->statement)) {
33
            $aff = @\pg_affected_rows($temp);
34
        }
35
36
        return new Result($temp, $this->driver, $aff);
37
    }
38
}
39