1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\MysqlCompat; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOStatement; |
7
|
|
|
|
8
|
|
|
class Result |
9
|
|
|
{ |
10
|
|
|
const FETCH_ASSOC = 1; |
11
|
|
|
const FETCH_NUM = 2; |
12
|
|
|
const FETCH_BOTH = 3; |
13
|
|
|
const FETCH_OBJ = PDO::FETCH_OBJ; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Connection |
17
|
|
|
*/ |
18
|
|
|
protected $connection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PDOStatement |
22
|
|
|
*/ |
23
|
|
|
protected $statement; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $cursor; |
29
|
|
|
|
30
|
|
|
protected $lastFetch; |
31
|
|
|
|
32
|
|
|
public function __construct(PDOStatement $statement, Connection $connection) |
33
|
|
|
{ |
34
|
|
|
$this->statement = $statement; |
35
|
|
|
$this->connection = $connection; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the PDO statement. |
40
|
|
|
* |
41
|
|
|
* @return PDOStatement |
42
|
|
|
*/ |
43
|
|
|
public function getStatement() |
44
|
|
|
{ |
45
|
|
|
return $this->statement; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function toArray() |
49
|
|
|
{ |
50
|
|
|
return $this->statement->fetchAll(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function count() |
54
|
|
|
{ |
55
|
|
|
return $this->statement->rowCount(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function column($column, $row) |
59
|
|
|
{ |
60
|
|
|
$row = $this->statement->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, $row); |
61
|
|
|
|
62
|
|
|
return $row[$column]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function fetch($style = self::FETCH_BOTH, $orientation = PDO::FETCH_ORI_NEXT, $offset = 0) |
66
|
|
|
{ |
67
|
|
|
$fetchMode = $this->convertFetchStyle($style); |
68
|
|
|
|
69
|
|
|
if ($this->cursor !== null) { |
70
|
|
|
$result = $this->statement->fetch($fetchMode, PDO::FETCH_ORI_ABS, $this->cursor); |
71
|
|
|
$this->cursor++; |
72
|
|
|
} else { |
73
|
|
|
$result = $this->statement->fetch($fetchMode, $orientation, $offset); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->lastFetch = is_object($result) ? clone($result) : $result; |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function fetchObject($class = 'stdClass', array $params = []) |
82
|
|
|
{ |
83
|
|
|
$this->statement->setFetchMode(PDO::FETCH_CLASS, $class, $params); |
84
|
|
|
|
85
|
|
|
return $this->fetch(static::FETCH_OBJ); |
86
|
|
|
} |
87
|
|
|
public function fetchAll() |
88
|
|
|
{ |
89
|
|
|
return $this->statement->fetchAll(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function free() |
93
|
|
|
{ |
94
|
|
|
return $this->statement->closeCursor(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function convertFetchStyle($style) |
98
|
|
|
{ |
99
|
|
|
switch ($style) { |
100
|
|
|
case static::FETCH_ASSOC: |
101
|
|
|
return PDO::FETCH_ASSOC; |
102
|
|
|
|
103
|
|
|
case static::FETCH_NUM: |
104
|
|
|
return PDO::FETCH_NUM; |
105
|
|
|
|
106
|
|
|
case static::FETCH_BOTH: |
107
|
|
|
return PDO::FETCH_BOTH; |
108
|
|
|
|
109
|
|
|
case static::FETCH_OBJ: |
110
|
|
|
return PDO::FETCH_CLASS; |
111
|
|
|
|
112
|
|
|
default: |
113
|
|
|
return $style; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setCursor($rowNumber) |
118
|
|
|
{ |
119
|
|
|
$this->cursor = $rowNumber; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getColumnMeta($columnNumber) |
123
|
|
|
{ |
124
|
|
|
return $this->statement->getColumnMeta($columnNumber); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getColumnCount() |
128
|
|
|
{ |
129
|
|
|
return $this->statement->columnCount(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getConnection() |
133
|
|
|
{ |
134
|
|
|
return $this->connection; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getLastFetch() |
138
|
|
|
{ |
139
|
|
|
return $this->lastFetch; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|