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

Result   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 83
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A __destruct() 0 4 1
A affected() 0 4 1
A insertID() 0 4 1
A toArray() 0 4 1
A count() 0 4 1
A key() 0 4 1
A current() 0 4 1
A rewind() 0 12 3
A next() 0 13 3
A valid() 0 4 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\ResultInterface;
8
use \vakata\collection\Collection;
9
10
class Result implements ResultInterface
11
{
12
    protected $statement;
13
    protected $data;
14
    protected $columns;
15
    protected $last = null;
16
    protected $fetched = -1;
17
    protected $iid = null;
18
19
    public function __construct($statement, $data, $iid)
20
    {
21
        $this->statement = $statement;
22
        $this->data = $data;
23
        $this->iid = $iid;
24
        $this->columns = [];
25
        $i = 0;
26
        try {
27
            while ($temp = \odbc_field_name($this->statement, ++$i)) {
28
                $this->columns[] = $temp;
29
            }
30
        } catch (\Exception $ignore) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
        }
32
    }
33
    public function __destruct()
34
    {
35
        \odbc_free_result($this->statement);
36
    }
37
    public function affected() : int
38
    {
39
        return \odbc_num_rows($this->statement);
40
    }
41
    public function insertID()
42
    {
43
        return $this->iid;
44
    }
45
    public function toArray() : array
46
    {
47
        return iterator_to_array($this);
48
    }
49
50
    public function count()
51
    {
52
        return \odbc_num_rows($this->statement);
53
    }
54
55
    public function key()
56
    {
57
        return $this->fetched;
58
    }
59
    public function current()
60
    {
61
        return $this->last;
62
    }
63
    public function rewind()
64
    {
65
        if ($this->fetched >= 0) {
66
            $temp = \odbc_execute($this->statement, $this->data);
67
            if (!$temp) {
68
                throw new DBException('Could not execute query : '.\odbc_errormsg());
69
            }
70
        }
71
        $this->last = null;
72
        $this->fetched = -1;
73
        $this->next();
74
    }
75
    public function next()
76
    {
77
        $this->fetched ++;
78
        $temp = \odbc_fetch_row($this->statement);
79
        if (!$temp) {
80
            $this->last = false;
81
        } else {
82
            $this->last = [];
83
            foreach ($this->columns as $col) {
84
                $this->last[$col] = \odbc_result($this->statement, $col);
85
            }
86
        }
87
    }
88
    public function valid()
89
    {
90
        return !!$this->last;
91
    }
92
}