Passed
Push — master ( c78efc...84ef81 )
by Darío
01:42
created

TableGateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db\TableGateway;
12
13
use Drone\Db\Entity;
14
use Drone\Db\SQLFunction;
15
use Drone\Exception;
16
17
/**
18
 * TableGateway class
19
 *
20
 * This class is a query builder for CRUD (create, read, update, delete)
21
 */
22
class TableGateway extends AbstractTableGateway implements TableGatewayInterface
23
{
24
    /**
25
     * Select statement
26
     *
27
     * @param array $where
28
     *
29
     * @return array With all results
30
     */
31
    public function select(Array $where = [])
32
    {
33
        $bind_values = [];
34
35
        $driver = $this->getDb()->getDriverName();
36
37
        if (count($where))
38
        {
39
            $parsed_where = [];
40
41
            $k = 0;
42
43
            foreach ($where as $key => $value)
44
            {
45
                $k++;
46
47
                if (is_null($value))
48
                    $parsed_where[] = "$key IS NULL";
49
                elseif ($value instanceof SQLFunction)
50
                    $parsed_where[] = "$key = " . $value->getStatement();
51
                elseif (is_array($value))
52
                {
53
                    $parsed_in = [];
54
55
                    foreach ($value as $in_value)
56
                    {
57
                        switch ($driver)
58
                        {
59
                            case 'Oci8':
60
                                $parsed_in[] = ":$k";
61
                                $bind_values[":$k"] = $in_value;
62
                                break;
63
64
                            case 'Mysqli' || 'Sqlsrv':
65
                                $parsed_in[] = "?";
66
                                $bind_values[] = $in_value;
67
                                break;
68
                        }
69
70
                        $k++;
71
                    }
72
73
                    $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")";
74
                }
75
                else
76
                {
77
                    switch ($driver)
78
                    {
79
                        case 'Oci8':
80
                            $parsed_where[] = "$key = :$k";
81
                            $bind_values[":$k"] = $value;
82
                            break;
83
84
                        case 'Mysqli' || 'Sqlsrv':
85
                            $parsed_where[] = "$key = ?";
86
                            $bind_values[] = $value;
87
                            break;
88
                    }
89
                }
90
            }
91
92
            $where = "WHERE \r\n\t" . implode(" AND\r\n\t", $parsed_where);
93
        }
94
        else
95
            $where = "";
96
97
        $table = $this->entity->getTableName();
98
99
        $sql = "SELECT * \r\nFROM {$table}\r\n$where";
100
101
        if (count($bind_values))
102
            $this->getDb()->execute($sql, $bind_values);
103
        else
104
            $this->getDb()->execute($sql);
105
106
        return $this->getDb()->getArrayResult();
107
    }
108
109
    /**
110
     * Insert statement
111
     *
112
     * @param array $data
113
     *
114
     * @throws RuntimeException from internal execute()
115
     * @throws LogicException
116
     *
117
     * @return resource|object
118
     */
119
    public function insert(Array $data)
120
    {
121
        if (!count($data))
122
            throw new \LogicException("Missing values for INSERT statement!");
123
124
        $bind_values = [];
125
126
        $driver = $this->getDb()->getDriverName();
127
128
        $k = 0;
129
130
        $null_keys = [];
131
132
        foreach ($data as $key => $value)
133
        {
134
            $k++;
135
136
            # insert NULL values cause problems when BEFORE INSERT triggers are
137
            # defined to assigns values over fields. For SQLServer is better not
138
            # pass NULL values
139
            if ($driver == 'Sqlsrv' && is_null($value))
140
            {
141
                $null_keys[] = $key;
142
                continue;
143
            }
144
145
            if (is_null($value))
146
                $value = "NULL";
147
            elseif ($value instanceof SQLFunction)
148
                $value = $value->getStatement();
149
            else {
150
151
                switch ($driver)
152
                {
153
                    case 'Oci8':
154
                        $bind_values[":$k"] = $value;
155
                        $value = ":$k";
156
                        break;
157
158
                    case 'Mysqli' || 'Sqlsrv':
159
                        $bind_values[] = $value;
160
                        $value = "?";
161
                        break;
162
                }
163
            }
164
165
            $data[$key] = $value;
166
        }
167
168
        foreach ($null_keys as $key)
169
        {
170
            unset($data[$key]);
171
        }
172
173
        $cols = implode(",\r\n\t", array_keys($data));
174
        $vals = implode(",\r\n\t", array_values($data));
175
176
        $table = $this->entity->getTableName();
177
178
        $sql = "INSERT INTO {$table} \r\n(\r\n\t$cols\r\n) \r\nVALUES \r\n(\r\n\t$vals\r\n)";
179
180
        return $this->getDb()->execute($sql, $bind_values);
181
    }
182
183
    /**
184
     * Update statement
185
     *
186
     * @param array $set
187
     * @param array $where
188
     *
189
     * @throws RuntimeException from internal execute()
190
     * @throws LogicException
191
     * @throws SecurityException
192
     *
193
     * @return resource|object
194
     */
195
    public function update(Array $set, Array $where)
196
    {
197
        $parsed_set = [];
198
199
        if (!count($set))
200
            throw new \LogicException("You cannot update rows without SET clause");
201
202
        if (!count($where))
203
            throw new SecurityException("You cannot update rows without WHERE clause!");
0 ignored issues
show
Bug introduced by
The type Drone\Db\TableGateway\SecurityException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
204
205
        $bind_values = [];
206
207
        $driver = $this->getDb()->getDriverName();
208
209
        $k = 0;
210
211
        foreach ($set as $key => $value)
212
        {
213
            $k++;
214
215
            if (is_null($value))
216
                $parsed_set[] = "$key = NULL";
217
            elseif ($value instanceof SQLFunction)
218
                $parsed_set[] = "$key = " . $value->getStatement();
219
            elseif (is_array($value))
220
            {
221
                $parsed_in = [];
222
223
                foreach ($value as $in_value)
224
                {
225
                    switch ($driver)
226
                    {
227
                        case 'Oci8':
228
229
                            # [POSSIBLE BUG] - To Future revision (What about non-string values ?)
230
                            if (is_string($in_value))
231
                                $parsed_in[] = ":$k";
232
233
                            $bind_values[":$k"] = $in_value;
234
                            break;
235
236
                        case 'Mysqli' || 'Sqlsrv':
237
                            $parsed_in[] = "?";
238
                            $bind_values[] = $in_value;
239
                            break;
240
                    }
241
242
                    $k++;
243
                }
244
245
                $parsed_set[] = "$key IN (" . implode(", ", $parsed_in) . ")";
246
            }
247
            else
248
            {
249
                switch ($driver)
250
                {
251
                    case 'Oci8':
252
                        $parsed_set[] = "$key = :$k";
253
                        $bind_values[":$k"] = $value;
254
                        break;
255
256
                    case 'Mysqli' || 'Sqlsrv':
257
                        $parsed_set[] = "$key = ?";
258
                        $bind_values[] = $value;
259
                        break;
260
                }
261
            }
262
        }
263
264
        $parsed_set = implode(",\r\n\t", $parsed_set);
265
266
        $parsed_where = [];
267
268
        foreach ($where as $key => $value)
269
        {
270
            $k++;
271
272
            if (is_null($value))
273
                $parsed_where[] = "$key IS NULL";
274
            elseif ($value instanceof SQLFunction)
275
                $parsed_where[] = "$key = " . $value->getStatement();
276
            elseif (is_array($value))
277
            {
278
                $parsed_in = [];
279
280
                foreach ($value as $in_value)
281
                {
282
                    switch ($driver)
283
                    {
284
                        case 'Oci8':
285
                            $parsed_in[] = ":$k";
286
                            $bind_values[":$k"] = $in_value;
287
                            break;
288
289
                        case 'Mysqli' || 'Sqlsrv':
290
                            $parsed_in[] = "?";
291
                            $bind_values[] = $in_value;
292
                            break;
293
                    }
294
295
                    $k++;
296
                }
297
298
                $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")";
299
            }
300
            else
301
            {
302
                switch ($driver)
303
                {
304
                    case 'Oci8':
305
                        $parsed_where[] = "$key = :$k";
306
                        $bind_values[":$k"] = $value;
307
                        break;
308
309
                    case 'Mysqli' || 'Sqlsrv':
310
                        $parsed_where[] = "$key = ?";
311
                        $bind_values[] = $value;
312
                        break;
313
                }
314
            }
315
        }
316
317
        $parsed_where = implode(" AND\r\n\t", $parsed_where);
318
319
        $table = $this->entity->getTableName();
320
321
        $sql = "UPDATE {$table} \r\nSET \r\n\t$parsed_set \r\nWHERE \r\n\t$parsed_where";
322
323
        return $this->getDb()->execute($sql, $bind_values);
324
    }
325
326
    /**
327
     * Delete statement
328
     *
329
     * @param array $where
330
     *
331
     * @throws RuntimeException from internal execute()
332
     * @throws SecurityException
333
     *
334
     * @return resource|object
335
     */
336
    public function delete(Array $where)
337
    {
338
        if (count($where))
339
        {
340
            $parsed_where = [];
341
342
            $bind_values = [];
343
344
            $driver = $this->getDb()->getDriverName();
345
346
            $k = 0;
347
348
            foreach ($where as $key => $value)
349
            {
350
                $k++;
351
352
                if (is_null($value))
353
                    $parsed_where[] = "$key IS NULL";
354
                elseif ($value instanceof SQLFunction)
355
                    $parsed_where[] = "$key = " . $value->getStatement();
356
                elseif (is_array($value))
357
                {
358
                    $parsed_in = [];
359
360
                    foreach ($value as $in_value)
361
                    {
362
                        switch ($driver)
363
                        {
364
                            case 'Oci8':
365
                                $parsed_in[] = ":$k";
366
                                $bind_values[":$k"] = $in_value;
367
                                break;
368
369
                            case 'Mysqli' || 'Sqlsrv':
370
                                $parsed_in[] = "?";
371
                                $bind_values[] = $in_value;
372
                                break;
373
                        }
374
375
                        $k++;
376
                    }
377
378
                    $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")";
379
                }
380
                else
381
                {
382
                    switch ($driver)
383
                    {
384
                        case 'Oci8':
385
                            $parsed_where[] = "$key = :$k";
386
                            $bind_values[":$k"] = $value;
387
                            break;
388
389
                        case 'Mysqli' || 'Sqlsrv':
390
                            $parsed_where[] = "$key = ?";
391
                            $bind_values[] = $value;
392
                            break;
393
                    }
394
                }
395
            }
396
397
            $where = "\r\nWHERE \r\n\t" . implode(" AND\r\n\t", $parsed_where);
398
        }
399
        else
400
            throw new SecurityException("You cannot delete rows without WHERE clause!. Use TRUNCATE statement instead.");
401
402
        $table = $this->entity->getTableName();
403
404
        $sql = "DELETE FROM {$table} $where";
405
406
        return $this->getDb()->execute($sql, $bind_values);
407
    }
408
}