Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
result.php
1<?php
2namespace Bitrix\Main\DB;
3
18abstract class Result implements \IteratorAggregate
19{
21 protected $connection;
23 protected $resource;
25 protected $trackerQuery = null;
26
28 protected $converters = array();
30 protected $serializedFields = array();
32 protected $replacedAliases = array();
34 protected $fetchDataModifiers = array();
35
37 protected $count;
38
44 public function __construct($result, Connection $dbConnection = null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null)
45 {
46 $this->resource = $result;
47 $this->connection = $dbConnection;
48 $this->trackerQuery = $trackerQuery;
49 $resultFields = $this->getFields();
50 if ($resultFields && $this->connection)
51 {
52 $helper = $this->connection->getSqlHelper();
53 foreach ($resultFields as $key => $type)
54 {
55 $converter = $helper->getConverter($resultFields[$key]);
56 if (is_callable($converter))
57 {
58 $this->converters[$key] = $converter;
59 }
60 }
61 }
62 }
63
69 public function getResource()
70 {
71 return $this->resource;
72 }
73
83 public function setReplacedAliases(array $replacedAliases)
84 {
85 $this->replacedAliases = $replacedAliases;
86 }
87
96 public function addReplacedAliases(array $replacedAliases)
97 {
98 $this->replacedAliases = array_merge($this->replacedAliases, $replacedAliases);
99 }
100
109 {
110 $this->serializedFields = $serializedFields;
111 }
112
123 public function addFetchDataModifier($fetchDataModifier)
124 {
125 if (!is_callable($fetchDataModifier))
126 {
127 throw new \Bitrix\Main\ArgumentException('Data Modifier should be a callback');
128 }
129
130 $this->fetchDataModifiers[] = $fetchDataModifier;
131 }
132
138 public function fetchRaw()
139 {
140 if ($this->trackerQuery != null)
141 {
142 $this->trackerQuery->restartQuery();
143 }
144
145 $data = $this->fetchRowInternal();
146
147 if ($this->trackerQuery != null)
148 {
149 $this->trackerQuery->refinishQuery();
150 }
151
152 if (!$data)
153 {
154 return false;
155 }
156
157 return $data;
158 }
159
167 public function fetch(\Bitrix\Main\Text\Converter $converter = null)
168 {
169 $data = $this->fetchRaw();
170
171 if (!$data)
172 {
173 return false;
174 }
175
176 if ($this->converters)
177 {
178 foreach ($this->converters as $field => $convertDataModifier)
179 {
180 $data[$field] = call_user_func_array($convertDataModifier, array($data[$field]));
181 }
182 }
183
184 if ($this->serializedFields)
185 {
186 foreach ($this->serializedFields as $field)
187 {
188 if (isset($data[$field]))
189 $data[$field] = unserialize($data[$field]);
190 }
191 }
192
193 if ($this->replacedAliases)
194 {
195 foreach ($this->replacedAliases as $tech => $human)
196 {
197 $data[$human] = $data[$tech];
198 unset($data[$tech]);
199 }
200 }
201
202 if ($this->fetchDataModifiers)
203 {
204 foreach ($this->fetchDataModifiers as $fetchDataModifier)
205 {
206 $result = call_user_func_array($fetchDataModifier, array(&$data));
207
208 if (is_array($result))
209 {
210 $data = $result;
211 }
212 }
213 }
214
215 if ($converter !== null)
216 {
217 foreach ($data as $key => $val)
218 {
219 $data[$key] = $converter->encode(
220 $val,
221 ($data[$key."_TYPE"] ?? \Bitrix\Main\Text\Converter::TEXT)
222 );
223 }
224 }
225
226 return $data;
227 }
228
237 public function fetchAll(\Bitrix\Main\Text\Converter $converter = null)
238 {
239 $res = array();
240 while ($ar = $this->fetch($converter))
241 {
242 $res[] = $ar;
243 }
244 return $res;
245 }
246
252 abstract public function getFields();
253
259 abstract public function getSelectedRowsCount();
260
266 abstract protected function fetchRowInternal();
267
273 public function getTrackerQuery()
274 {
275 return $this->trackerQuery;
276 }
277
281 public function getConverters()
282 {
283 return $this->converters;
284 }
285
289 public function setConverters($converters)
290 {
291 $this->converters = $converters;
292 }
293
298 public function setCount($n)
299 {
300 $this->count = (int)$n;
301 }
302
308 public function getCount()
309 {
310 if($this->count !== null)
311 {
312 return $this->count;
313 }
314 throw new \Bitrix\Main\ObjectPropertyException("count");
315 }
316
324 public function getIterator(): \Traversable
325 {
326 return new ResultIterator($this);
327 }
328}
setSerializedFields(array $serializedFields)
Definition result.php:108
__construct($result, Connection $dbConnection=null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery=null)
Definition result.php:44
setConverters($converters)
Definition result.php:289
fetch(\Bitrix\Main\Text\Converter $converter=null)
Definition result.php:167
addFetchDataModifier($fetchDataModifier)
Definition result.php:123
setReplacedAliases(array $replacedAliases)
Definition result.php:83
addReplacedAliases(array $replacedAliases)
Definition result.php:96
fetchAll(\Bitrix\Main\Text\Converter $converter=null)
Definition result.php:237