These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace ByJG\AnyDataset\Database; |
||
4 | |||
5 | use ByJG\AnyDataset\AnyDatasetContext; |
||
6 | use ByJG\AnyDataset\LogHandler; |
||
7 | use ByJG\AnyDataset\Repository\DBDataset; |
||
8 | use ByJG\AnyDataset\Repository\IteratorInterface; |
||
9 | |||
10 | class BaseDBAccess |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var DBDataset |
||
15 | */ |
||
16 | private $_db = null; |
||
17 | |||
18 | /** |
||
19 | * Wrapper for SQLHelper |
||
20 | * |
||
21 | * @var SQLHelper |
||
22 | */ |
||
23 | protected $_sqlhelper = null; |
||
24 | |||
25 | /** |
||
26 | * Base Class Constructor. Don't must be override. |
||
27 | * @param DBDataset $db |
||
28 | */ |
||
29 | public function __construct(DBDataset $db) |
||
30 | { |
||
31 | $this->_db = $db; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Create a instance of DBDataset to connect database |
||
36 | * @return DBDataset |
||
37 | */ |
||
38 | protected function getDBDataset() |
||
39 | { |
||
40 | return $this->_db; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Execute a SQL and dont wait for a response. |
||
45 | * @param string $sql |
||
46 | * @param string $param |
||
47 | * @param bool $getId |
||
48 | * @return int|null |
||
49 | */ |
||
50 | protected function executeSQL($sql, $param = null, $getId = false) |
||
51 | { |
||
52 | $dbfunction = $this->getDbFunctions(); |
||
53 | |||
54 | $debug = $this->getDebug(); |
||
55 | $start = 0; |
||
56 | if ($debug) { |
||
57 | $log = LogHandler::getInstance(); |
||
58 | $log->debug("Class name: " . get_class($this)); |
||
59 | $log->debug("SQL: " . $sql); |
||
60 | if (!is_null($param)) { |
||
61 | $s = ""; |
||
62 | foreach ($param as $key => $value) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
63 | if ($s != "") { |
||
64 | $s .= ", "; |
||
65 | } |
||
66 | $s .= "[$key]=$value"; |
||
67 | } |
||
68 | $log->debug("Params: $s"); |
||
69 | } |
||
70 | $start = microtime(true); |
||
71 | } |
||
72 | |||
73 | if ($getId) { |
||
74 | $id = $dbfunction->executeAndGetInsertedId($this->getDBDataset(), $sql, $param); |
||
0 ignored issues
–
show
$param is of type string|null , but the function expects a array .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
75 | } else { |
||
76 | $id = null; |
||
77 | $this->getDBDataset()->execSQL($sql, $param); |
||
0 ignored issues
–
show
It seems like
$param defined by parameter $param on line 50 can also be of type string ; however, ByJG\AnyDataset\Repository\DBDataset::execSQL() does only seem to accept array|null , maybe add an additional type check?
This check looks at variables that have been passed in as parameters and are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. ![]() |
|||
78 | } |
||
79 | |||
80 | if ($debug) { |
||
81 | $end = microtime(true); |
||
82 | $log->debug("Execution time: " . ($end - $start) . " seconds "); |
||
0 ignored issues
–
show
The variable
$log does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
83 | } |
||
84 | |||
85 | return $id; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Execulte SELECT SQL Query |
||
90 | * |
||
91 | * @param string $sql |
||
92 | * @param array $param |
||
93 | * @param int $ttl |
||
94 | * @return IteratorInterface |
||
95 | */ |
||
96 | View Code Duplication | protected function getIterator($sql, $param = null, $ttl = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
97 | { |
||
98 | $debug = $this->getDebug(); |
||
99 | $start = 0; |
||
100 | if ($debug) { |
||
101 | $log = LogHandler::getInstance(); |
||
102 | $log->debug("Class name: " . get_class($this)); |
||
103 | $log->debug("SQL: " . $sql); |
||
104 | if (!is_null($param)) { |
||
105 | $s = ""; |
||
106 | foreach ($param as $key => $value) { |
||
107 | if ($s != "") { |
||
108 | $s .= ", "; |
||
109 | } |
||
110 | $s .= "[$key]=$value"; |
||
111 | } |
||
112 | $log->debug("Params: $s"); |
||
113 | } |
||
114 | $start = microtime(true); |
||
115 | } |
||
116 | $it = $this->getDBDataset()->getIterator($sql, $param, $ttl); |
||
117 | if ($debug) { |
||
118 | $end = microtime(true); |
||
119 | $log->debug("Execution Time: " . ($end - $start) . " segundos "); |
||
0 ignored issues
–
show
The variable
$log does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
120 | } |
||
121 | return $it; |
||
122 | } |
||
123 | |||
124 | View Code Duplication | protected function getScalar($sql, $param = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
125 | { |
||
126 | $debug = $this->getDebug(); |
||
127 | $start = 0; |
||
128 | if ($debug) { |
||
129 | $log = LogHandler::getInstance(); |
||
130 | $log->debug("Class name: " . get_class($this)); |
||
131 | $log->debug("SQL: " . $sql); |
||
132 | if (!is_null($param)) { |
||
133 | $s = ""; |
||
134 | foreach ($param as $key => $value) { |
||
135 | if ($s != "") { |
||
136 | $s .= ", "; |
||
137 | } |
||
138 | $s .= "[$key]=$value"; |
||
139 | } |
||
140 | $log->debug("Params: $s"); |
||
141 | } |
||
142 | $start = microtime(true); |
||
143 | } |
||
144 | $scalar = $this->getDBDataset()->getScalar($sql, $param); |
||
145 | if ($debug) { |
||
146 | $end = microtime(true); |
||
147 | $log->debug("Execution Time: " . ($end - $start) . " segundos "); |
||
0 ignored issues
–
show
The variable
$log does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
148 | } |
||
149 | return $scalar; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get a SQLHelper object |
||
154 | * |
||
155 | * @return SQLHelper |
||
156 | */ |
||
157 | public function getSQLHelper() |
||
158 | { |
||
159 | if (is_null($this->_sqlhelper)) { |
||
160 | $this->_sqlhelper = new SQLHelper($this->getDBDataset()); |
||
161 | } |
||
162 | |||
163 | return $this->_sqlhelper; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get an Interator from an ID. Ideal for get data from PK |
||
168 | * |
||
169 | * @param string $tablename |
||
170 | * @param string $key |
||
171 | * @param string $value |
||
172 | * @return IteratorInterface |
||
173 | */ |
||
174 | protected function getIteratorbyId($tablename, $key, $value) |
||
175 | { |
||
176 | $sql = "select * from $tablename where $key = [[$key]] "; |
||
177 | $param = array(); |
||
178 | $param[$key] = $value; |
||
179 | return $this->getIterator($sql, $param); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Get an Array from an existing Iterator |
||
184 | * |
||
185 | * @param IteratorInterface $it |
||
186 | * @param string $key |
||
187 | * @param string $value |
||
188 | * @param string $firstElement |
||
189 | * @return array |
||
190 | */ |
||
191 | public static function getArrayFromIterator(IteratorInterface $it, $key, $value, $firstElement = "-- Selecione --") |
||
192 | { |
||
193 | $retArray = array(); |
||
194 | if ($firstElement != "") { |
||
195 | $retArray[""] = $firstElement; |
||
196 | } |
||
197 | while ($it->hasNext()) { |
||
198 | $sr = $it->moveNext(); |
||
199 | $retArray[$sr->getField(strtolower($key))] = $sr->getField(strtolower($value)); |
||
200 | } |
||
201 | return $retArray; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * |
||
206 | * @param IteratorInterface $it |
||
207 | * @param string $name |
||
208 | * @param array $fields |
||
209 | * @param bool $echoToBrowser |
||
210 | * @return string |
||
211 | */ |
||
212 | public static function saveToCSV($it, $name = "data.csv", $fields = null, $echoToBrowser = true) |
||
213 | { |
||
214 | if ($echoToBrowser) { |
||
215 | ob_clean(); |
||
216 | |||
217 | header("Content-Type: text/csv;"); |
||
218 | header("Content-Disposition: attachment; filename=$name"); |
||
219 | } |
||
220 | |||
221 | $first = true; |
||
222 | $line = ""; |
||
223 | foreach ($it as $sr) { |
||
0 ignored issues
–
show
|
|||
224 | if ($first) { |
||
225 | $first = false; |
||
226 | |||
227 | if (is_null($fields)) { |
||
228 | $fields = $sr->getFieldNames(); |
||
229 | } |
||
230 | |||
231 | $line .= '"' . implode('","', $fields) . '"' . "\n"; |
||
232 | } |
||
233 | |||
234 | $raw = array(); |
||
235 | foreach ($fields as $field) { |
||
236 | $raw[] = $sr->getField($field); |
||
237 | } |
||
238 | $line .= '"' . implode('","', array_values($raw)) . '"' . "\n"; |
||
239 | |||
240 | if ($echoToBrowser) { |
||
241 | echo $line; |
||
242 | $line = ""; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | if (!$echoToBrowser) { |
||
247 | return $line; |
||
248 | } |
||
249 | |||
250 | return null; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get a IDbFunctions class containing specific database operations |
||
255 | * @return DBFunctionsInterface |
||
256 | */ |
||
257 | public function getDbFunctions() |
||
258 | { |
||
259 | return $this->getDBDataset()->getDbFunctions(); |
||
260 | } |
||
261 | |||
262 | public function beginTransaction() |
||
263 | { |
||
264 | $this->getDBDataset()->beginTransaction(); |
||
265 | } |
||
266 | |||
267 | public function commitTransaction() |
||
268 | { |
||
269 | $this->getDBDataset()->commitTransaction(); |
||
270 | } |
||
271 | |||
272 | public function rollbackTransaction() |
||
273 | { |
||
274 | $this->getDBDataset()->rollbackTransaction(); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function getDebug() |
||
281 | { |
||
282 | return AnyDatasetContext::getInstance()->getDebug(); |
||
283 | } |
||
284 | } |
||
285 |