|
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) { |
|
|
|
|
|
|
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
|
|
|
} |