Passed
Push — main ( 7ae905...1918cf )
by Thierry
01:35
created

TableTrait::makeStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
6
use Lagdo\DbAdmin\Driver\Entity\IndexEntity;
7
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
8
9
trait TableTrait
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function isView(TableEntity $tableStatus)
15
    {
16
        return $tableStatus->engine == 'view';
17
    }
18
19
    /**
20
     * @param string $table
21
     *
22
     * @return array
23
     */
24
    private function queryStatus(string $table = '')
25
    {
26
        $query = "SELECT name AS Name, type AS Engine, 'rowid' AS Oid, '' AS Auto_increment " .
27
            "FROM sqlite_master WHERE type IN ('table', 'view') " .
28
            ($table != "" ? "AND name = " . $this->driver->quote($table) : "ORDER BY name");
29
        return $this->driver->rows($query);
30
    }
31
32
    /**
33
     * @param array $row
34
     *
35
     * @return TableEntity
36
     */
37
    private function makeStatus(array $row)
38
    {
39
        $status = new TableEntity($row['Name']);
40
        $status->engine = $row['Engine'];
41
        $status->oid = $row['Oid'];
42
        // $status->Auto_increment = $row['Auto_increment'];
43
        $query = 'SELECT COUNT(*) FROM ' . $this->driver->escapeId($row['Name']);
44
        $status->rows = $this->connection->result($query);
45
46
        return $status;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function tableStatus(string $table, bool $fast = false)
0 ignored issues
show
Unused Code introduced by
The parameter $fast is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
    public function tableStatus(string $table, /** @scrutinizer ignore-unused */ bool $fast = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        $rows = $this->queryStatus($table);
55
        if (!($row = reset($rows))) {
56
            return null;
57
        }
58
        return $this->makeStatus($row);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function tableStatuses(bool $fast = false)
0 ignored issues
show
Unused Code introduced by
The parameter $fast is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    public function tableStatuses(/** @scrutinizer ignore-unused */ bool $fast = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        $tables = [];
67
        $rows = $this->queryStatus();
68
        foreach ($rows as $row) {
69
            $tables[$row['Name']] = $this->makeStatus($row);
70
        }
71
        return $tables;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function tableNames()
78
    {
79
        $tables = [];
80
        $rows = $this->queryStatus();
81
        foreach ($rows as $row) {
82
            $tables[] = $row['Name'];
83
        }
84
        return $tables;
85
    }
86
87
    /**
88
     * @param string $table
89
     * @param ConnectionInterface $connection
90
     *
91
     * @return IndexEntity|null
92
     */
93
    private function queryPrimaryIndex(string $table, ConnectionInterface $connection)
94
    {
95
        $primaryIndex = null;
96
        $query = "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . $this->driver->quote($table);
97
        $result = $connection->result($query);
98
        if (preg_match('~\bPRIMARY\s+KEY\s*\((([^)"]+|"[^"]*"|`[^`]*`)++)~i', $result, $match)) {
99
            $primaryIndex = new IndexEntity();
100
            $primaryIndex->type = "PRIMARY";
101
            preg_match_all('~((("[^"]*+")+|(?:`[^`]*+`)+)|(\S+))(\s+(ASC|DESC))?(,\s*|$)~i',
102
                $match[1], $matches, PREG_SET_ORDER);
103
            foreach ($matches as $match) {
104
                $primaryIndex->columns[] = $this->driver->unescapeId($match[2]) . $match[4];
105
                $primaryIndex->descs[] = (preg_match('~DESC~i', $match[5]) ? '1' : null);
106
            }
107
        }
108
        return $primaryIndex;
109
    }
110
111
    /**
112
     * @param string $table
113
     * @param ConnectionInterface $connection
114
     *
115
     * @return IndexEntity
116
     */
117
    private function makePrimaryIndex(string $table, ConnectionInterface $connection)
118
    {
119
        $primaryIndex = $this->queryPrimaryIndex($table, $connection);
120
        if ($primaryIndex !== null) {
121
            return $primaryIndex;
122
        }
123
        foreach ($this->fields($table) as $name => $field) {
0 ignored issues
show
Bug introduced by
It seems like fields() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        foreach ($this->/** @scrutinizer ignore-call */ fields($table) as $name => $field) {
Loading history...
124
            if (!$field->primary) {
125
                continue;
126
            }
127
            if ($primaryIndex === null) {
128
                $primaryIndex = new IndexEntity();
129
            }
130
            $primaryIndex->type = "PRIMARY";
131
            $primaryIndex->columns = [$name];
132
            $primaryIndex->lengths = [];
133
            $primaryIndex->descs = [null];
134
        }
135
        return $primaryIndex;
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function tableHelp(string $name)
142
    {
143
        if ($name == "sqlite_sequence") {
144
            return "fileformat2.html#seqtab";
145
        }
146
        if ($name == "sqlite_master") {
147
            return "fileformat2.html#$name";
148
        }
149
    }
150
}
151