This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @author Rémy M. Böhler <[email protected]> |
||
4 | */ |
||
5 | namespace Rorm; |
||
6 | |||
7 | use PDO; |
||
8 | |||
9 | /** |
||
10 | * Class Query |
||
11 | */ |
||
12 | class Query |
||
13 | { |
||
14 | /** @var PDO */ |
||
15 | protected $dbh; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $class; |
||
19 | |||
20 | /** @var bool */ |
||
21 | protected $classIsOrmModel; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $query; |
||
25 | |||
26 | /** @var array */ |
||
27 | protected $params; |
||
28 | |||
29 | /** @var \PDOStatement */ |
||
30 | protected $statement; |
||
31 | |||
32 | /** |
||
33 | * @param string $class |
||
34 | * @param PDO|null $dbh if null the default database connection is used |
||
35 | */ |
||
36 | 31 | public function __construct($class = 'stdClass', PDO $dbh = null) |
|
37 | { |
||
38 | 31 | $this->class = $class; |
|
39 | 31 | $this->classIsOrmModel = is_subclass_of($this->class, '\\Rorm\\Model'); |
|
40 | 31 | $this->dbh = $dbh ? $dbh : Rorm::getDatabase(); |
|
41 | 31 | } |
|
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | 3 | public function getClass() |
|
47 | { |
||
48 | 3 | return $this->class; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param string $query |
||
53 | */ |
||
54 | 4 | public function setQuery($query) |
|
55 | { |
||
56 | 4 | $this->query = $query; |
|
57 | 4 | } |
|
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | 9 | public function getQuery() |
|
63 | { |
||
64 | 9 | return $this->query; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param array $params |
||
69 | */ |
||
70 | 2 | public function setParams(array $params) |
|
71 | { |
||
72 | 2 | $this->params = $params; |
|
73 | 2 | } |
|
74 | |||
75 | /** |
||
76 | * @return array |
||
77 | */ |
||
78 | 4 | public function getParams() |
|
79 | { |
||
80 | 4 | return $this->params; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | * |
||
86 | * @todo probably we can unset query an params to free up memory |
||
0 ignored issues
–
show
|
|||
87 | */ |
||
88 | 12 | protected function execute() |
|
89 | { |
||
90 | 12 | $this->statement = $this->dbh->prepare($this->query); |
|
91 | // set fetchMode to assoc, it is easier to copy data from an array than an object |
||
92 | 12 | $this->statement->setFetchMode(PDO::FETCH_ASSOC); |
|
93 | 12 | return $this->statement->execute($this->params); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return object|null |
||
98 | */ |
||
99 | 8 | public function fetch() |
|
100 | { |
||
101 | 8 | $data = $this->statement->fetch(); |
|
102 | 8 | if ($data !== false) { |
|
103 | 7 | return $this->instanceFromObject($data); |
|
104 | } |
||
105 | 3 | return null; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param array $data |
||
110 | * @return object |
||
111 | */ |
||
112 | 11 | public function instanceFromObject(array $data) |
|
113 | { |
||
114 | 11 | $instance = new $this->class; |
|
115 | 11 | if ($this->classIsOrmModel) { |
|
116 | /** @var \Rorm\Model $instance */ |
||
117 | 10 | $instance->setData($data); |
|
118 | 10 | } else { |
|
119 | 1 | foreach ($data as $key => $value) { |
|
120 | 1 | $instance->$key = $value; |
|
121 | 1 | } |
|
122 | } |
||
123 | |||
124 | 11 | return $instance; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return string|null |
||
129 | */ |
||
130 | 3 | public function findColumn() |
|
131 | { |
||
132 | 3 | if ($this->execute()) { |
|
133 | 3 | return $this->statement->fetchColumn(); |
|
134 | } |
||
135 | return null; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Return one object |
||
140 | * |
||
141 | * @return object|null |
||
142 | */ |
||
143 | 9 | public function findOne() |
|
144 | { |
||
145 | // DO NOT use rowCount to check if something was found because not all drivers support it |
||
146 | 9 | if ($this->execute()) { |
|
147 | 8 | return $this->fetch(); |
|
148 | } |
||
149 | return null; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Return a iterator to iterate over which returns one object at a time |
||
154 | * the objects are lazy loaded and not kept on memory |
||
155 | * |
||
156 | * because the results are not buffered you can only iterate once over it! |
||
157 | * If you need to iterate multiple times over the result you should use the findAll method |
||
158 | * |
||
159 | * Note for PHP 5.5 |
||
160 | * yield could be used |
||
161 | * |
||
162 | * @return QueryIterator |
||
163 | */ |
||
164 | 5 | public function findMany() |
|
165 | { |
||
166 | 5 | $this->execute(); |
|
167 | 5 | return new QueryIterator($this->statement, $this); |
|
168 | // PHP 5.5 yield version for future use |
||
169 | /*while ($object = $this->statement->fetchObject()) { |
||
170 | yield $this->instanceFromObject($object); |
||
171 | }*/ |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return an array with all objects, this can lead to heavy memory consumption |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 3 | public function findAll() |
|
180 | { |
||
181 | 3 | $result = array(); |
|
182 | |||
183 | 3 | foreach ($this->findMany() as $object) { |
|
184 | 3 | $result[] = $object; |
|
185 | 3 | } |
|
186 | |||
187 | 3 | return $result; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * This operation is very expensive. |
||
192 | * |
||
193 | * PDOStatement::rowCount does not work on all drivers! |
||
194 | * |
||
195 | * @return int |
||
196 | */ |
||
197 | 1 | public function count() |
|
198 | { |
||
199 | 1 | return count($this->findAll()); |
|
200 | } |
||
201 | } |
||
202 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.