1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Entities\Query; |
10
|
|
|
|
11
|
|
|
use PDOStatement; |
12
|
|
|
use Spiral\Database\Helpers\QueryInterpolator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Works as prepared PDOStatement. |
16
|
|
|
*/ |
17
|
|
|
class PDOResult extends PDOStatement |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Limits after which no records will be dumped in __debugInfo. |
21
|
|
|
*/ |
22
|
|
|
const DUMP_LIMIT = 500; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $parameters = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $parameters |
31
|
|
|
*/ |
32
|
|
|
protected function __construct(array $parameters) |
33
|
|
|
{ |
34
|
|
|
$this->parameters = $parameters; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Bind a column value to a PHP variable. Aliased to bindParam. |
39
|
|
|
* |
40
|
|
|
* @param int|string $columnID Column number (0 - first column) |
41
|
|
|
* @param mixed $variable |
42
|
|
|
* |
43
|
|
|
* @return self|$this |
44
|
|
|
*/ |
45
|
|
|
public function bind($columnID, &$variable): PDOResult |
46
|
|
|
{ |
47
|
|
|
if (is_numeric($columnID)) { |
48
|
|
|
//PDO columns are 1-indexed |
49
|
|
|
$columnID = $columnID + 1; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->bindColumn($columnID, $variable); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Just an alias. |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public function countColumns(): int |
63
|
|
|
{ |
64
|
|
|
return $this->columnCount(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function queryString(): string |
71
|
|
|
{ |
72
|
|
|
return QueryInterpolator::interpolate($this->queryString, $this->parameters); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* Attention, this method will return 0 for SQLite databases. |
79
|
|
|
* |
80
|
|
|
* @link http://php.net/manual/en/pdostatement.rowcount.php |
81
|
|
|
* @link http://stackoverflow.com/questions/15003232/pdo-returns-wrong-rowcount-after-select-statement |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function count(): int |
86
|
|
|
{ |
87
|
|
|
return $this->rowCount(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function close() |
94
|
|
|
{ |
95
|
|
|
$this->closeCursor(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function toArray(): array |
102
|
|
|
{ |
103
|
|
|
return $this->fetchAll(\PDO::FETCH_ASSOC); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function __debugInfo() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'query' => $this->queryString(), |
113
|
|
|
'count' => $this->count(), |
114
|
|
|
'rows' => $this->count() > static::DUMP_LIMIT ? '[TOO MANY ROWS]' : $this->fetchAll(\PDO::FETCH_ASSOC) |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|