Completed
Push — master ( a98725...347d9e )
by Ivan
03:41
created

Statement   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 20 6
1
<?php
2
3
namespace vakata\database\driver\odbc;
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 $sql;
14
    protected $driver;
15
16
    public function __construct(string $statement, $driver)
17
    {
18
        $this->sql = $statement;
19
        $this->driver = $driver;
20
        $this->statement = \odbc_prepare($this->driver, $statement);
21
    }
22
    public function execute(array $data = []) : ResultInterface
23
    {
24
        if (!is_array($data)) {
25
            $data = array();
26
        }
27
        $temp = \odbc_execute($this->statement, $data);
28
        if (!$temp) {
29
            throw new DBException('Could not execute query : '.\odbc_errormsg($this->driver));
30
        }
31
        $iid = null;
32
        if (preg_match('@^\s*(INSERT|REPLACE)\s+INTO@i', $this->sql)) {
33
            $iid = \odbc_exec($this->driver, 'SELECT @@IDENTITY');
34
            if ($iid && \odbc_fetch_row($iid)) {
35
                $iid = odbc_result($result, 1);
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
36
            } else {
37
                $iid = null;
38
            }
39
        }
40
        return new Result($this->statement, $data, $iid);
41
    }
42
}