1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?php
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
abstract class Minz_Pdo extends PDO {
public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
abstract public function dbType();
private $prefix = '';
public function prefix(): string {
return $this->prefix;
}
public function setPrefix(string $prefix) {
$this->prefix = $prefix;
}
private function autoPrefix(string $sql): string {
return str_replace('`_', '`' . $this->prefix, $sql);
}
protected function preSql(string $statement): string {
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
invalidateHttpCache();
}
return $this->autoPrefix($statement);
}
// PHP8+: PDO::lastInsertId(?string $name = null): string|false
#[\ReturnTypeWillChange]
public function lastInsertId($name = null) {
if ($name != null) {
$name = $this->preSql($name);
}
return parent::lastInsertId($name);
}
// PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
#[\ReturnTypeWillChange]
public function prepare($statement, $driver_options = []) {
$statement = $this->preSql($statement);
return parent::prepare($statement, $driver_options);
}
// PHP8+: PDO::exec(string $statement): int|false
#[\ReturnTypeWillChange]
public function exec($statement) {
$statement = $this->preSql($statement);
return parent::exec($statement);
}
// PHP8+: PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs): PDOStatement|false
#[\ReturnTypeWillChange]
public function query($query, $fetch_mode = null, ...$fetch_mode_args) {
$query = $this->preSql($query);
return $fetch_mode ? parent::query($query, $fetch_mode, ...$fetch_mode_args) : parent::query($query);
}
}
|