3
3
4
4
namespace TheCodingMachine \TDBM ;
5
5
6
+ use Psr \Log \NullLogger ;
6
7
use function array_map ;
7
8
use Doctrine \DBAL \Connection ;
8
9
use Doctrine \DBAL \Statement ;
@@ -66,23 +67,37 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
66
67
67
68
private $ logger ;
68
69
70
+ private function __construct ()
71
+ {
72
+ }
73
+
69
74
/**
70
75
* @param mixed[] $parameters
71
76
*/
72
- public function __construct (QueryFactory $ queryFactory , array $ parameters , ObjectStorageInterface $ objectStorage , ?string $ className , TDBMService $ tdbmService , MagicQuery $ magicQuery , int $ mode , LoggerInterface $ logger )
77
+ public static function createResultIterator (QueryFactory $ queryFactory , array $ parameters , ObjectStorageInterface $ objectStorage , ?string $ className , TDBMService $ tdbmService , MagicQuery $ magicQuery , int $ mode , LoggerInterface $ logger ): self
73
78
{
79
+ $ iterator = new self ();
74
80
if ($ mode !== TDBMService::MODE_CURSOR && $ mode !== TDBMService::MODE_ARRAY ) {
75
81
throw new TDBMException ("Unknown fetch mode: ' " .$ mode ."' " );
76
82
}
77
83
78
- $ this ->queryFactory = $ queryFactory ;
79
- $ this ->objectStorage = $ objectStorage ;
80
- $ this ->className = $ className ;
81
- $ this ->tdbmService = $ tdbmService ;
82
- $ this ->parameters = $ parameters ;
83
- $ this ->magicQuery = $ magicQuery ;
84
- $ this ->mode = $ mode ;
85
- $ this ->logger = $ logger ;
84
+ $ iterator ->queryFactory = $ queryFactory ;
85
+ $ iterator ->objectStorage = $ objectStorage ;
86
+ $ iterator ->className = $ className ;
87
+ $ iterator ->tdbmService = $ tdbmService ;
88
+ $ iterator ->parameters = $ parameters ;
89
+ $ iterator ->magicQuery = $ magicQuery ;
90
+ $ iterator ->mode = $ mode ;
91
+ $ iterator ->logger = $ logger ;
92
+ return $ iterator ;
93
+ }
94
+
95
+ public static function createEmpyIterator (): self
96
+ {
97
+ $ iterator = new self ();
98
+ $ iterator ->totalCount = 0 ;
99
+ $ iterator ->logger = new NullLogger ();
100
+ return $ iterator ;
86
101
}
87
102
88
103
protected function executeCountQuery (): void
@@ -113,6 +128,9 @@ public function count(): int
113
128
*/
114
129
public function toArray (): array
115
130
{
131
+ if ($ this ->totalCount === 0 ) {
132
+ return [];
133
+ }
116
134
return iterator_to_array ($ this ->getIterator ());
117
135
}
118
136
@@ -125,6 +143,9 @@ public function toArray(): array
125
143
*/
126
144
public function map (callable $ callable ): MapIterator
127
145
{
146
+ if ($ this ->totalCount === 0 ) {
147
+ return new MapIterator ([], $ callable );
148
+ }
128
149
return new MapIterator ($ this ->getIterator (), $ callable );
129
150
}
130
151
@@ -141,10 +162,12 @@ public function map(callable $callable): MapIterator
141
162
public function getIterator ()
142
163
{
143
164
if ($ this ->innerResultIterator === null ) {
144
- if ($ this ->mode === TDBMService::MODE_CURSOR ) {
145
- $ this ->innerResultIterator = new InnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
165
+ if ($ this ->totalCount === 0 ) {
166
+ $ this ->innerResultIterator = InnerResultArray::createEmpyIterator ();
167
+ } elseif ($ this ->mode === TDBMService::MODE_CURSOR ) {
168
+ $ this ->innerResultIterator = InnerResultIterator::createInnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
146
169
} else {
147
- $ this ->innerResultIterator = new InnerResultArray ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
170
+ $ this ->innerResultIterator = InnerResultArray:: createInnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
148
171
}
149
172
}
150
173
@@ -159,7 +182,10 @@ public function getIterator()
159
182
*/
160
183
public function take ($ offset , $ limit )
161
184
{
162
- return new PageIterator ($ this , $ this ->queryFactory ->getMagicSql (), $ this ->parameters , $ limit , $ offset , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->mode , $ this ->logger );
185
+ if ($ this ->totalCount === 0 ) {
186
+ return PageIterator::createEmpyIterator ($ this );
187
+ }
188
+ return PageIterator::createResultIterator ($ this , $ this ->queryFactory ->getMagicSql (), $ this ->parameters , $ limit , $ offset , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->mode , $ this ->logger );
163
189
}
164
190
165
191
/**
@@ -265,12 +291,15 @@ public function jsonSerialize($stopRecursion = false)
265
291
*/
266
292
public function first ()
267
293
{
294
+ if ($ this ->totalCount === 0 ) {
295
+ return null ;
296
+ }
268
297
$ page = $ this ->take (0 , 1 );
269
298
foreach ($ page as $ bean ) {
270
299
return $ bean ;
271
300
}
272
301
273
- return ;
302
+ return null ;
274
303
}
275
304
276
305
/**
@@ -292,6 +321,9 @@ public function first()
292
321
public function withOrder ($ orderBy ) : ResultIterator
293
322
{
294
323
$ clone = clone $ this ;
324
+ if ($ this ->totalCount === 0 ) {
325
+ return $ clone ;
326
+ }
295
327
$ clone ->queryFactory = clone $ this ->queryFactory ;
296
328
$ clone ->queryFactory ->sort ($ orderBy );
297
329
$ clone ->innerResultIterator = null ;
@@ -313,6 +345,9 @@ public function withOrder($orderBy) : ResultIterator
313
345
public function withParameters (array $ parameters ) : ResultIterator
314
346
{
315
347
$ clone = clone $ this ;
348
+ if ($ this ->totalCount === 0 ) {
349
+ return $ clone ;
350
+ }
316
351
$ clone ->parameters = $ parameters ;
317
352
$ clone ->innerResultIterator = null ;
318
353
$ clone ->totalCount = null ;
0 commit comments