Skip to content

Commit be31cd7

Browse files
committed
- Added RunnableSelect::fetchObjects, RunnableSelect::fetchObjectsLazy, RunnableSelect::fetchObject
1 parent de8a99e commit be31cd7

File tree

4 files changed

+114
-7
lines changed

4 files changed

+114
-7
lines changed

src/Builder/Helpers/LazyRowGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct($preserveTypes) {
2121
* @return array[]|\Generator
2222
*/
2323
public function generate(QueryStatement $statement, Closure $callback = null) {
24-
while($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
24+
while($row = $statement->fetch()) {
2525
if($this->preserveTypes) {
2626
$columnDefinitions = FieldTypeProvider::getFieldTypes($statement);
2727
$row = FieldValueConverter::convertValues($row, $columnDefinitions);

src/Builder/Helpers/YieldPolyfillIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function current() {
6464
*/
6565
public function next() {
6666
$this->idx++;
67-
$this->currentItem = $this->getStmt()->fetch(PDO::FETCH_ASSOC);
67+
$this->currentItem = $this->getStmt()->fetch();
6868
}
6969

7070
/**

src/Builder/QueryStatement.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ public function getStatement() {
3939
return $this->statement;
4040
}
4141

42+
/**
43+
* @param int $mode
44+
* @param mixed $arg0
45+
* @param array $arg1
46+
* @return $this
47+
*/
48+
public function setFetchMode($mode, $arg0 = null, array $arg1 = null) {
49+
if($arg1 !== null) {
50+
/** @noinspection PhpMethodParametersCountMismatchInspection */
51+
$this->statement->setFetchMode($mode, $arg0, $arg1);
52+
return $this;
53+
}
54+
if($arg0 !== null) {
55+
/** @noinspection PhpMethodParametersCountMismatchInspection */
56+
$this->statement->setFetchMode($mode, $arg0);
57+
return $this;
58+
}
59+
$this->statement->setFetchMode($mode);
60+
return $this;
61+
}
62+
4263
/**
4364
* @param array $params
4465
* @throws SqlException
@@ -62,7 +83,7 @@ public function execute(array $params = []) {
6283
* @param array $ctorArgs
6384
* @return array
6485
*/
65-
public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []) {
86+
public function fetchAll($fetchStyle = null, $fetchArgument = null, array $ctorArgs = []) {
6687
return $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
6788
if($fetchArgument !== null) {
6889
return $this->statement->fetchAll($fetchStyle, $fetchArgument, $ctorArgs);
@@ -77,7 +98,7 @@ public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null,
7798
* @param int $cursorOffset
7899
* @return mixed
79100
*/
80-
public function fetch($fetchStyle = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
101+
public function fetch($fetchStyle = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
81102
return $this->exceptionHandler(function() use ($fetchStyle, $cursorOrientation, $cursorOffset) {
82103
return $this->statement->fetch($fetchStyle, $cursorOrientation, $cursorOffset);
83104
});

src/Builder/RunnableSelect.php

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
namespace Kir\MySQL\Builder;
33

44
use Closure;
5+
use Generator;
56
use IteratorAggregate;
67
use Kir\MySQL\Builder\Helpers\DBIgnoreRow;
78
use Kir\MySQL\Builder\Helpers\FieldTypeProvider;
89
use Kir\MySQL\Builder\Helpers\FieldValueConverter;
910
use Kir\MySQL\Builder\Helpers\LazyRowGenerator;
1011
use Kir\MySQL\Builder\Helpers\YieldPolyfillIterator;
12+
use PDO;
1113
use Traversable;
1214

1315
/**
@@ -62,7 +64,8 @@ public function setPreserveTypes($preserveTypes = true) {
6264
*/
6365
public function fetchRows(Closure $callback = null) {
6466
return $this->createTempStatement(function (QueryStatement $statement) use ($callback) {
65-
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);
67+
$statement->setFetchMode(PDO::FETCH_ASSOC);
68+
$data = $statement->fetchAll();
6669
if($this->preserveTypes) {
6770
$columnDefinitions = FieldTypeProvider::getFieldTypes($statement);
6871
foreach($data as &$row) {
@@ -93,10 +96,13 @@ public function fetchRows(Closure $callback = null) {
9396
public function fetchRowsLazy(Closure $callback = null) {
9497
if(version_compare(PHP_VERSION, '5.5', '<')) {
9598
return new YieldPolyfillIterator($callback, $this->preserveTypes, function () {
96-
return $this->createStatement();
99+
$statement = $this->createStatement();
100+
$statement->setFetchMode(PDO::FETCH_ASSOC);
101+
return $statement;
97102
});
98103
}
99104
$statement = $this->createStatement();
105+
$statement->setFetchMode(PDO::FETCH_ASSOC);
100106
$generator = new LazyRowGenerator($this->preserveTypes);
101107
return $generator->generate($statement, $callback);
102108
}
@@ -108,7 +114,87 @@ public function fetchRowsLazy(Closure $callback = null) {
108114
*/
109115
public function fetchRow(Closure $callback = null) {
110116
return $this->createTempStatement(function (QueryStatement $statement) use ($callback) {
111-
$row = $statement->fetch(\PDO::FETCH_ASSOC);
117+
$statement->setFetchMode(PDO::FETCH_ASSOC);
118+
$row = $statement->fetch();
119+
if(!is_array($row)) {
120+
return [];
121+
}
122+
if($this->preserveTypes) {
123+
$columnDefinitions = FieldTypeProvider::getFieldTypes($statement);
124+
$row = FieldValueConverter::convertValues($row, $columnDefinitions);
125+
}
126+
if($callback !== null) {
127+
$result = $callback($row);
128+
if($result !== null) {
129+
$row = $result;
130+
}
131+
}
132+
return $row;
133+
});
134+
}
135+
136+
/**
137+
* @param string $className
138+
* @param Closure $callback
139+
* @return \array[]
140+
* @throws \Exception
141+
*/
142+
public function fetchObjects($className, Closure $callback = null) {
143+
return $this->createTempStatement(function (QueryStatement $statement) use ($className, $callback) {
144+
$statement->setFetchMode(PDO::FETCH_CLASS, $className);
145+
$data = $statement->fetchAll();
146+
if($this->preserveTypes) {
147+
$columnDefinitions = FieldTypeProvider::getFieldTypes($statement);
148+
foreach($data as &$row) {
149+
$row = FieldValueConverter::convertValues($row, $columnDefinitions);
150+
}
151+
}
152+
if($callback !== null) {
153+
return call_user_func(function ($resultData = []) use ($data, $callback) {
154+
foreach($data as $row) {
155+
$result = $callback($row);
156+
if($result !== null && !($result instanceof DBIgnoreRow)) {
157+
$resultData[] = $result;
158+
} else {
159+
$resultData[] = $row;
160+
}
161+
}
162+
return $resultData;
163+
});
164+
}
165+
return $data;
166+
});
167+
}
168+
169+
/**
170+
* @param string $className
171+
* @param Closure $callback
172+
* @return array[]|Generator
173+
*/
174+
public function fetchObjectsLazy($className, Closure $callback = null) {
175+
if(version_compare(PHP_VERSION, '5.5', '<')) {
176+
return new YieldPolyfillIterator($callback, $this->preserveTypes, function () use ($className) {
177+
$statement = $this->createStatement();
178+
$statement->setFetchMode(PDO::FETCH_CLASS, $className);
179+
return $statement;
180+
});
181+
}
182+
$statement = $this->createStatement();
183+
$statement->setFetchMode(PDO::FETCH_CLASS, $className);
184+
$generator = new LazyRowGenerator($this->preserveTypes);
185+
return $generator->generate($statement, $callback);
186+
}
187+
188+
/**
189+
* @param string $className
190+
* @param Closure|null $callback
191+
* @return string[]
192+
* @throws \Exception
193+
*/
194+
public function fetchObject($className, Closure $callback = null) {
195+
return $this->createTempStatement(function (QueryStatement $statement) use ($className, $callback) {
196+
$statement->setFetchMode(PDO::FETCH_CLASS, $className);
197+
$row = $statement->fetch();
112198
if(!is_array($row)) {
113199
return [];
114200
}

0 commit comments

Comments
 (0)