Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

Statement   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C execute() 0 24 7
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 = []) : 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
        $iid = null;
32
        $aff = 0;
33
        if (preg_match('@^\s*(INSERT|REPLACE)\s+INTO@i', $this->statement)) {
34
            $iid = \pg_query($this->driver, 'SELECT lastval()');
35
            if ($iid) {
36
                $res = \pg_fetch_row($iid);
37
                $iid = $res[0];
38
            }
39
            $aff = \pg_affected_rows($temp);
40
        }
41
42
        return new Result($temp, $iid, $aff);
43
    }
44
}