29
29
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30
30
*/
31
31
32
- namespace Liuch \DmarcSrg \Database \Mariadb ;
32
+ namespace Liuch \DmarcSrg \Database \Common ;
33
33
34
34
use Liuch \DmarcSrg \DateTime ;
35
35
use Liuch \DmarcSrg \ErrorCodes ;
40
40
use Liuch \DmarcSrg \Exception \DatabaseNotFoundException ;
41
41
42
42
/**
43
- * DomainMapper class implementation for MariaDB
43
+ * Universal implementation of DomainMapper class
44
44
*/
45
45
class DomainMapper implements DomainMapperInterface
46
46
{
@@ -68,8 +68,8 @@ public function exists(array &$data): bool
68
68
{
69
69
try {
70
70
$ st = $ this ->connector ->dbh ()->prepare (
71
- 'SELECT `id` FROM ` ' . $ this ->connector ->tablePrefix ('domains ' ) .
72
- '` WHERE ' . $ this ->sqlCondition ($ data )
71
+ 'SELECT id FROM ' . $ this ->connector ->tablePrefix ('domains ' ) .
72
+ ' WHERE ' . $ this ->sqlCondition ($ data )
73
73
);
74
74
$ this ->sqlBindValues ($ st , $ data , 1 );
75
75
$ st ->execute ();
@@ -98,8 +98,8 @@ public function isAssigned(array &$data, int $user_id): bool
98
98
$ res = null ;
99
99
try {
100
100
$ st = $ this ->connector ->dbh ()->prepare (
101
- 'SELECT 1 FROM ` ' . $ this ->connector ->tablePrefix ('userdomains ' ) . '` INNER JOIN ` '
102
- . $ this ->connector ->tablePrefix ('domains ' ) . '` ON ` domain_id` = `id` WHERE ` user_id` = ? AND '
101
+ 'SELECT 1 FROM ' . $ this ->connector ->tablePrefix ('userdomains ' ) . ' INNER JOIN '
102
+ . $ this ->connector ->tablePrefix ('domains ' ) . ' ON domain_id = id WHERE user_id = ? AND '
103
103
. $ this ->sqlCondition ($ data )
104
104
);
105
105
$ st ->bindValue (1 , $ user_id , \PDO ::PARAM_INT );
@@ -124,8 +124,8 @@ public function fetch(array &$data): void
124
124
{
125
125
try {
126
126
$ st = $ this ->connector ->dbh ()->prepare (
127
- 'SELECT `id`, ` fqdn`, ` active`, ` description`, ` created_time`, ` updated_time` FROM ` '
128
- . $ this ->connector ->tablePrefix ('domains ' ) . '` WHERE ' . $ this ->sqlCondition ($ data )
127
+ 'SELECT id, fqdn, active, description, created_time, updated_time FROM '
128
+ . $ this ->connector ->tablePrefix ('domains ' ) . ' WHERE ' . $ this ->sqlCondition ($ data )
129
129
);
130
130
$ this ->sqlBindValues ($ st , $ data , 1 );
131
131
$ st ->execute ();
@@ -159,8 +159,8 @@ public function save(array &$data): void
159
159
if ($ this ->exists ($ data )) {
160
160
try {
161
161
$ st = $ db ->prepare (
162
- 'UPDATE ` ' . $ this ->connector ->tablePrefix ('domains ' )
163
- . '` SET ` active` = ?, ` description` = ?, ` updated_time` = ? WHERE `id` = ? '
162
+ 'UPDATE ' . $ this ->connector ->tablePrefix ('domains ' )
163
+ . ' SET active = ?, description = ?, updated_time = ? WHERE id = ? '
164
164
);
165
165
$ st ->bindValue (1 , $ data ['active ' ], \PDO ::PARAM_BOOL );
166
166
$ st ->bindValue (2 , $ data ['description ' ], \PDO ::PARAM_STR );
@@ -179,12 +179,12 @@ public function save(array &$data): void
179
179
$ sql1 = '' ;
180
180
$ sql2 = '' ;
181
181
} else {
182
- $ sql1 = ', ` description` ' ;
182
+ $ sql1 = ', description ' ;
183
183
$ sql2 = ', ? ' ;
184
184
}
185
185
$ st = $ db ->prepare (
186
- 'INSERT INTO ` ' . $ this ->connector ->tablePrefix ('domains ' )
187
- . '` (` fqdn`, ` active` ' . $ sql1 . ', ` created_time`, ` updated_time` ) '
186
+ 'INSERT INTO ' . $ this ->connector ->tablePrefix ('domains ' )
187
+ . ' ( fqdn, active ' . $ sql1 . ', created_time, updated_time) '
188
188
. ' VALUES (?, ? ' . $ sql2 . ', ?, ?) '
189
189
);
190
190
$ idx = 0 ;
@@ -247,12 +247,12 @@ public function delete(int $id, bool $force): void
247
247
}
248
248
}
249
249
$ st = $ db ->prepare (
250
- 'DELETE FROM ` ' . $ this ->connector ->tablePrefix ('userdomains ' ) . '` WHERE ` domain_id` = ? '
250
+ 'DELETE FROM ' . $ this ->connector ->tablePrefix ('userdomains ' ) . ' WHERE domain_id = ? '
251
251
);
252
252
$ st ->bindValue (1 , $ id , \PDO ::PARAM_INT );
253
253
$ st ->execute ();
254
254
$ st ->closeCursor ();
255
- $ st = $ db ->prepare ('DELETE FROM ` ' . $ this ->connector ->tablePrefix ('domains ' ) . '` WHERE `id` = ? ' );
255
+ $ st = $ db ->prepare ('DELETE FROM ' . $ this ->connector ->tablePrefix ('domains ' ) . ' WHERE id = ? ' );
256
256
$ st ->bindValue (1 , $ id , \PDO ::PARAM_INT );
257
257
$ st ->execute ();
258
258
$ st ->closeCursor ();
@@ -277,13 +277,13 @@ public function list(int $user_id): array
277
277
{
278
278
$ list = [];
279
279
try {
280
- $ query_str = 'SELECT `id`, ` fqdn`, ` active`, ` description`, ` created_time`, ` updated_time` FROM ` ' ;
280
+ $ query_str = 'SELECT id, fqdn, active, description, created_time, updated_time FROM ' ;
281
281
if ($ user_id ) {
282
- $ query_str .= $ this ->connector ->tablePrefix ('userdomains ' ) . '` INNER JOIN ` '
283
- . $ this ->connector ->tablePrefix ('domains ' ) . '` ON ` domain_id` = `id` WHERE ` user_id` = '
282
+ $ query_str .= $ this ->connector ->tablePrefix ('userdomains ' ) . ' INNER JOIN '
283
+ . $ this ->connector ->tablePrefix ('domains ' ) . ' ON domain_id = id WHERE user_id = '
284
284
. $ user_id ;
285
285
} else {
286
- $ query_str .= $ this ->connector ->tablePrefix ('domains ' ) . ' ` ' ;
286
+ $ query_str .= $ this ->connector ->tablePrefix ('domains ' );
287
287
}
288
288
$ st = $ this ->connector ->dbh ()->query ($ query_str );
289
289
while ($ row = $ st ->fetch (\PDO ::FETCH_NUM )) {
@@ -315,11 +315,11 @@ public function names(int $user_id): array
315
315
$ res = [];
316
316
try {
317
317
if ($ user_id ) {
318
- $ query_str = 'SELECT ` fqdn` FROM ` ' . $ this ->connector ->tablePrefix ('userdomains ' )
319
- . '` INNER JOIN ` ' . $ this ->connector ->tablePrefix ('domains ' )
320
- . '` ON ` domain_id` = `id` WHERE ` user_id` = ' . $ user_id . ' ORDER BY ` fqdn` ' ;
318
+ $ query_str = 'SELECT fqdn FROM ' . $ this ->connector ->tablePrefix ('userdomains ' )
319
+ . ' INNER JOIN ' . $ this ->connector ->tablePrefix ('domains ' )
320
+ . ' ON domain_id = id WHERE user_id = ' . $ user_id . ' ORDER BY fqdn ' ;
321
321
} else {
322
- $ query_str = 'SELECT ` fqdn` FROM ` ' . $ this ->connector ->tablePrefix ('domains ' ) . '` ORDER BY ` fqdn` ' ;
322
+ $ query_str = 'SELECT fqdn FROM ' . $ this ->connector ->tablePrefix ('domains ' ) . ' ORDER BY fqdn ' ;
323
323
}
324
324
$ st = $ this ->connector ->dbh ()->query ($ query_str , \PDO ::FETCH_NUM );
325
325
while ($ name = $ st ->fetchColumn (0 )) {
@@ -349,10 +349,10 @@ public function count(int $user_id, int $max = 0): int
349
349
$ wr = '' ;
350
350
} else {
351
351
$ tn = 'userdomains ' ;
352
- $ wr = " WHERE ` user_id` = {$ user_id }" ;
352
+ $ wr = " WHERE user_id = {$ user_id }" ;
353
353
}
354
354
$ tn = $ this ->connector ->tablePrefix ($ tn );
355
- $ query_str = "SELECT COUNT(*) FROM ` {$ tn }` {$ wr }" ;
355
+ $ query_str = "SELECT COUNT(*) FROM {$ tn }{$ wr }" ;
356
356
if ($ max > 0 ) {
357
357
$ query_str .= " LIMIT {$ max }" ;
358
358
}
@@ -383,8 +383,8 @@ public function assignUser(array &$data, int $user_id): void
383
383
$ db ->beginTransaction ();
384
384
try {
385
385
$ st = $ db ->prepare (
386
- 'SELECT `id` FROM ` ' . $ this ->connector ->tablePrefix ('domains ' )
387
- . '` WHERE ' . $ this ->sqlCondition ($ data )
386
+ 'SELECT id FROM ' . $ this ->connector ->tablePrefix ('domains ' )
387
+ . ' WHERE ' . $ this ->sqlCondition ($ data )
388
388
);
389
389
$ this ->sqlBindValues ($ st , $ data , 1 );
390
390
$ st ->execute ();
@@ -393,22 +393,22 @@ public function assignUser(array &$data, int $user_id): void
393
393
if ($ id !== false ) {
394
394
$ data ['id ' ] = intval ($ id );
395
395
$ st = $ db ->prepare (
396
- 'SELECT 1 FROM ` ' . $ this ->connector ->tablePrefix ('users ' ) . '` WHERE `id` = ? '
396
+ 'SELECT 1 FROM ' . $ this ->connector ->tablePrefix ('users ' ) . ' WHERE id = ? '
397
397
);
398
398
$ st ->bindValue (1 , $ user_id , \PDO ::PARAM_INT );
399
399
$ st ->execute ();
400
400
$ res = $ st ->fetchColumn (0 );
401
401
$ st ->closeCursor ();
402
402
if ($ res ) {
403
403
$ ud_tn = $ this ->connector ->tablePrefix ('userdomains ' );
404
- $ st = $ db ->prepare ('SELECT 1 FROM ` ' . $ ud_tn . '` WHERE ` domain_id` = ? AND ` user_id` = ? ' );
404
+ $ st = $ db ->prepare ('SELECT 1 FROM ' . $ ud_tn . ' WHERE domain_id = ? AND user_id = ? ' );
405
405
$ st ->bindValue (1 , $ data ['id ' ], \PDO ::PARAM_INT );
406
406
$ st ->bindValue (2 , $ user_id , \PDO ::PARAM_INT );
407
407
$ st ->execute ();
408
408
$ res = $ st ->fetchColumn (0 );
409
409
$ st ->closeCursor ();
410
410
if (!$ res ) {
411
- $ st = $ db ->prepare ('INSERT INTO ` ' . $ ud_tn . '` (` domain_id`, ` user_id` ) VALUES (?, ?) ' );
411
+ $ st = $ db ->prepare ('INSERT INTO ' . $ ud_tn . ' ( domain_id, user_id) VALUES (?, ?) ' );
412
412
$ st ->bindValue (1 , $ data ['id ' ], \PDO ::PARAM_INT );
413
413
$ st ->bindValue (2 , $ user_id , \PDO ::PARAM_INT );
414
414
$ st ->execute ();
@@ -419,7 +419,7 @@ public function assignUser(array &$data, int $user_id): void
419
419
$ db ->commit ();
420
420
} catch (\Exception $ e ) {
421
421
$ db ->rollBack ();
422
- throw $ e ;
422
+ throw new DatabaseFatalException ( ' Failed to assign a domain ' , - 1 , $ e ) ;
423
423
}
424
424
}
425
425
@@ -441,7 +441,7 @@ public function unassignUser(array &$data, int $user_id): void
441
441
$ dm_tn = $ this ->connector ->tablePrefix ('domains ' );
442
442
$ ud_tn = $ this ->connector ->tablePrefix ('userdomains ' );
443
443
$ st = $ this ->connector ->dbh ()->prepare (
444
- "DELETE ` {$ ud_tn }` FROM ` {$ ud_tn }` INNER JOIN ` {$ dm_tn }` ON ` domain_id` = `id` WHERE "
444
+ "DELETE {$ ud_tn } FROM {$ ud_tn } INNER JOIN {$ dm_tn } ON domain_id = id WHERE "
445
445
. $ this ->sqlCondition ($ data )
446
446
);
447
447
$ this ->sqlBindValues ($ st , $ data , 1 );
@@ -470,16 +470,16 @@ public function updateUserDomains(array &$domains, int $user_id): void
470
470
$ db ->beginTransaction ();
471
471
try {
472
472
$ st = $ db ->prepare (
473
- 'DELETE FROM ` ' . $ this ->connector ->tablePrefix ('userdomains ' ) . '` WHERE ` user_id` = ? '
473
+ 'DELETE FROM ' . $ this ->connector ->tablePrefix ('userdomains ' ) . ' WHERE user_id = ? '
474
474
);
475
475
$ st ->bindValue (1 , $ user_id , \PDO ::PARAM_INT );
476
476
$ st ->execute ();
477
477
$ st ->closeCursor ();
478
478
$ cnt = count ($ domains );
479
479
if ($ cnt ) {
480
- $ query_str = 'INSERT INTO ` ' . $ this ->connector ->tablePrefix ('userdomains ' )
481
- . '` (` domain_id`, ` user_id` ) SELECT `id` , ' . $ user_id . ' FROM ` '
482
- . $ this ->connector ->tablePrefix ('domains ' ) . '` WHERE ` fqdn` IN ( '
480
+ $ query_str = 'INSERT INTO ' . $ this ->connector ->tablePrefix ('userdomains ' )
481
+ . ' ( domain_id, user_id) SELECT id , ' . $ user_id . ' FROM '
482
+ . $ this ->connector ->tablePrefix ('domains ' ) . ' WHERE fqdn IN ( '
483
483
. substr (str_repeat ('?, ' , $ cnt ), 0 , -1 ) . ') ' ;
484
484
$ st = $ db ->prepare ($ query_str );
485
485
$ pos = 0 ;
@@ -492,7 +492,7 @@ public function updateUserDomains(array &$domains, int $user_id): void
492
492
$ db ->commit ();
493
493
} catch (\Exception $ e ) {
494
494
$ db ->rollBack ();
495
- throw $ e ;
495
+ throw new DatabaseFatalException ( ' Failed to update the user \' s domains ' , - 1 , $ e ) ;
496
496
}
497
497
}
498
498
@@ -506,9 +506,9 @@ public function updateUserDomains(array &$domains, int $user_id): void
506
506
private function sqlCondition (array &$ data ): string
507
507
{
508
508
if (isset ($ data ['id ' ])) {
509
- return '`id` = ? ' ;
509
+ return 'id = ? ' ;
510
510
}
511
- return '` fqdn` = ? ' ;
511
+ return 'fqdn = ? ' ;
512
512
}
513
513
514
514
/**
0 commit comments