Completed
Push — master ( 347d9e...b8cb2d )
by Ivan
05:29
created

Result::insertID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace vakata\database\driver\odbc;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\ResultInterface;
8
use \vakata\collection\Collection;
9
10
class Result implements ResultInterface
11
{
12
    protected $statement;
13
    protected $data;
14
    protected $charIn;
15
    protected $charOut;
16
    protected $columns;
17
    protected $last = null;
18
    protected $fetched = -1;
19
    protected $iid = null;
20
21
    public function __construct($statement, $data, $iid, $charIn = null, $charOut = null)
22
    {
23
        $this->statement = $statement;
24
        $this->data = $data;
25
        $this->iid = $iid;
26
        $this->charIn = $charIn;
27
        $this->charOut = $charOut;
28
        $this->columns = [];
29
        $i = 0;
30
        try {
31
            while ($temp = \odbc_field_name($this->statement, ++$i)) {
32
                $this->columns[] = $temp;
33
            }
34
        } catch (\Exception $ignore) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
        }
36
    }
37
    public function __destruct()
38
    {
39
        \odbc_free_result($this->statement);
40
    }
41
    public function affected() : int
42
    {
43
        return \odbc_num_rows($this->statement);
44
    }
45
    public function insertID()
46
    {
47
        return $this->iid;
48
    }
49
    public function toArray() : array
50
    {
51
        return iterator_to_array($this);
52
    }
53
54
    public function count()
55
    {
56
        return \odbc_num_rows($this->statement);
57
    }
58
59
    public function key()
60
    {
61
        return $this->fetched;
62
    }
63
    public function current()
64
    {
65
        return $this->last;
66
    }
67
    public function rewind()
68
    {
69
        if ($this->fetched >= 0) {
70
            $temp = \odbc_execute($this->statement, $this->data);
71
            if (!$temp) {
72
                throw new DBException('Could not execute query : '.\odbc_errormsg());
73
            }
74
        }
75
        $this->last = null;
76
        $this->fetched = -1;
77
        $this->next();
78
    }
79
    public function next()
80
    {
81
        $this->fetched ++;
82
        $temp = \odbc_fetch_row($this->statement);
83
        if (!$temp) {
84
            $this->last = false;
85
        } else {
86
            $this->last = [];
87
            foreach ($this->columns as $col) {
88
                $this->last[$col] = \odbc_result($this->statement, $col);
89
            }
90
            $this->last = $this->convert($this->last);
91
        }
92
    }
93
    public function valid()
94
    {
95
        return !!$this->last;
96
    }
97
    protected function convert($data)
98
    {
99
        if (!is_callable("\iconv") || !isset($this->charIn) || !isset($this->charOut)) {
100
            return $data;
101
        }
102
        if (is_array($data)) {
103
            foreach ($data as $k => $v) {
104
                $data[$k] = $this->convert($v);
105
            }
106
            return $data;
107
        }
108
        if (is_string($data)) {
109
            return \iconv($this->charIn, $this->charOut, $data);
110
        }
111
        return $data;
112
    }
113
}