QueryRelationManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createQuery() 0 3 1
A getPrimaryKey() 0 13 2
A getTableName() 0 3 1
A getTableFields() 0 13 2
1
<?php
2
3
namespace Smoren\QueryRelationManager\Pdo;
4
5
use Smoren\QueryRelationManager\Base\QueryRelationManagerBase;
6
use Smoren\QueryRelationManager\Base\QueryWrapperInterface;
7
use Smoren\QueryRelationManager\Base\QueryRelationManagerException;
8
9
/**
10
 * QueryRelationManager implementation for PDO
11
 * @author Smoren <[email protected]>
12
 */
13
class QueryRelationManager extends QueryRelationManagerBase
14
{
15
    /**
16
     * @inheritDoc
17
     */
18
    protected function getTableName(string $className): string
19
    {
20
        return $className;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    protected function createQuery(): QueryWrapperInterface
27
    {
28
        return new QueryWrapper();
29
    }
30
31
    /**
32
     * @inheritDoc
33
     * @throws QueryRelationManagerException
34
     */
35
    protected function getTableFields(string $className): array
36
    {
37
        $qw = new QueryWrapper();
38
        $qw->setRawSql('SHOW COLUMNS FROM '.addslashes($className));
39
        $rows = $qw->all();
40
41
        /** @var array<string> $result */
42
        $result = [];
43
        foreach($rows as $row) {
44
            $result[] = $row['Field'];
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @throws QueryRelationManagerException
53
     */
54
    protected function getPrimaryKey(string $className): array
55
    {
56
        $qw = new QueryWrapper();
57
        $qw->setRawSql("SHOW COLUMNS FROM `".addslashes($className)."` WHERE `Key` = 'PRI'");
58
        $rows = $qw->all();
59
60
        /** @var array<string> $result */
61
        $result = [];
62
        foreach($rows as $row) {
63
            $result[] = $row['Field'];
64
        }
65
66
        return $result;
67
    }
68
}
69