1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\MysqlCompat\BridgeComponents; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Mattbit\MysqlCompat\Result; |
7
|
|
|
use Mattbit\MysqlCompat\MysqlConstants; |
8
|
|
|
use Mattbit\MysqlCompat\Exception\QueryException; |
9
|
|
|
use Mattbit\MysqlCompat\Exception\NotSupportedException; |
10
|
|
|
|
11
|
|
|
trait FetchResults |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Fetch the next row from the result as an array. |
15
|
|
|
* |
16
|
|
|
* @param Result $result |
17
|
|
|
* @param int $resultType |
18
|
|
|
* |
19
|
|
|
* @return bool|array |
20
|
|
|
*/ |
21
|
|
|
public function fetchArray(Result $result, $resultType = MysqlConstants::FETCH_BOTH) |
22
|
|
|
{ |
23
|
|
|
$fetchMode = $this->convertFetchStyle($resultType); |
24
|
|
|
|
25
|
|
|
return $result->fetch($fetchMode); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fetch the next row as an associative array. |
30
|
|
|
* |
31
|
|
|
* @param Result $result |
32
|
|
|
* |
33
|
|
|
* @return bool|array |
34
|
|
|
*/ |
35
|
|
|
public function fetchAssoc(Result $result) |
36
|
|
|
{ |
37
|
|
|
return $this->fetchArray($result, MysqlConstants::FETCH_ASSOC); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Fetch the metadata of a column. |
42
|
|
|
* USE WITH CARE! Accuracy of results is not guaranteed. |
43
|
|
|
* |
44
|
|
|
* @param Result $result |
45
|
|
|
* @param int $fieldOffset |
46
|
|
|
* |
47
|
|
|
* @return bool|object |
48
|
|
|
* |
49
|
|
|
* @deprecated |
50
|
|
|
*/ |
51
|
|
|
public function fetchField(Result $result, $fieldOffset = 0) |
52
|
|
|
{ |
53
|
|
|
$meta = $result->getColumnMeta($fieldOffset); |
54
|
|
|
|
55
|
|
|
if ($meta === false) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$meta = (object) $meta; |
60
|
|
|
|
61
|
|
|
foreach ($meta->flags as $flag) { |
62
|
|
|
$meta->{$flag} = 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $meta; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function fetchLengths(Result $result) |
69
|
|
|
{ |
70
|
|
|
if (!is_array($result->getLastFetch())) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return array_values(array_map('strlen', $result->getLastFetch())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function fetchObject(Result $result, $className = 'stdClass', array $params = []) |
78
|
|
|
{ |
79
|
|
|
return $result->fetchObject($className, $params); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function fetchRow(Result $result) |
83
|
|
|
{ |
84
|
|
|
return $result->fetch(PDO::FETCH_NUM); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function result(Result $result, $row, $field = 0) |
88
|
|
|
{ |
89
|
|
|
$row = $result->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, $row); |
90
|
|
|
|
91
|
|
|
return $row[$field]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function dataSeek() |
95
|
|
|
{ |
96
|
|
|
throw new NotSupportedException('The mysql_data_seek function is not supported. You must refactor your code.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function numFields(Result $result) |
100
|
|
|
{ |
101
|
|
|
return $result->getColumnCount(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function numRows(Result $result) |
105
|
|
|
{ |
106
|
|
|
$query = $result->getStatement()->queryString; |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$countResult = $result->getConnection()->query( |
110
|
|
|
'SELECT COUNT(*) FROM (' . $query . ') AS ' . uniqid('count_') |
111
|
|
|
); |
112
|
|
|
} catch (QueryException $e) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return (int) $countResult->fetch()[0]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function convertFetchStyle($style) |
120
|
|
|
{ |
121
|
|
|
switch ($style) { |
122
|
|
|
case MysqlConstants::FETCH_ASSOC: |
123
|
|
|
return PDO::FETCH_ASSOC; |
124
|
|
|
|
125
|
|
|
case MysqlConstants::FETCH_NUM: |
126
|
|
|
return PDO::FETCH_NUM; |
127
|
|
|
|
128
|
|
|
case MysqlConstants::FETCH_BOTH: |
129
|
|
|
return PDO::FETCH_BOTH; |
130
|
|
|
|
131
|
|
|
case MysqlConstants::FETCH_OBJ: |
132
|
|
|
return PDO::FETCH_CLASS; |
133
|
|
|
|
134
|
|
|
default: |
135
|
|
|
return $style; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|