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

Statement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace vakata\database\driver\pdo;
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(\PDOStatement $statement, \PDO $driver)
16
    {
17
        $this->statement = $statement;
18
        $this->driver = $driver;
19
    }
20
    public function execute(array $data = []) : ResultInterface
21
    {
22
        $data = array_values($data);
23
        //var_dump($data);
24
        foreach ($data as $i => $v) {
25
            switch (gettype($v)) {
26
                case 'boolean':
27
                    $this->statement->bindValue($i+1, $v, \PDO::PARAM_BOOL);
28
                    break;
29
                case 'integer':
30
                    $this->statement->bindValue($i+1, $v, \PDO::PARAM_INT);
31
                    break;
32
                case 'NULL':
33
                    $this->statement->bindValue($i+1, $v, \PDO::PARAM_NULL);
34
                    break;
35
                case 'double':
36
                    $this->statement->bindValue($i+1, $v);
37
                    break;
38
                default:
39
                    // keep in mind oracle needs a transaction when inserting LOBs, aside from the specific syntax:
40
                    // INSERT INTO table (column, lobcolumn) VALUES (?, ?, EMPTY_BLOB()) RETURNING lobcolumn INTO ?
41
                    if (is_resource($v) && get_resource_type($v) === 'stream') {
42
                        $this->statement->bindParam($i+1, $v, \PDO::PARAM_LOB);
43
                        continue;
44
                    }
45
                    if (!is_string($data[$i])) {
46
                        $data[$i] = serialize($data[$i]);
47
                    }
48
                    $this->statement->bindValue($i+1, $v);
49
                    break;
50
            }
51
        }
52
        try {
53
            if (!$this->statement->execute()) {
54
                throw new DBException('Prepared execute error');
55
            }
56
        } catch (\Exception $e) {
57
            throw new DBException($e->getMessage());
58
        }
59
        return new Result($this->statement, $this->driver);
60
    }
61
}