Passed
Branch main (217991)
by Thierry
02:18
created

Grammar::sqlForCreateTrigger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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