1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\MysqlCompat; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOStatement; |
7
|
|
|
|
8
|
|
|
class Result |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connection |
12
|
|
|
*/ |
13
|
|
|
protected $connection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var PDOStatement |
17
|
|
|
*/ |
18
|
|
|
protected $statement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The last fetched row. |
22
|
|
|
* |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $lastFetch; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a Result instance. |
29
|
|
|
* |
30
|
|
|
* @param PDOStatement $statement |
31
|
|
|
* @param Connection $connection |
32
|
|
|
*/ |
33
|
|
|
public function __construct(PDOStatement $statement, Connection $connection) |
34
|
|
|
{ |
35
|
|
|
$this->statement = $statement; |
36
|
|
|
$this->connection = $connection; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the PDO statement. |
41
|
|
|
* |
42
|
|
|
* @return PDOStatement |
43
|
|
|
*/ |
44
|
|
|
public function getStatement() |
45
|
|
|
{ |
46
|
|
|
return $this->statement; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function toArray() |
50
|
|
|
{ |
51
|
|
|
return $this->statement->fetchAll(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function count() |
55
|
|
|
{ |
56
|
|
|
return $this->statement->rowCount(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function column($column, $row) |
60
|
|
|
{ |
61
|
|
|
$rows = $this->statement->fetchAll(PDO::FETCH_BOTH); |
62
|
|
|
|
63
|
|
|
return $rows[$row][$column]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function fetch($fetchMode = PDO::FETCH_BOTH, $orientation = PDO::FETCH_ORI_NEXT, $offset = 0) |
67
|
|
|
{ |
68
|
|
|
$result = $this->statement->fetch($fetchMode, $orientation, $offset); |
69
|
|
|
$this->lastFetch = is_object($result) ? clone($result) : $result; |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function fetchObject($class = 'stdClass', array $params = []) |
75
|
|
|
{ |
76
|
|
|
$this->statement->setFetchMode(PDO::FETCH_CLASS, $class, $params); |
77
|
|
|
|
78
|
|
|
return $this->fetch(PDO::FETCH_CLASS); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function fetchAll() |
82
|
|
|
{ |
83
|
|
|
return $this->statement->fetchAll(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function free() |
87
|
|
|
{ |
88
|
|
|
return $this->statement->closeCursor(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setCursor($rowNumber) |
92
|
|
|
{ |
93
|
|
|
$this->cursor = $rowNumber; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getColumnMeta($columnNumber) |
97
|
|
|
{ |
98
|
|
|
return $this->statement->getColumnMeta($columnNumber); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getColumnCount() |
102
|
|
|
{ |
103
|
|
|
return $this->statement->columnCount(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getConnection() |
107
|
|
|
{ |
108
|
|
|
return $this->connection; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getLastFetch() |
112
|
|
|
{ |
113
|
|
|
return $this->lastFetch; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: