|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\PdoCrud; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class PdoCrud |
|
6
|
|
|
* |
|
7
|
|
|
* @package Mezon |
|
8
|
|
|
* @subpackage PdoCrud |
|
9
|
|
|
* @author Dodonov A.A. |
|
10
|
|
|
* @version v.1.0 (2019/08/16) |
|
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class provides simple CRUD operations |
|
16
|
|
|
*/ |
|
17
|
|
|
trait PdoCrudStatement |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* PDO statement |
|
22
|
|
|
* |
|
23
|
|
|
* @var \PDOStatement |
|
24
|
|
|
*/ |
|
25
|
|
|
private $pdoStatement = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Method sets safe query |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $query |
|
31
|
|
|
* safe query |
|
32
|
|
|
* @codeCoverageIgnore |
|
33
|
|
|
*/ |
|
34
|
|
|
public function prepare(string $query): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->pdoStatement = $this->pdo->prepare($query, [ |
|
37
|
|
|
\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
if ($this->pdoStatement === false) { |
|
41
|
|
|
$errorInfo = $this->pdo->errorInfo(); |
|
42
|
|
|
throw (new \Exception('Query "' . $query . '" was not prepared. ' . $errorInfo[2], - 1)); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Method binds parameters to query |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $parameter |
|
50
|
|
|
* name of the parameter |
|
51
|
|
|
* @param mixed $variable |
|
52
|
|
|
* value |
|
53
|
|
|
* @param int $type |
|
54
|
|
|
* parameter type |
|
55
|
|
|
* @codeCoverageIgnore |
|
56
|
|
|
*/ |
|
57
|
|
|
public function bindParameter(string $parameter, $variable, int $type = \PDO::PARAM_STR): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->pdoStatement->bindParam($parameter, $variable, $type); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Method executes SQL query |
|
64
|
|
|
* |
|
65
|
|
|
* @param ?array $data |
|
66
|
|
|
* query data |
|
67
|
|
|
* @codeCoverageIgnore |
|
68
|
|
|
*/ |
|
69
|
|
|
public function execute(?array $data = null): void |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->pdoStatement->execute($data) === false) { |
|
72
|
|
|
$info = $this->pdoStatement->errorInfo(); |
|
73
|
|
|
|
|
74
|
|
|
throw (new \Exception($info[2], - 1)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Method executes select query and fetches results |
|
80
|
|
|
* |
|
81
|
|
|
* @param ?array $data |
|
82
|
|
|
* query data |
|
83
|
|
|
* @return array query result as an array of objects |
|
84
|
|
|
* @codeCoverageIgnore |
|
85
|
|
|
*/ |
|
86
|
|
|
public function executeSelect(?array $data = null): array |
|
87
|
|
|
{ |
|
88
|
|
|
$this->execute($data); |
|
89
|
|
|
|
|
90
|
|
|
return $this->pdoStatement->fetchAll(\PDO::FETCH_OBJ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Method executes select query and fetches results |
|
95
|
|
|
* |
|
96
|
|
|
* @param ?array $data |
|
97
|
|
|
* query data |
|
98
|
|
|
* @return array query result as an array of objects |
|
99
|
|
|
* @codeCoverageIgnore |
|
100
|
|
|
* @deprecated Deprecated since 2020-11-21, use executeSelect |
|
101
|
|
|
*/ |
|
102
|
|
|
public function execSelect(?array $data = null): array |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->executeSelect($data); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Method returns count of affected rows |
|
109
|
|
|
* |
|
110
|
|
|
* @return int count of affected rows |
|
111
|
|
|
* @codeCoverageIgnore |
|
112
|
|
|
*/ |
|
113
|
|
|
public function rowCount(): int |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->pdoStatement->rowCount(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|