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

Statement::execute()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 14
nc 8
nop 1
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
}