Passed
Branch main (27f97d)
by Thierry
04:43 queued 03:05
created

Grammar::sqlForCreateIndex()   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
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 5
rs 10
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 autoIncrement()
21
    {
22
        return " PRIMARY KEY AUTOINCREMENT";
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function sqlForCreateTable(string $table, bool $autoIncrement, string $style)
29
    {
30
        $query = $this->connection->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->sqlForCreateIndex($table, $index->type, $name, "($columns)");
40
        }
41
        return $query;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function sqlForCreateIndex(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->table($table) . " $columns";
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function sqlForTruncateTable(string $table)
58
    {
59
        return "DELETE FROM " . $this->table($table);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function sqlForCreateTrigger(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