-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDatabase.php
161 lines (135 loc) · 4.54 KB
/
Database.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Gt\Database;
use Gt\Database\Connection\Connection;
use Gt\Database\Connection\DefaultSettings;
use Gt\Database\Connection\Driver;
use Gt\Database\Connection\SettingsInterface;
use Gt\Database\Query\QueryCollection;
use Gt\Database\Query\QueryCollectionFactory;
use Gt\Database\Result\ResultSet;
use PDOException;
/**
* The Database Client stores the factory for creating QueryCollections, and an
* associative array of connection settings, allowing for multiple database
* connections. If only one database connection is required, a name is not
* required as the default name will be used.
*/
class Database {
use Fetchable;
const COLLECTION_SEPARATOR_CHARACTERS = [".", "/", "\\"];
/** @var array<QueryCollectionFactory> */
protected array $queryCollectionFactoryArray;
/** @var array<Driver> */
protected array $driverArray;
protected Connection $currentConnectionName;
public function __construct(SettingsInterface...$connectionSettings) {
if(empty($connectionSettings)) {
$connectionSettings[DefaultSettings::DEFAULT_NAME]
= new DefaultSettings();
}
$this->storeConnectionDriverFromSettings($connectionSettings);
$this->storeQueryCollectionFactoryFromSettings($connectionSettings);
}
public function insert(string $queryName, mixed...$bindings):string {
$result = $this->query($queryName, $bindings);
return $result->lastInsertId();
}
public function delete(string $queryName, mixed...$bindings):int {
$result = $this->query($queryName, $bindings);
return $result->affectedRows();
}
public function update(string $queryName, mixed...$bindings):int {
$result = $this->query($queryName, $bindings);
return $result->affectedRows();
}
public function query(string $fullQueryPath, mixed...$bindings):ResultSet {
$queryCollectionName = $queryFile = "";
foreach(self::COLLECTION_SEPARATOR_CHARACTERS as $char) {
if(!strstr($fullQueryPath, $char)) {
continue;
}
$queryCollectionName = substr(
$fullQueryPath,
0,
strrpos($fullQueryPath, $char)
);
$queryFile = substr(
$fullQueryPath,
strrpos($fullQueryPath, $char) + 1
);
break;
}
$connectionName = $this->currentConnectionName ?? DefaultSettings::DEFAULT_NAME;
$queryCollection = $this->queryCollection(
$queryCollectionName,
$connectionName
);
return $queryCollection->query($queryFile, $bindings);
}
public function setCurrentConnectionName(string $connectionName):void {
$this->currentConnectionName = $this->getNamedConnection(
$connectionName
);
}
/** @param array<string, mixed>|array<mixed> $bindings */
public function executeSql(
string $query,
array $bindings = [],
string $connectionName = DefaultSettings::DEFAULT_NAME
):ResultSet {
$connection = $this->getNamedConnection($connectionName);
try {
$statement = $connection->prepare($query);
}
catch(PDOException $exception) {
throw new StatementPreparationException(
$exception->getMessage(),
intval($exception->getCode())
);
}
try {
$statement->execute($bindings);
}
catch(PDOException $exception) {
throw new StatementExecutionException(
$exception->getMessage(),
intval($exception->getCode())
);
}
return new ResultSet($statement, $connection->lastInsertId());
}
protected function getNamedConnection(string $connectionName):Connection {
$driver = $this->driverArray[$connectionName];
return $driver->getConnection();
}
/** @param array<SettingsInterface> $settingsArray */
protected function storeConnectionDriverFromSettings(array $settingsArray):void {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->driverArray[$connectionName] = new Driver($settings);
}
}
/** @param array<SettingsInterface> $settingsArray */
protected function storeQueryCollectionFactoryFromSettings(array $settingsArray):void {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->queryCollectionFactoryArray[$connectionName] =
new QueryCollectionFactory($this->driverArray[$connectionName]);
}
}
public function queryCollection(
string $queryCollectionName,
string $connectionName = DefaultSettings::DEFAULT_NAME
):QueryCollection {
return $this->queryCollectionFactoryArray[$connectionName]->create($queryCollectionName);
}
public function getDriver(
string $connectionName = DefaultSettings::DEFAULT_NAME
):Driver {
return $this->driverArray[$connectionName];
}
protected function getFirstConnectionName():string {
reset($this->driverArray);
return key($this->driverArray);
}
}