|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ___ _ _ |
|
5
|
|
|
* | _ \__ _| |_ __ _| |__ __ _ ___ ___ |
|
6
|
|
|
* | _/ _` | _/ _` | '_ \/ _` (_-</ -_) |
|
7
|
|
|
* |_| \__,_|\__\__,_|_.__/\__,_/__/\___| |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of Kristuff\Patabase. |
|
10
|
|
|
* (c) Kristuff <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* @version 1.0.1 |
|
16
|
|
|
* @copyright 2017-2022 Christophe Buliard |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Kristuff\Patabase; |
|
20
|
|
|
|
|
21
|
|
|
use Kristuff\Patabase\Driver; |
|
22
|
|
|
use Kristuff\Patabase\Datasource; |
|
23
|
|
|
use Kristuff\Patabase\Query; |
|
24
|
|
|
use Kristuff\Patabase\Query\CreateTable; |
|
25
|
|
|
use Kristuff\Patabase\Query\Delete; |
|
26
|
|
|
use Kristuff\Patabase\Query\Insert; |
|
27
|
|
|
use Kristuff\Patabase\Query\Select; |
|
28
|
|
|
use Kristuff\Patabase\Query\Update; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class Database |
|
32
|
|
|
* |
|
33
|
|
|
* Represents a connection to SQL database |
|
34
|
|
|
*/ |
|
35
|
|
|
class Database extends Datasource |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Open a database connection |
|
40
|
|
|
* |
|
41
|
|
|
* @access protected |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function openConnection(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->driver = Driver\DriverFactory::getInstance($this->settings, false); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a new instance of Table object |
|
51
|
|
|
* |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @param string $tableName The name of the table |
|
54
|
|
|
* |
|
55
|
|
|
* @return Table |
|
56
|
|
|
*/ |
|
57
|
|
|
public function table($tableName): Table |
|
58
|
|
|
{ |
|
59
|
|
|
return new Table($this, $tableName); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a new CreateTable instance |
|
64
|
|
|
* |
|
65
|
|
|
* @access public |
|
66
|
|
|
* @param string $tableName The name of the table |
|
67
|
|
|
* @return Query\CreateTable |
|
68
|
|
|
*/ |
|
69
|
|
|
public function createTable($tableName): CreateTable |
|
70
|
|
|
{ |
|
71
|
|
|
return new Query\CreateTable($this->getDriver(), $tableName); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Checks if the a table exists |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @param string $tableName The name of the table |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool True if the table exists, otherwise false. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function tableExists($tableName): bool |
|
83
|
|
|
{ |
|
84
|
|
|
$sql = trim(sprintf('SELECT 1 FROM %s LIMIT 1', $tableName)); |
|
85
|
|
|
try { |
|
86
|
|
|
$query = $this->getConnection()->prepare($sql); |
|
87
|
|
|
return $query->execute(); |
|
88
|
|
|
} catch (\Exception $e) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Drop a table |
|
95
|
|
|
* |
|
96
|
|
|
* @access public |
|
97
|
|
|
* @param string $tableName The name of the table |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool True if the table has been dropped, otherwise false |
|
100
|
|
|
*/ |
|
101
|
|
|
public function dropTable(string $tableName): bool |
|
102
|
|
|
{ |
|
103
|
|
|
$sql = trim(sprintf('DROP TABLE %s', $tableName)); |
|
104
|
|
|
return $this->driver->prepareAndExecuteSql($sql); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Rename a table |
|
109
|
|
|
* |
|
110
|
|
|
* @access public |
|
111
|
|
|
* @param string $currentName The current name of the table to rename |
|
112
|
|
|
* @param string $newName The new table name |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool True if the table has been renamed, otherwise false. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function renameTable(string $currentName, string $newName): bool |
|
117
|
|
|
{ |
|
118
|
|
|
$sql = trim(sprintf('ALTER TABLE %s RENAME TO %s', $currentName, $newName)); |
|
119
|
|
|
return $this->driver->prepareAndExecuteSql($sql); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get a list of tables in the current database |
|
124
|
|
|
* |
|
125
|
|
|
* @access public |
|
126
|
|
|
* @return array A non indexed array containing tables names |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getTables(): array |
|
129
|
|
|
{ |
|
130
|
|
|
$sql = $this->driver->sqlShowTables(); |
|
131
|
|
|
$query = $this->getConnection()->prepare($sql); |
|
132
|
|
|
$query->execute(); |
|
133
|
|
|
return $query->fetchAll(\PDO::FETCH_COLUMN); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Enable foreign keys |
|
138
|
|
|
* |
|
139
|
|
|
* @access public |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function enableForeignKeys(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$this->driver->enableForeignKeys(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Disable foreign keys |
|
149
|
|
|
* |
|
150
|
|
|
* @access public |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function disableForeignKeys(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$this->driver->disableForeignKeys(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Add a foreign key |
|
160
|
|
|
* |
|
161
|
|
|
* @access public |
|
162
|
|
|
* @param string $fkName The constraint name |
|
163
|
|
|
* @param string $srcTable The source table |
|
164
|
|
|
* @param string $srcColumn The source column |
|
165
|
|
|
* @param string $refTable The referenced table |
|
166
|
|
|
* @param string $refColumn The referenced column |
|
167
|
|
|
* |
|
168
|
|
|
* @return bool True if the foreign key has been added, otherwise false |
|
169
|
|
|
*/ |
|
170
|
|
|
public function addForeignKey(string $fkName, string $srcTable, string $srcColumn, string $refTable, string $refColumn): bool |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->driver->addForeignKey($fkName, $srcTable, $srcColumn, $refTable, $refColumn); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Drop a foreign key |
|
177
|
|
|
* |
|
178
|
|
|
* @access public |
|
179
|
|
|
* @param string $fkName The constraint name |
|
180
|
|
|
* @param string $tableName The source table |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool True if the foreign key has been dropped, otherwise false |
|
183
|
|
|
*/ |
|
184
|
|
|
public function dropForeignKey(string $fkName, string $tableName): bool |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->driver->dropForeignKey($fkName, $tableName); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get a new Insert query instance |
|
191
|
|
|
* |
|
192
|
|
|
* @access public |
|
193
|
|
|
* @param string $tableName The table in wich insert to |
|
194
|
|
|
* |
|
195
|
|
|
* @return Query\Insert |
|
196
|
|
|
*/ |
|
197
|
|
|
public function insert(string $tableName): Insert |
|
198
|
|
|
{ |
|
199
|
|
|
$query = new Query\Insert($this->driver, $tableName); |
|
200
|
|
|
return $query; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get a new Select query instance |
|
205
|
|
|
* |
|
206
|
|
|
* @access public |
|
207
|
|
|
* @param mixed |
|
208
|
|
|
* |
|
209
|
|
|
* @return Query\Select |
|
210
|
|
|
*/ |
|
211
|
|
|
public function select(): Select |
|
212
|
|
|
{ |
|
213
|
|
|
$args = func_get_args(); |
|
214
|
|
|
$query = new Query\Select($this->driver, null, $args); |
|
215
|
|
|
return $query; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get a new Update query instance |
|
220
|
|
|
* |
|
221
|
|
|
* @access public |
|
222
|
|
|
* @param string $tableName The name of the table |
|
223
|
|
|
* |
|
224
|
|
|
* @return Query\Update |
|
225
|
|
|
*/ |
|
226
|
|
|
public function update(string $tableName): Update |
|
227
|
|
|
{ |
|
228
|
|
|
$query = new Query\Update($this->driver, $tableName); |
|
229
|
|
|
return $query; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get a new Delete query instance |
|
234
|
|
|
* |
|
235
|
|
|
* @access public |
|
236
|
|
|
* @param string $tableName The name of the table |
|
237
|
|
|
* |
|
238
|
|
|
* @return Query\Delete |
|
239
|
|
|
*/ |
|
240
|
|
|
public function delete(string $tableName): Delete |
|
241
|
|
|
{ |
|
242
|
|
|
return new Query\Delete($this->driver, $tableName); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Get whether the driver is in transaction |
|
247
|
|
|
* |
|
248
|
|
|
* @access public |
|
249
|
|
|
* @return bool True if the driver is in transaction, otherwise false |
|
250
|
|
|
*/ |
|
251
|
|
|
public function inTransaction(): bool |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->getConnection()->inTransaction(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Begin a transaction |
|
258
|
|
|
* |
|
259
|
|
|
* @access public |
|
260
|
|
|
* @return void |
|
261
|
|
|
*/ |
|
262
|
|
|
public function beginTransaction(): void |
|
263
|
|
|
{ |
|
264
|
|
|
if (! $this->inTransaction()) { |
|
265
|
|
|
$this->getConnection()->beginTransaction(); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Commit a transaction |
|
271
|
|
|
* |
|
272
|
|
|
* @access public |
|
273
|
|
|
* @return void |
|
274
|
|
|
*/ |
|
275
|
|
|
public function commit(): void |
|
276
|
|
|
{ |
|
277
|
|
|
if ($this->inTransaction()) { |
|
278
|
|
|
$this->getConnection()->commit(); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Rollback a transaction |
|
284
|
|
|
* |
|
285
|
|
|
* @access public |
|
286
|
|
|
* @return void |
|
287
|
|
|
*/ |
|
288
|
|
|
public function rollback(): void |
|
289
|
|
|
{ |
|
290
|
|
|
$this->getConnection()->rollback(); |
|
291
|
|
|
} |
|
292
|
|
|
} |