Skip to content

Commit 64dda0b

Browse files
committed
new ResultSet Handling
1 parent 074249f commit 64dda0b

File tree

11 files changed

+235
-59
lines changed

11 files changed

+235
-59
lines changed

doc/initialize.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ $config = [
2323
'username' => '<the username if necessary>'
2424
'password' => 'the password if necessary>',
2525
EQM::RUN_MODE => [EQM::DEV_MODE | EQM::PROD_MODE]
26-
EQM::RESULT_SET_CLASS => '<a class that implements ResultSetInterface>'
2726
EQM::CONVENTION_HANDLER => <object that implements ConventionHandlerInterface>
2827
EQM::SQL_BUILDER => <object that implements SqlBuilderInterface>
2928
];
@@ -36,7 +35,6 @@ $config = [
3635
'username' => '<the username if necessary>'
3736
'password' => 'the password if necessary>',
3837
EQM::RUN_MODE => EQM::PROD_MODE,
39-
EQM::RESULT_SET_CLASS => 'troba\\EQM\\ResultSet'
4038
EQM::CONVENTION_HANDLER => new troba\EQM\DefaultConventionHandler(),
4139
EQM::SQL_BUILDER => new troba\EQM\MySqlBuilder()
4240
];

lib/troba/EQM/AbstractResultSet.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace troba\EQM;
4+
5+
abstract class AbstractResultSet implements \Iterator, \Countable
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $relations = [];
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $classname = '\StdClass';
16+
17+
/**
18+
* @param string $property
19+
* @param AbstractResultSet $resultSet
20+
* @param array|string $keys
21+
*/
22+
public function relate($property, $resultSet, $keys)
23+
{
24+
$keys = is_string($keys) ? [$keys] : $keys;
25+
foreach ($resultSet as $object) {
26+
$this->relations[$property][$object->{$keys[0]}][] = $object;
27+
}
28+
}
29+
30+
/**
31+
* @return object
32+
*/
33+
public function current()
34+
{
35+
$current = $this->getCurrent();
36+
if (count($this->relations) > 0) {
37+
$primary = EQM::tableMeta($this->classname)->getPrimary();
38+
$key = array_pop($primary);
39+
foreach ($this->relations as $property => $relation) {
40+
$current->{$property} = $relation[$current->{$key}];
41+
}
42+
43+
}
44+
return $current;
45+
}
46+
47+
/**
48+
* @param \PDOStatement|array $result
49+
* @param string $classname optional
50+
*/
51+
public abstract function __construct($result, $classname = '\StdClass');
52+
53+
/**
54+
* @return object
55+
*/
56+
protected abstract function getCurrent();
57+
58+
/**
59+
* @return void
60+
*/
61+
public abstract function rewind();
62+
63+
/**
64+
* @return bool
65+
*/
66+
public abstract function valid();
67+
68+
/**
69+
* @return void
70+
*/
71+
public abstract function next();
72+
73+
/**
74+
* @return mixed
75+
*/
76+
public abstract function key();
77+
/**
78+
* @return int the size of the the result set
79+
*/
80+
public abstract function count();
81+
82+
/**
83+
* @return array all objects as an array
84+
*/
85+
public abstract function all();
86+
87+
/**
88+
* @return object
89+
*/
90+
public abstract function one();
91+
}

lib/troba/EQM/EQM.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class EQM extends PDOWrapper
5454
* 'username' => '<the username if necessary>'
5555
* 'password' => 'the password if necessary>',
5656
* EQM::RUN_MODE => [EQM::DEV_MODE | EQM::PROD_MODE]
57-
* EQM::RESULT_SET_CLASS => '<a class that implements ResultSetInterface>'
5857
* EQM::CONVENTION_HANDLER => <object that implements ConventionHandlerInterface>
5958
* EQM::SQL_BUILDER => <object that implements SqlBuilderInterface>
6059
* ]
@@ -335,7 +334,7 @@ public static function query($objectOrClass = '\StdClass')
335334
* 'offset' => start point for the records
336335
*
337336
* @param array $queryParams
338-
* @return ResultSetInterface
337+
* @return AbstractResultSet
339338
*/
340339
public static function queryByArray($queryParams = [])
341340
{

lib/troba/EQM/PDOWrapper.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ class PDOWrapper
1212
*/
1313
const RUN_MODE = 'run_mode';
1414

15-
/**
16-
* ResultSet
17-
*/
18-
const RESULT_SET_CLASS = 'result_set_class';
19-
2015
/**
2116
* Run mode dev (for development)
2217
*/
@@ -70,11 +65,6 @@ public static function initialize($config = [], $connectionName = 'default')
7065
} else {
7166
static::$runMode[$connectionName] = self::PROD_MODE;
7267
}
73-
if (array_key_exists(EQM::RESULT_SET_CLASS, $config)) {
74-
static::$resultSetClass[$connectionName] = $config[self::RESULT_SET_CLASS];
75-
} else {
76-
static::$resultSetClass[$connectionName] = '\\troba\\EQM\\ResultSet';
77-
}
7868
} catch (\PDOException $e) {
7969
throw new EQMException($e->getMessage(), $e->getCode(), $e);
8070
}
@@ -164,7 +154,7 @@ public static function nativeExecute($sql, $params = [], $lastInsertedId = false
164154
* @param array $params optional if the statement needs parameters
165155
*
166156
* @throws EQMException
167-
* @return ResultSetInterface set of objects - the result class is defined in $objectOrClass
157+
* @return AbstractResultSet set of objects - the result class is defined in $objectOrClass
168158
*/
169159
public static function nativeQuery($objectOrClass, $sql = null, $params = [])
170160
{
@@ -174,7 +164,7 @@ public static function nativeQuery($objectOrClass, $sql = null, $params = [])
174164
try {
175165
$stmt = static::$db[static::$activeConnection]->prepare($sql);
176166
$stmt->execute($params);
177-
return new static::$resultSetClass[static::$activeConnection]($stmt, $objectOrClass);
167+
return new ResultSet($stmt, $objectOrClass);
178168
} catch (\PDOException $e) {
179169
throw new EQMException($e->getMessage(), $e->getCode(), $e);
180170
}

lib/troba/EQM/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function limit($limit, $offset = null)
211211
/**
212212
* @param int|null $limit optional the number of records to be returned
213213
* @param int|null $offset optional the starting point
214-
* @return ResultSetInterface
214+
* @return AbstractResultSet
215215
*/
216216
public function result($limit = null, $offset = null)
217217
{

lib/troba/EQM/ResultSet.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace troba\EQM;
44

5-
class ResultSet implements ResultSetInterface
5+
class ResultSet extends AbstractResultSet
66
{
77
/**
88
* @var \PDOStatement
@@ -24,11 +24,6 @@ class ResultSet implements ResultSetInterface
2424
*/
2525
protected $count = null;
2626

27-
/**
28-
* @var string
29-
*/
30-
protected $classname = '\StdClass';
31-
3227
/**
3328
* @param \PDOStatement $pdoStatement
3429
* @param string $classname optional
@@ -61,7 +56,7 @@ public function valid()
6156
}
6257

6358
/**
64-
*
59+
* @return void
6560
*/
6661
public function next()
6762
{
@@ -72,7 +67,7 @@ public function next()
7267
/**
7368
* @return object
7469
*/
75-
public function current()
70+
protected function getCurrent()
7671
{
7772
return $this->current;
7873
}
@@ -94,12 +89,12 @@ public function count()
9489
}
9590

9691
/**
97-
* @return array
92+
* @return AbstractResultSet
9893
*/
9994
public function all()
10095
{
10196
$this->cursor = $this->count;
102-
return $this->pdoStatement->fetchAll();
97+
return new ResultSetArray($this->pdoStatement->fetchAll(), $this->classname);
10398
}
10499

105100
/**

lib/troba/EQM/ResultSetArray.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace troba\EQM;
4+
5+
class ResultSetArray extends AbstractResultSet implements \ArrayAccess
6+
{
7+
/**
8+
* @var object[]
9+
*/
10+
protected $entities = [];
11+
12+
/**
13+
* @var int
14+
*/
15+
protected $cursor = 0;
16+
17+
/**
18+
* @param array $result
19+
* @param string $classname optional
20+
*/
21+
public function __construct($result, $classname = '\StdClass')
22+
{
23+
$this->entities = $result;
24+
$this->classname = $classname;
25+
}
26+
27+
/**
28+
* @return void
29+
*/
30+
public function rewind()
31+
{
32+
$this->cursor = 0;
33+
}
34+
35+
/**
36+
* @return bool
37+
*/
38+
public function valid()
39+
{
40+
return $this->cursor < count($this->entities);
41+
}
42+
43+
/**
44+
* @return void
45+
*/
46+
public function next()
47+
{
48+
$this->cursor++;
49+
}
50+
51+
/**
52+
* @return object
53+
*/
54+
protected function getCurrent()
55+
{
56+
return $this->entities[$this->cursor];
57+
}
58+
59+
/**
60+
* @return int|mixed
61+
*/
62+
public function key()
63+
{
64+
return $this->cursor;
65+
}
66+
67+
/**
68+
* @return int
69+
*/
70+
public function count()
71+
{
72+
return count($this->entities);
73+
}
74+
75+
/**
76+
* @return ResultSetArray
77+
*/
78+
public function all()
79+
{
80+
return $this;
81+
}
82+
83+
/**
84+
* @return null|object
85+
*/
86+
public function one()
87+
{
88+
$this->next();
89+
return ($this->valid()) ? $this->entities[$this->cursor] : null;
90+
}
91+
92+
/**
93+
* @param mixed $offset
94+
* @return bool
95+
*/
96+
public function offsetExists($offset)
97+
{
98+
return isset($this->entities[$offset]);
99+
}
100+
101+
/**
102+
* @param mixed $offset
103+
* @return null|object
104+
*/
105+
public function offsetGet($offset)
106+
{
107+
return isset($this->entities[$offset]) ? $this->entities[$offset] : null;
108+
}
109+
110+
/**
111+
* @param mixed $offset
112+
* @param object $value
113+
*/
114+
public function offsetSet($offset, $value)
115+
{
116+
if (is_null($offset)) {
117+
$this->entities[] = $value;
118+
} else {
119+
$this->entities[$offset] = $value;
120+
}
121+
}
122+
123+
/**
124+
* @param mixed $offset
125+
*/
126+
public function offsetUnset($offset)
127+
{
128+
unset($this->entities[$offset]);
129+
}
130+
}

lib/troba/EQM/ResultSetInterface.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)