Skip to content

Commit 3f51d4b

Browse files
authored
Merge pull request #267 from envms/feature/fetch-cursor
Implement fetch() cursor support
2 parents fed1d06 + 0da6e44 commit 3f51d4b

5 files changed

Lines changed: 82 additions & 64 deletions

File tree

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ FluentPDO is a PHP SQL query builder using PDO. It's a quick and light library f
1313

1414
The latest (2.x) release of FluentPDO officially supports PHP 7.1, 7.2 and 7.3. v2.x is actively maintained.
1515

16-
The legacy (1.x) release of FluentPDO works with PHP 5.4 to 7.1. **Note:** The v1.x branch is no longer supported and will not be maintained or updated.
16+
The legacy (1.x) release of FluentPDO works with PHP 5.4 to 7.1. **Note:** v1.x is no longer supported and will not be maintained or updated.
1717

1818
## Reference
1919

@@ -23,14 +23,14 @@ The legacy (1.x) release of FluentPDO works with PHP 5.4 to 7.1. **Note:** The v
2323

2424
### Composer
2525

26-
The preferred way to install FluentPDO is via [composer](http://getcomposer.org/). Version 2.0 is in beta! Please start using v2.0.0 in your projects
27-
and let us know of any issues you find, and they will be resolved quickly. No further breaking changes will be introduced in the 2.0 branch.
26+
The preferred way to install FluentPDO is via [composer](http://getcomposer.org/). Version 2.0 is now released! Please start using 2.x in your projects
27+
and let us know of any issues you find, they will be resolved quickly.
2828

2929
Add the following line in your `composer.json` file:
3030

3131
"require": {
3232
...
33-
"envms/fluentpdo": "^2.0.0-beta1"
33+
"envms/fluentpdo": "^2.1.0"
3434
}
3535

3636
update your dependencies with `composer update`, and you're done!

src/Queries/Base.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
abstract class Base implements \IteratorAggregate
1111
{
1212

13-
/** @var \PDOStatement */
14-
private $result;
15-
1613
/** @var float */
1714
private $totalTime;
1815

@@ -25,6 +22,9 @@ abstract class Base implements \IteratorAggregate
2522
/** @var Query */
2623
protected $fluent;
2724

25+
/** @var \PDOStatement */
26+
protected $result;
27+
2828
/** @var array - definition clauses */
2929
protected $clauses = [];
3030
/** @var array */
@@ -38,6 +38,9 @@ abstract class Base implements \IteratorAggregate
3838
/** @var string */
3939
protected $message = '';
4040

41+
/** @var @var int */
42+
protected $currentFetchMode;
43+
4144
/**
4245
* BaseQuery constructor.
4346
*
@@ -48,6 +51,8 @@ protected function __construct(Query $fluent, $clauses)
4851
{
4952
$this->fluent = $fluent;
5053
$this->clauses = $clauses;
54+
$this->result = null;
55+
5156
$this->initClauses();
5257

5358
$this->regex = new Regex();
@@ -245,14 +250,6 @@ private function debugger()
245250
}
246251
}
247252

248-
/**
249-
* @return \PDO
250-
*/
251-
protected function getPDO()
252-
{
253-
return $this->fluent->getPdo();
254-
}
255-
256253
/**
257254
* @return Structure
258255
*/
@@ -535,12 +532,15 @@ private function setObjectFetchMode(\PDOStatement &$result): void
535532
{
536533
if ($this->object !== false) {
537534
if (class_exists($this->object)) {
538-
$result->setFetchMode(\PDO::FETCH_CLASS, $this->object);
535+
$this->currentFetchMode = \PDO::FETCH_CLASS;
536+
$result->setFetchMode($this->currentFetchMode, $this->object);
539537
} else {
540-
$result->setFetchMode(\PDO::FETCH_OBJ);
538+
$this->currentFetchMode = \PDO::FETCH_OBJ;
539+
$result->setFetchMode($this->currentFetchMode);
541540
}
542541
} elseif ($this->fluent->getPdo()->getAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE) == \PDO::FETCH_BOTH) {
543-
$result->setFetchMode(\PDO::FETCH_ASSOC);
542+
$this->currentFetchMode = \PDO::FETCH_ASSOC;
543+
$result->setFetchMode($this->currentFetchMode);
544544
}
545545
}
546546

src/Queries/Common.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@
77
/**
88
* CommonQuery add JOIN and WHERE clauses for (SELECT, UPDATE, DELETE)
99
*
10-
* @method leftJoin(string $statement) add LEFT JOIN to query
11-
* ($statement can be 'table' name only or 'table:' means back reference)
12-
* @method innerJoin(string $statement) add INNER JOIN to query
13-
* ($statement can be 'table' name only or 'table:' means back reference)
14-
* @method groupBy(string $column) add GROUP BY to query
15-
* @method having(string $column) add HAVING query
16-
* @method orderBy(string $column) add ORDER BY to query
17-
* @method limit(int $limit) add LIMIT to query
18-
* @method offset(int $offset) add OFFSET to query
10+
* @method $this from(string $table) - add FROM to DELETE query
11+
* @method $this leftJoin(string $statement) - add LEFT JOIN to query
12+
* $statement can be the 'table' name only or 'table:' to back reference the join
13+
* @method $this rightJoin(string $statement) - add RIGHT JOIN to query
14+
* @method $this innerJoin(string $statement) - add INNER JOIN to query
15+
* @method $this outerJoin(string $statement) - add OUTER JOIN to query
16+
* @method $this fullJoin(string $statement) - add FULL JOIN to query
17+
* @method $this group(string $column) - add GROUP BY to query
18+
* @method $this groupBy(string $column) - add GROUP BY to query
19+
* @method $this having(string $column) - add HAVING query
20+
* @method $this order(string $column) - add ORDER BY to query
21+
* @method $this orderBy(string $column) - add ORDER BY to query
22+
* @method $this limit(int $limit) - add LIMIT to query
23+
* @method $this offset(int $offset) - add OFFSET to query
24+
* @method $this comment(string $comment) - add COMMENT (--) to query
1925
*/
2026
abstract class Common extends Base
2127
{
2228

2329
/** @var array - methods which are allowed to be called by the magic method __call() */
2430
private $validMethods = [
31+
'comment',
2532
'from',
2633
'fullJoin',
2734
'group',
@@ -58,13 +65,11 @@ public function __call($name, $parameters = [])
5865

5966
$clause = Utilities::toUpperWords($name);
6067

61-
if ($clause == 'GROUP') {
62-
$clause = 'GROUP BY';
68+
if ($clause == 'GROUP' || $clause == 'ORDER') {
69+
$clause = "{$clause} BY";
6370
}
64-
if ($clause == 'ORDER') {
65-
$clause = 'ORDER BY';
66-
}
67-
if ($clause == 'FOOT NOTE') {
71+
72+
if ($clause == 'COMMENT') {
6873
$clause = "\n--";
6974
}
7075

src/Queries/Insert.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function execute($sequence = null)
122122
{
123123
$result = parent::execute();
124124
if ($result) {
125-
return $this->getPDO()->lastInsertId($sequence);
125+
return $this->fluent->getPdo()->lastInsertId($sequence);
126126
}
127127

128128
return false;

src/Queries/Select.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getFromAlias()
9191
*
9292
* @return string
9393
*/
94-
public function fetchColumn($columnNumber = 0)
94+
public function fetchColumn(int $columnNumber = 0)
9595
{
9696
if (($s = $this->execute()) !== false) {
9797
return $s->fetchColumn($columnNumber);
@@ -103,27 +103,30 @@ public function fetchColumn($columnNumber = 0)
103103
/**
104104
* Fetch first row or column
105105
*
106-
* @param string $column column name or empty string for the whole row
106+
* @param string $column - column name or empty string for the whole row
107+
* @param int $cursorOrientation
107108
*
108109
* @throws Exception
109110
*
110111
* @return mixed string, array or false if there is no row
111112
*/
112-
public function fetch($column = '')
113+
public function fetch(?string $column = null, int $cursorOrientation = \PDO::FETCH_ORI_NEXT)
113114
{
114-
$result = $this->execute();
115+
if ($this->result === null) {
116+
$this->execute();
117+
}
115118

116-
if ($result === false) {
119+
if ($this->result === false) {
117120
return false;
118121
}
119122

120-
$row = $result->fetch();
123+
$row = $this->result->fetch($this->currentFetchMode, $cursorOrientation);
121124

122125
if ($this->fluent->convertRead === true) {
123-
$row = Utilities::stringToNumeric($result, $row);
126+
$row = Utilities::stringToNumeric($this->result, $row);
124127
}
125128

126-
if ($row && $column != '') {
129+
if ($row && $column !== null) {
127130
if (is_object($row)) {
128131
return $row->{$column};
129132
} else {
@@ -156,16 +159,15 @@ public function fetchPairs($key, $value, $object = false)
156159

157160
/** Fetch all row
158161
*
159-
* @param string $index specify index column
160-
* @param string $selectOnly select columns which could be fetched
162+
* @param string $index - specify index column. Allows for data organization by field using 'field[]'
163+
* @param string $selectOnly - select columns which could be fetched
161164
*
162165
* @throws Exception
163166
*
164-
* @return \PDOStatement|array of fetched rows
167+
* @return array|bool - fetched rows
165168
*/
166169
public function fetchAll($index = '', $selectOnly = '')
167170
{
168-
// allows for data organization by field -> fetchAll('column[]')
169171
$indexAsArray = strpos($index, '[]');
170172

171173
if ($indexAsArray !== false) {
@@ -177,23 +179,7 @@ public function fetchAll($index = '', $selectOnly = '')
177179
}
178180

179181
if ($index) {
180-
$data = [];
181-
182-
foreach ($this as $row) {
183-
if (is_object($row)) {
184-
$key = $row->{$index};
185-
} else {
186-
$key = $row[$index];
187-
}
188-
189-
if ($indexAsArray) {
190-
$data[$key][] = $row;
191-
} else {
192-
$data[$key] = $row;
193-
}
194-
}
195-
196-
return $data;
182+
return $this->buildSelectData($index, $indexAsArray);
197183
} else {
198184
if (($result = $this->execute()) !== false) {
199185
if ($this->fluent->convertRead === true) {
@@ -203,7 +189,7 @@ public function fetchAll($index = '', $selectOnly = '')
203189
}
204190
}
205191

206-
return $result;
192+
return false;
207193
}
208194
}
209195

@@ -235,4 +221,31 @@ public function getIterator()
235221
}
236222
}
237223

224+
/**
225+
* @param $index
226+
* @param $indexAsArray
227+
*
228+
* @return array
229+
*/
230+
private function buildSelectData($index, $indexAsArray)
231+
{
232+
$data = [];
233+
234+
foreach ($this as $row) {
235+
if (is_object($row)) {
236+
$key = $row->{$index};
237+
} else {
238+
$key = $row[$index];
239+
}
240+
241+
if ($indexAsArray) {
242+
$data[$key][] = $row;
243+
} else {
244+
$data[$key] = $row;
245+
}
246+
}
247+
248+
return $data;
249+
}
250+
238251
}

0 commit comments

Comments
 (0)