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
|
70 |
|
public function __construct(protected readonly Connection $conn) |
20
|
|
|
{ |
21
|
70 |
|
$this->print = $conn->print(); |
22
|
|
|
} |
23
|
|
|
|
24
|
27 |
|
public function __get(string $key): Folder |
25
|
|
|
{ |
26
|
27 |
|
$exists = false; |
27
|
|
|
|
28
|
27 |
|
foreach ($this->conn->sql() as $path) { |
29
|
|
|
assert(is_string($path)); |
30
|
27 |
|
$exists = is_dir($path . DIRECTORY_SEPARATOR . $key); |
31
|
|
|
|
32
|
27 |
|
if ($exists) { |
33
|
26 |
|
break; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
27 |
|
if (!$exists) { |
38
|
1 |
|
throw new RuntimeException('The SQL folder does not exist: ' . $key); |
39
|
|
|
} |
40
|
|
|
|
41
|
26 |
|
return new Folder($this, $key); |
42
|
|
|
} |
43
|
|
|
|
44
|
41 |
|
public function getFetchMode(): int |
45
|
|
|
{ |
46
|
41 |
|
return $this->conn->fetchMode; |
47
|
|
|
} |
48
|
|
|
|
49
|
37 |
|
public function getPdoDriver(): string |
50
|
|
|
{ |
51
|
37 |
|
return $this->conn->driver; |
52
|
|
|
} |
53
|
|
|
|
54
|
26 |
|
public function getSqlDirs(): array |
55
|
|
|
{ |
56
|
26 |
|
return $this->conn->sql(); |
57
|
|
|
} |
58
|
|
|
|
59
|
61 |
|
public function connect(): static |
60
|
|
|
{ |
61
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
62
|
61 |
|
if (isset($this->pdo)) { |
63
|
54 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
61 |
|
$conn = $this->conn; |
67
|
|
|
|
68
|
|
|
/** @psalm-suppress InaccessibleProperty */ |
69
|
61 |
|
$this->pdo = new PDO( |
70
|
61 |
|
$conn->dsn, |
71
|
61 |
|
$conn->username, |
72
|
61 |
|
$conn->password, |
73
|
61 |
|
$conn->options, |
74
|
61 |
|
); |
75
|
|
|
|
76
|
|
|
// Always throw an exception when an error occures |
77
|
61 |
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
78
|
|
|
// Allow getting the number of rows |
79
|
61 |
|
$this->pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL); |
80
|
|
|
// deactivate native prepared statements by default |
81
|
61 |
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
82
|
|
|
// do not alter casing of the columns from sql |
83
|
61 |
|
$this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); |
84
|
|
|
|
85
|
61 |
|
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
|
60 |
|
public function getConn(): PDO |
113
|
|
|
{ |
114
|
60 |
|
$this->connect(); |
115
|
|
|
|
116
|
60 |
|
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
|
|
|
|