Passed
Branch main (cffb1a)
by Thierry
01:39
created

Grammar::queryRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 autoIncrement()
24
    {
25
        $autoIncrementIndex = " PRIMARY KEY";
26
        // don't overwrite primary key by auto increment
27
        $query = $this->util->input();
28
        $table = $query->getTable();
29
        $fields = $query->getFields();
30
        $autoIncrementField = $query->getAutoIncrementField();
31
        if ($table != "" && $autoIncrementField) {
32
            foreach ($this->driver->indexes($table) as $index) {
33
                if (in_array($fields[$autoIncrementField]["orig"], $index->columns, true)) {
34
                    $autoIncrementIndex = "";
35
                    break;
36
                }
37
                if ($index->type == "PRIMARY") {
38
                    $autoIncrementIndex = " UNIQUE";
39
                }
40
            }
41
        }
42
        return " AUTO_INCREMENT$autoIncrementIndex";
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function buildSelectQuery(TableSelectEntity $select)
49
    {
50
        $prefix = '';
51
        if (($select->page) && ($select->limit) && !empty($select->group) &&
52
            count($select->group) < count($select->fields)) {
53
            $prefix = 'SQL_CALC_FOUND_ROWS ';
54
        }
55
56
        return $prefix . parent::buildSelectQuery($select);
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function sqlForCreateTable(string $table, bool $autoIncrement, string $style)
63
    {
64
        $query = $this->connection->result("SHOW CREATE TABLE " . $this->table($table), 1);
65
        if (!$autoIncrement) {
66
            $query = preg_replace('~ AUTO_INCREMENT=\d+~', '', $query); //! skip comments
67
        }
68
        return $query;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function sqlForTruncateTable(string $table)
75
    {
76
        return "TRUNCATE " . $this->table($table);
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function sqlForUseDatabase(string $database)
83
    {
84
        return "USE " . $this->escapeId($database);
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function sqlForCreateTrigger(string $table)
91
    {
92
        $query = "";
93
        foreach ($this->driver->rows("SHOW TRIGGERS LIKE " .
94
            $this->driver->quote(addcslashes($table, "%_\\")), null) as $row) {
95
            $query .= "\nCREATE TRIGGER " . $this->escapeId($row["Trigger"]) .
96
                " $row[Timing] $row[Event] ON " . $this->table($row["Table"]) .
97
                " FOR EACH ROW\n$row[Statement];;\n";
98
        }
99
        return $query;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function convertField(TableFieldEntity $field)
106
    {
107
        if (preg_match("~binary~", $field->type)) {
108
            return "HEX(" . $this->escapeId($field->name) . ")";
109
        }
110
        if ($field->type == "bit") {
111
            return "BIN(" . $this->escapeId($field->name) . " + 0)"; // + 0 is required outside MySQLnd
112
        }
113
        if (preg_match("~geometry|point|linestring|polygon~", $field->type)) {
114
            return ($this->driver->minVersion(8) ? "ST_" : "") . "AsWKT(" . $this->escapeId($field->name) . ")";
115
        }
116
        return '';
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function unconvertField(TableFieldEntity $field, string $value)
123
    {
124
        if (preg_match("~binary~", $field->type)) {
125
            $value = "UNHEX($value)";
126
        }
127
        if ($field->type == "bit") {
128
            $value = "CONV($value, 2, 10) + 0";
129
        }
130
        if (preg_match("~geometry|point|linestring|polygon~", $field->type)) {
131
            $value = ($this->driver->minVersion(8) ? "ST_" : "") . "GeomFromText($value, SRID($field[field]))";
132
        }
133
        return $value;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function connectionId()
140
    {
141
        return "SELECT CONNECTION_ID()";
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    protected function queryRegex()
148
    {
149
        return '\\s*|[\'"`#]|/\*|-- |$';
150
    }
151
}
152