Passed
Push — main ( 0ad8f5...e78d26 )
by Thierry
28:37 queued 19:50
created

Grammar::getCreateIndexQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\Grammar as AbstractGrammar;
6
7
class Grammar extends AbstractGrammar
8
{
9
    /**
10
     * @inheritDoc
11
     */
12
    public function escapeId(string $idf)
13
    {
14
        return '"' . str_replace('"', '""', $idf) . '"';
15
    }
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function getAutoIncrementModifier()
21
    {
22
        return " PRIMARY KEY AUTOINCREMENT";
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function getCreateTableQuery(string $table, bool $autoIncrement, string $style)
0 ignored issues
show
Unused Code introduced by
The parameter $autoIncrement 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

28
    public function getCreateTableQuery(string $table, /** @scrutinizer ignore-unused */ bool $autoIncrement, string $style)

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...
Unused Code introduced by
The parameter $style 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

28
    public function getCreateTableQuery(string $table, bool $autoIncrement, /** @scrutinizer ignore-unused */ string $style)

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...
29
    {
30
        $query = $this->driver->result("SELECT sql FROM sqlite_master " .
31
            "WHERE type IN ('table', 'view') AND name = " . $this->driver->quote($table));
32
        foreach ($this->driver->indexes($table) as $name => $index) {
33
            if ($name == '') {
34
                continue;
35
            }
36
            $columns = implode(", ", array_map(function ($key) {
37
                return $this->escapeId($key);
38
            }, $index->columns));
39
            $query .= ";\n\n" . $this->getCreateIndexQuery($table, $index->type, $name, "($columns)");
40
        }
41
        return $query;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function getCreateIndexQuery(string $table, string $type, string $name, string $columns)
48
    {
49
        return "CREATE $type " . ($type != "INDEX" ? "INDEX " : "") .
50
            $this->escapeId($name != "" ? $name : uniqid($table . "_")) .
51
            " ON " . $this->escapeTableName($table) . " $columns";
0 ignored issues
show
Bug introduced by
The method escapeTableName() does not exist on Lagdo\DbAdmin\Driver\Sqlite\Db\Grammar. ( Ignorable by Annotation )

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

51
            " ON " . $this->/** @scrutinizer ignore-call */ escapeTableName($table) . " $columns";

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getTruncateTableQuery(string $table)
58
    {
59
        return "DELETE FROM " . $this->escapeTableName($table);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getCreateTriggerQuery(string $table)
66
    {
67
        $query = "SELECT sql || ';;\n' FROM sqlite_master WHERE type = 'trigger' AND tbl_name = " .
68
            $this->driver->quote($table);
69
        return implode($this->driver->values($query));
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    protected function queryRegex()
76
    {
77
        return '\\s*|[\'"`[]|/\*|-- |$';
78
    }
79
}
80