Skip to content

Commit 9e89cca

Browse files
committed
Add the named query executor
1 parent 5eb28f4 commit 9e89cca

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/LightQL/Entities/Query.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* LightQL - The lightweight PHP ORM
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package LightQL
26+
* @author Axel Nana <ax.lnana@outlook.com>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/LightQL/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://lightql.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\LightQL\Entities;
34+
35+
use ElementaryFramework\LightQL\Exceptions\QueryException;
36+
37+
/**
38+
* Query
39+
*
40+
* Manage, run and get results from named queries.
41+
*
42+
* @category Sessions
43+
* @package LightQL
44+
* @author Nana Axel <ax.lnana@outlook.com>
45+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Entities/Query
46+
*/
47+
class Query
48+
{
49+
/**
50+
* @var EntityManager
51+
*/
52+
private $_entityManager;
53+
54+
/**
55+
* @var \ReflectionClass
56+
*/
57+
private $_entityReflection;
58+
59+
/**
60+
* @var string
61+
*/
62+
private $_namedQuery;
63+
64+
/**
65+
* @var array
66+
*/
67+
private $_parameters = array();
68+
69+
/**
70+
* @var \PDOStatement
71+
*/
72+
private $_query = null;
73+
74+
/**
75+
* Query constructor.
76+
*
77+
* @param EntityManager $manager
78+
*/
79+
public function __construct(EntityManager $manager)
80+
{
81+
$this->_entityManager = $manager;
82+
}
83+
84+
public function setEntity(\ReflectionClass $entity)
85+
{
86+
$this->_entityReflection = $entity;
87+
}
88+
89+
public function setQuery(string $query)
90+
{
91+
$this->_namedQuery = $query;
92+
}
93+
94+
public function setParam(string $name, $value)
95+
{
96+
$this->_parameters[$name] = $value;
97+
}
98+
99+
public function run(): bool
100+
{
101+
try {
102+
$this->_query = $this->_entityManager->getLightQL()->prepare($this->_namedQuery);
103+
104+
foreach ($this->_parameters as $name => $value) {
105+
$this->_query->bindValue($name, $value);
106+
}
107+
108+
return $this->_query->execute();
109+
} catch (\Exception $e) {
110+
throw new QueryException($e->getMessage());
111+
}
112+
}
113+
114+
public function getResults(): array
115+
{
116+
if ($this->_query === null) {
117+
throw new QueryException("Cannot get results, have you ran the query?");
118+
}
119+
120+
$results = array_map(function ($item) {
121+
return $this->_entityReflection->newInstance($item);
122+
}, $this->_query->fetchAll());
123+
124+
return $results;
125+
}
126+
127+
public function getFirstResult(): IEntity
128+
{
129+
$results = $this->getResults();
130+
return $results[0];
131+
}
132+
}

0 commit comments

Comments
 (0)