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

Statement   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 18 6
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