|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Storage\Database\PDO; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Storage\Database\ASQL; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
use PDOStatement; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class APDO |
|
14
|
|
|
* @package kalanis\kw_mapper\Storage\Database\PDO |
|
15
|
|
|
* PHP data object abstraction |
|
16
|
|
|
* Uses placeholders, not question marks |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class APDO extends ASQL |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var PDO|null */ |
|
21
|
|
|
protected $connection = null; |
|
22
|
|
|
protected ?PDOStatement $lastStatement; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $query |
|
26
|
|
|
* @param array<string, mixed> $params |
|
27
|
|
|
* @param int $fetchType |
|
28
|
|
|
* @throws MapperException |
|
29
|
|
|
* @return array<string|int, array<int, string|int|float>> |
|
30
|
|
|
*/ |
|
31
|
20 |
|
public function query(string $query, array $params, int $fetchType = PDO::FETCH_ASSOC): array |
|
32
|
|
|
{ |
|
33
|
20 |
|
if (empty($query)) { |
|
34
|
3 |
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
20 |
|
$this->connect(); |
|
38
|
|
|
|
|
39
|
|
|
//print_r(['qu', str_split($query, 80), $params]); |
|
40
|
20 |
|
$statement = $this->connection->prepare($query); |
|
41
|
20 |
|
$statement->execute($params); |
|
42
|
|
|
|
|
43
|
20 |
|
$this->lastStatement = $statement; |
|
44
|
|
|
|
|
45
|
20 |
|
$result = $statement->fetchAll($fetchType); |
|
46
|
20 |
|
return (false !== $result) ? $result : []; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $query |
|
51
|
|
|
* @param array<string, mixed> $params |
|
52
|
|
|
* @throws MapperException |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
45 |
|
public function exec(string $query, array $params): bool |
|
56
|
|
|
{ |
|
57
|
45 |
|
if (empty($query)) { |
|
58
|
3 |
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
45 |
|
$this->connect(); |
|
62
|
|
|
|
|
63
|
45 |
|
$statement = $this->connection->prepare($query); |
|
64
|
45 |
|
$result = $statement->execute($params); |
|
65
|
|
|
|
|
66
|
45 |
|
$this->lastStatement = $statement; |
|
67
|
|
|
|
|
68
|
45 |
|
return $result && $statement->closeCursor(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
45 |
|
public function connect(): void |
|
72
|
|
|
{ |
|
73
|
45 |
|
if (!$this->isConnected()) { |
|
74
|
3 |
|
$this->connection = $this->connectToServer(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
abstract protected function connectToServer(): PDO; |
|
79
|
|
|
|
|
80
|
2 |
|
public function lastInsertId(): ?string |
|
81
|
|
|
{ |
|
82
|
|
|
/** @scrutinizer ignore-call */ |
|
83
|
2 |
|
$id = $this->connection->lastInsertId(); |
|
84
|
2 |
|
return (false === $id) ? null : strval($id); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
14 |
|
public function rowCount(): ?int |
|
88
|
|
|
{ |
|
89
|
14 |
|
return $this->lastStatement ? $this->lastStatement->rowCount() : null ; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
public function beginTransaction(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
// @codeCoverageIgnoreStart |
|
95
|
|
|
if (!$this->isConnected()) { |
|
96
|
|
|
$this->connection = $this->connectToServer(); |
|
97
|
|
|
} |
|
98
|
|
|
// @codeCoverageIgnoreEnd |
|
99
|
|
|
|
|
100
|
3 |
|
return (bool) $this->connection->beginTransaction(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
public function commit(): bool |
|
104
|
|
|
{ |
|
105
|
3 |
|
return (bool) $this->connection->commit(); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
public function rollBack(): bool |
|
109
|
|
|
{ |
|
110
|
3 |
|
return (bool) $this->connection->rollBack(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.