|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2017, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use PDO; |
|
15
|
|
|
use PDOStatement; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Extended PDO |
|
20
|
|
|
*/ |
|
21
|
|
|
class ExtendedPDO extends PDO |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool Status for last operation. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $lastOperationStatus; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Executes an SQL statement with parameters, |
|
30
|
|
|
* returning a result set as a PDOStatement object |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $query SQL statement |
|
33
|
|
|
* @param array $param Parameter as array as PDOStatement::bindParam |
|
34
|
|
|
* |
|
35
|
|
|
* @return PDOStatement |
|
36
|
|
|
*/ |
|
37
|
15 |
|
public function queryWithParam(string $query, array $param) : PDOStatement |
|
38
|
|
|
{ |
|
39
|
15 |
|
$statment = $this->prepare($query); |
|
40
|
|
|
|
|
41
|
15 |
|
foreach ($param as $value) { |
|
42
|
14 |
|
$this->checkValue($value); |
|
43
|
|
|
|
|
44
|
|
|
//reassign as reference |
|
45
|
|
|
//because bindParam need it as reference |
|
46
|
12 |
|
$ref = $value; |
|
47
|
12 |
|
$ref[1] = &$value[1]; |
|
48
|
|
|
|
|
49
|
12 |
|
call_user_func_array([$statment, "bindParam"], $ref); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
13 |
|
$this->lastOperationStatus = $statment->execute(); |
|
53
|
|
|
|
|
54
|
12 |
|
return $statment; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return the last opration status. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
9 |
|
public function getLastOperationStatus() : bool |
|
63
|
|
|
{ |
|
64
|
9 |
|
return $this->lastOperationStatus; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check values passed to queryWithParam. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $value |
|
71
|
|
|
* @throws InvalidArgumentException |
|
72
|
|
|
*/ |
|
73
|
14 |
|
private function checkValue(array &$value) |
|
74
|
|
|
{ |
|
75
|
14 |
|
if (count($value) < 2) { |
|
76
|
1 |
|
throw new InvalidArgumentException(__METHOD__.': Parameters array must contain at least two elements with this form: [\':name\', \'value\']'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
13 |
|
if (strpos($value[0], ':') !== 0) { |
|
80
|
1 |
|
throw new InvalidArgumentException(__METHOD__.': Parameter name will be in the form :name'); |
|
81
|
|
|
} |
|
82
|
12 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|