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