Passed
Push — main ( 6dd21f...0366e5 )
by Thierry
19:47 queued 10:52
created

Grammar::getCreateTableQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity;
7
8
use Lagdo\DbAdmin\Driver\Db\Grammar as AbstractGrammar;
9
10
class Grammar extends AbstractGrammar
11
{
12
    /**
13
     * @inheritDoc
14
     */
15
    public function escapeId(string $idf)
16
    {
17
        return "`" . str_replace("`", "``", $idf) . "`";
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function getAutoIncrementModifier()
24
    {
25
        $autoIncrementIndex = " PRIMARY KEY";
26
        // don't overwrite primary key by auto increment
27
        $table = $this->utils->input->getTable();
0 ignored issues
show
Bug introduced by
The property utils does not exist on Lagdo\DbAdmin\Driver\MySql\Db\Grammar. Did you mean util?
Loading history...
28
        $fields = $this->utils->input->getFields();
29
        $autoIncrementField = $this->utils->input->getAutoIncrementField();
30
        if ($table != "" && $autoIncrementField) {
31
            foreach ($this->driver->indexes($table) as $index) {
32
                if (in_array($fields[$autoIncrementField]["orig"], $index->columns, true)) {
33
                    $autoIncrementIndex = "";
34
                    break;
35
                }
36
                if ($index->type == "PRIMARY") {
37
                    $autoIncrementIndex = " UNIQUE";
38
                }
39
            }
40
        }
41
        return " AUTO_INCREMENT$autoIncrementIndex";
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function buildSelectQuery(TableSelectEntity $select)
48
    {
49
        $prefix = '';
50
        if (($select->page) && ($select->limit) && !empty($select->group) &&
51
            count($select->group) < count($select->fields)) {
52
            $prefix = 'SQL_CALC_FOUND_ROWS ';
53
        }
54
55
        return $prefix . parent::buildSelectQuery($select);
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function getCreateTableQuery(string $table, bool $autoIncrement, string $style)
0 ignored issues
show
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

61
    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...
62
    {
63
        $query = $this->driver->result("SHOW CREATE TABLE " . $this->escapeTableName($table), 1);
0 ignored issues
show
Bug introduced by
The method escapeTableName() does not exist on Lagdo\DbAdmin\Driver\MySql\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

63
        $query = $this->driver->result("SHOW CREATE TABLE " . $this->/** @scrutinizer ignore-call */ escapeTableName($table), 1);

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...
64
        if (!$autoIncrement) {
65
            $query = preg_replace('~ AUTO_INCREMENT=\d+~', '', $query); //! skip comments
66
        }
67
        return $query;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getTruncateTableQuery(string $table)
74
    {
75
        return "TRUNCATE " . $this->escapeTableName($table);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getUseDatabaseQuery(string $database)
82
    {
83
        return "USE " . $this->escapeId($database);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function getCreateTriggerQuery(string $table)
90
    {
91
        $query = "";
92
        foreach ($this->driver->rows("SHOW TRIGGERS LIKE " .
93
            $this->driver->quote(addcslashes($table, "%_\\")), null) as $row) {
0 ignored issues
show
Unused Code introduced by
The call to Lagdo\DbAdmin\Driver\DriverInterface::rows() has too many arguments starting with null. ( Ignorable by Annotation )

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

93
        foreach ($this->driver->/** @scrutinizer ignore-call */ rows("SHOW TRIGGERS LIKE " .

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
            $query .= "\nCREATE TRIGGER " . $this->escapeId($row["Trigger"]) .
95
                " $row[Timing] $row[Event] ON " . $this->escapeTableName($row["Table"]) .
96
                " FOR EACH ROW\n$row[Statement];;\n";
97
        }
98
        return $query;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function convertField(TableFieldEntity $field)
105
    {
106
        if (preg_match("~binary~", $field->type)) {
107
            return "HEX(" . $this->escapeId($field->name) . ")";
108
        }
109
        if ($field->type == "bit") {
110
            return "BIN(" . $this->escapeId($field->name) . " + 0)"; // + 0 is required outside MySQLnd
111
        }
112
        if (preg_match("~geometry|point|linestring|polygon~", $field->type)) {
113
            return ($this->driver->minVersion(8) ? "ST_" : "") . "AsWKT(" . $this->escapeId($field->name) . ")";
114
        }
115
        return '';
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function unconvertField(TableFieldEntity $field, string $value)
122
    {
123
        if (preg_match("~binary~", $field->type)) {
124
            $value = "UNHEX($value)";
125
        }
126
        if ($field->type == "bit") {
127
            $value = "CONV($value, 2, 10) + 0";
128
        }
129
        if (preg_match("~geometry|point|linestring|polygon~", $field->type)) {
130
            $value = ($this->driver->minVersion(8) ? "ST_" : "") . "GeomFromText($value, SRID($field[field]))";
131
        }
132
        return $value;
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    // public function connectionId()
139
    // {
140
    //     return "SELECT CONNECTION_ID()";
141
    // }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    protected function queryRegex()
147
    {
148
        return '\\s*|[\'"`#]|/\*|-- |$';
149
    }
150
}
151