|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Conia\Quma; |
|
6
|
|
|
|
|
7
|
|
|
use Conia\Quma\Connection; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
/** @psalm-api */ |
|
12
|
|
|
class Database |
|
13
|
|
|
{ |
|
14
|
|
|
use GetsSetsPrint; |
|
15
|
|
|
|
|
16
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */ |
|
17
|
|
|
protected readonly PDO $pdo; |
|
18
|
|
|
|
|
19
|
69 |
|
public function __construct(protected readonly Connection $conn) |
|
20
|
|
|
{ |
|
21
|
69 |
|
$this->print = $conn->print(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
26 |
|
public function __get(string $key): Folder |
|
25
|
|
|
{ |
|
26
|
26 |
|
$exists = false; |
|
27
|
|
|
|
|
28
|
26 |
|
foreach ($this->conn->sql() as $path) { |
|
29
|
|
|
assert(is_string($path)); |
|
30
|
26 |
|
$exists = is_dir($path . DIRECTORY_SEPARATOR . $key); |
|
31
|
|
|
|
|
32
|
26 |
|
if ($exists) { |
|
33
|
25 |
|
break; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
26 |
|
if (!$exists) { |
|
38
|
1 |
|
throw new RuntimeException('The SQL folder does not exist: ' . $key); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
25 |
|
return new Folder($this, $key); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
40 |
|
public function getFetchMode(): int |
|
45
|
|
|
{ |
|
46
|
40 |
|
return $this->conn->fetchMode; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
37 |
|
public function getPdoDriver(): string |
|
50
|
|
|
{ |
|
51
|
37 |
|
return $this->conn->driver; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
25 |
|
public function getSqlDirs(): array |
|
55
|
|
|
{ |
|
56
|
25 |
|
return $this->conn->sql(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
60 |
|
public function connect(): static |
|
60
|
|
|
{ |
|
61
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
|
62
|
60 |
|
if (isset($this->pdo)) { |
|
63
|
53 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
60 |
|
$conn = $this->conn; |
|
67
|
|
|
|
|
68
|
|
|
/** @psalm-suppress InaccessibleProperty */ |
|
69
|
60 |
|
$this->pdo = new PDO( |
|
70
|
60 |
|
$conn->dsn, |
|
71
|
60 |
|
$conn->username, |
|
72
|
60 |
|
$conn->password, |
|
73
|
60 |
|
$conn->options, |
|
74
|
60 |
|
); |
|
75
|
|
|
|
|
76
|
|
|
// Always throw an exception when an error occures |
|
77
|
60 |
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
78
|
|
|
// Allow getting the number of rows |
|
79
|
60 |
|
$this->pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL); |
|
80
|
|
|
// deactivate native prepared statements by default |
|
81
|
60 |
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
|
82
|
|
|
// do not alter casing of the columns from sql |
|
83
|
60 |
|
$this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); |
|
84
|
|
|
|
|
85
|
60 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function quote(string $value): string |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->connect(); |
|
91
|
|
|
|
|
92
|
1 |
|
return $this->pdo->quote($value); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
17 |
|
public function begin(): bool |
|
96
|
|
|
{ |
|
97
|
17 |
|
$this->connect(); |
|
98
|
|
|
|
|
99
|
17 |
|
return $this->pdo->beginTransaction(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
public function commit(): bool |
|
103
|
|
|
{ |
|
104
|
4 |
|
return $this->pdo->commit(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
13 |
|
public function rollback(): bool |
|
108
|
|
|
{ |
|
109
|
13 |
|
return $this->pdo->rollback(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
59 |
|
public function getConn(): PDO |
|
113
|
|
|
{ |
|
114
|
59 |
|
$this->connect(); |
|
115
|
|
|
|
|
116
|
59 |
|
return $this->pdo; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
35 |
|
public function execute(string $query, mixed ...$args): Query |
|
120
|
|
|
{ |
|
121
|
35 |
|
return new Query($this, $query, new Args($args)); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|