Skip to content

Commit 658c8c6

Browse files
committed
Extending the driver, adding methods to use transactions
1 parent e2d4410 commit 658c8c6

File tree

8 files changed

+102
-313
lines changed

8 files changed

+102
-313
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"ext-mysqli": "*",
1212
"ext-json": "*",
1313
"microwin7/rcon": "^1.1",
14-
"sentry/sentry": "^4.7"
14+
"sentry/sentry": "^4.10"
1515
},
1616
"autoload": {
1717
"psr-4": {
@@ -34,6 +34,6 @@
3434
},
3535
"minimum-stability": "stable",
3636
"require-dev": {
37-
"vimeo/psalm": "5.x-dev"
37+
"vimeo/psalm": "6.8.8"
3838
}
3939
}

src/DB/Connector.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,34 @@
88

99
class Connector
1010
{
11-
/** @var array<string, DriverPDO> */
11+
/** @var array<string, DriverPDOInterface> */
1212
protected array $database = [];
1313

14-
public function __get(string $database): DriverPDO
14+
/** @var class-string<DriverPDOInterface> */
15+
protected static string $driver = DriverPDO::class;
16+
17+
public static function setCustomDriver(string $classDriver): void
18+
{
19+
if (is_subclass_of($classDriver, DriverPDOInterface::class)) {
20+
self::$driver = $classDriver;
21+
} else {
22+
throw new \RuntimeException("This $classDriver is not an implementation of " . DriverPDOInterface::class);
23+
}
24+
}
25+
public function __get(string $database): DriverPDOInterface
1526
{
1627
if (array_key_exists($database, $this->database)) return $this->database[$database];
1728
return $this->getConnect($database);
1829
}
19-
20-
private function getConnect(string $database): DriverPDO
30+
private function getConnect(string $database): DriverPDOInterface
2131
{
2232
$module = [];
2333
if (empty($database) || $database == Main::DB_NAME()) $database = Main::DB_NAME();
2434
else {
2535
try {
2636
/** @psalm-suppress RedundantFunctionCall */
2737
$database = strtolower(Main::DB_PREFIX_SERVERS() . Main::getServerWithoutDefault($database));
28-
} catch (ServerNotFoundException $e) {
38+
} catch (ServerNotFoundException) {
2939
$modules_keys_lower_case = array_change_key_case(MainConfig::MODULES);
3040
$key_exists = array_key_exists(strtolower($database), $modules_keys_lower_case);
3141
if ($key_exists === true) {
@@ -38,14 +48,6 @@ private function getConnect(string $database): DriverPDO
3848
}
3949
}
4050
if (array_key_exists($database, $this->database)) return $this->database[$database];
41-
return new DriverPDO($database, $module['prefix'] ?? '');
42-
// /**
43-
// * @var string $module['prefix']
44-
// * @psalm-suppress TypeDoesNotContainType
45-
// */
46-
// return $this->database[$database] = match (MainConfig::DB_DRIVER) {
47-
// DriverTypeEnum::MySQLi => new DriverMySQLi($database, $module['prefix'] ?? ''),
48-
// DriverTypeEnum::PDO => new DriverPDO($database, $module['prefix'] ?? ''),
49-
// };
51+
return new self::$driver($database, $module['prefix'] ?? '');
5052
}
5153
}

src/DB/DriverMySQLi.php

Lines changed: 0 additions & 237 deletions
This file was deleted.

src/DB/DriverPDO.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
/**
1212
* @template-implements \Iterator<int, array>
1313
*/
14-
class DriverPDO implements \Iterator
14+
class DriverPDO implements \Iterator, DriverPDOInterface
1515
{
16-
private \PDO $DBH;
16+
protected \PDO $DBH;
1717
private string $DSN;
1818
/** @psalm-suppress PropertyNotSetInConstructor */
1919
private \PDOStatement $STH;
@@ -63,6 +63,26 @@ private function preConnectionExec(): void
6363
public function __destruct()
6464
{
6565
}
66+
public function getDBH(): \PDO
67+
{
68+
return $this->DBH;
69+
}
70+
public function beginTransaction(): bool
71+
{
72+
return $this->DBH->beginTransaction();
73+
}
74+
public function inTransaction(): bool
75+
{
76+
return $this->DBH->inTransaction();
77+
}
78+
public function commit(): bool
79+
{
80+
return $this->DBH->commit();
81+
}
82+
public function rollback(): bool
83+
{
84+
return $this->DBH->rollBack();
85+
}
6686
private function table(string $table): string
6787
{
6888
return $this->table_prefix . $table . ' ';
@@ -144,25 +164,14 @@ public function query(string $sql, string $param_type = "", mixed ...$params): s
144164
}
145165
try {
146166
$this->insert_id = $this->DBH->lastInsertId();
147-
} catch (\PDOException $ignored) {
167+
} catch (\PDOException) {
148168
// Ошибка только с postgresql, предположительно, необходимо вызывать NEXTVAL('?') до LASTVAL()
149169
//Fatal error: Uncaught PDOException: SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR:
150170
//lastval is not yet defined in this session in Utils/DBDriverPDO.php:136
151171
}
152172
return $this;
153173
}
154174

155-
// public function execute(){
156-
// try {
157-
// $this->STH->execute();
158-
// } catch (\PDOException $e) {
159-
// $this->debug->debug_error($param_type ?
160-
// "[{$this->database}] Statement execution error: {$e}\n$sql with params:\n$param_type -> " . implode(', ', $params) :
161-
// "[{$this->database}] Statement execution error: {$e}\n$sql");
162-
// throw new DBException('SQL query error');
163-
// }
164-
// }
165-
166175
public function getStatementHandler(): \PDOStatement
167176
{
168177
return $this->STH;
@@ -238,20 +247,10 @@ public function all(): array
238247
* @param array $constructor_args Аргументы для конструктора передаваемого класса, для заполнения
239248
* @return T|null Возвращает объект с параметрами класса как в БД и заполненными добавочными данными из аргументов констркутора класса
240249
*/
241-
public function obj($class = \stdClass::class, array $constructor_args = [])
250+
public function obj($class = \stdClass::class, array $constructor_args = []): object|null
242251
{
243252
return $this->STH->fetchObject($class, $constructor_args) ?: null;
244253
}
245-
/**
246-
* Индексированный массив объектов результата
247-
* Use objects()
248-
* @deprecated
249-
* @return array
250-
*/
251-
public function object(): array
252-
{
253-
return $this->objects();
254-
}
255254
// Индексированный массив объектов результата
256255
/**
257256
* @template T of object

0 commit comments

Comments
 (0)