QueryRelationManager::createQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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