1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Storage\Database\Raw; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
7
|
|
|
use kalanis\kw_mapper\Storage\Database\ASQL; |
8
|
|
|
use kalanis\kw_mapper\Storage\Database\Dialects; |
9
|
|
|
use kalanis\kw_mapper\Storage\Database\TBindNames; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MySQLi |
14
|
|
|
* @package kalanis\kw_mapper\Storage\Database\Raw |
15
|
|
|
* Problematic connector to MySQL just for compatibility - USE PDO instead!!! |
16
|
|
|
* @codeCoverageIgnore remote connection |
17
|
|
|
*/ |
18
|
|
|
class MySQLi extends ASQL |
19
|
|
|
{ |
20
|
|
|
use TBindNames; |
21
|
|
|
|
22
|
|
|
protected string $extension = 'mysqli'; |
23
|
|
|
/** @var \mysqli|null */ |
24
|
|
|
protected $connection = null; |
25
|
|
|
protected ?\mysqli_stmt $lastStatement = null; |
26
|
|
|
|
27
|
|
|
public function disconnect(): void |
28
|
|
|
{ |
29
|
|
|
if ($this->isConnected()) { |
30
|
|
|
$this->connection->close(); |
31
|
|
|
} |
32
|
|
|
$this->connection = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function languageDialect(): string |
36
|
|
|
{ |
37
|
|
|
return Dialects\MySQL::class; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function query(string $query, array $params, int $fetchType = MYSQLI_ASSOC): array |
41
|
|
|
{ |
42
|
|
|
if (empty($query)) { |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->connect(); |
47
|
|
|
|
48
|
|
|
$statement = $this->connection->stmt_init(); |
49
|
|
|
list($updQuery, $binds, $types) = $this->bindFromNamedToQuestions($query, $params); |
50
|
|
|
$statement->prepare(strval($updQuery)); |
51
|
|
|
if (!empty($binds)) { |
52
|
|
|
$statement->bind_param(implode('', $types), ...$binds); // @phpstan-ignore-line |
53
|
|
|
} |
54
|
|
|
if (!$statement->execute()) { |
55
|
|
|
throw new MapperException('mysqli execute error: ' . $statement->error); |
56
|
|
|
} |
57
|
|
|
$result = $statement->get_result(); |
58
|
|
|
|
59
|
|
|
$this->lastStatement = $statement; |
60
|
|
|
|
61
|
|
|
return $result ? $result->fetch_all($fetchType) : []; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function exec(string $query, array $params): bool |
65
|
|
|
{ |
66
|
|
|
if (empty($query)) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->connect(); |
71
|
|
|
|
72
|
|
|
$statement = $this->connection->stmt_init(); |
73
|
|
|
list($updQuery, $binds, $types) = $this->bindFromNamedToQuestions($query, $params); |
74
|
|
|
$statement->prepare(strval($updQuery)); |
75
|
|
|
if (!empty($binds)) { |
76
|
|
|
$statement->bind_param(implode('', $types), ...$binds); // @phpstan-ignore-line |
77
|
|
|
} |
78
|
|
|
$this->lastStatement = $statement; |
79
|
|
|
|
80
|
|
|
if (!$statement->execute()) { |
81
|
|
|
throw new MapperException('mysqli execute error: ' . $statement->error); |
82
|
|
|
} |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws MapperException |
88
|
|
|
*/ |
89
|
|
|
public function connect(): void |
90
|
|
|
{ |
91
|
|
|
if (!$this->isConnected()) { |
92
|
|
|
$this->connection = $this->connectToServer(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws MapperException |
98
|
|
|
* @return \mysqli |
99
|
|
|
*/ |
100
|
|
|
protected function connectToServer(): \mysqli |
101
|
|
|
{ |
102
|
|
|
$connection = new \mysqli( |
103
|
|
|
$this->config->getLocation(), |
104
|
|
|
$this->config->getUser(), |
105
|
|
|
$this->config->getPassword(), |
106
|
|
|
$this->config->getDatabase(), |
107
|
|
|
$this->config->getPort() |
108
|
|
|
); |
109
|
|
|
if ($connection->connect_errno) { |
110
|
|
|
throw new MapperException('mysqli connection error: ' . $connection->connect_error); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// foreach ($this->attributes as $key => $value){ |
114
|
|
|
// $connection->setAttribute($key, $value); |
115
|
|
|
// } |
116
|
|
|
|
117
|
|
|
$connection->set_charset('utf8'); |
118
|
|
|
if ($connection->errno) { |
119
|
|
|
throw new MapperException('mysqli error: ' . $connection->error); |
120
|
|
|
} |
121
|
|
|
$connection->query('SET NAMES utf8;'); |
122
|
|
|
|
123
|
|
|
return $connection; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function lastInsertId(): ?string |
127
|
|
|
{ |
128
|
|
|
return $this->lastStatement ? strval($this->lastStatement->insert_id) : null ; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function rowCount(): ?int |
132
|
|
|
{ |
133
|
|
|
return $this->lastStatement ? intval($this->lastStatement->num_rows) : null ; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function beginTransaction(): bool |
137
|
|
|
{ |
138
|
|
|
if (!$this->isConnected()) { |
139
|
|
|
$this->connection = $this->connectToServer(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->connection->begin_transaction(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function commit(): bool |
146
|
|
|
{ |
147
|
|
|
return $this->connection->commit(); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function rollBack(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->connection->rollBack(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|