Passed
Push — main ( af1024...b746de )
by Thierry
02:04
created

Grammar::quote()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity;
7
use Lagdo\DbAdmin\Driver\Entity\ForeignKeyEntity;
8
9
use Lagdo\DbAdmin\Driver\DriverInterface;
10
use Lagdo\DbAdmin\Driver\UtilInterface;
11
use Lagdo\DbAdmin\Driver\TranslatorInterface;
12
13
abstract class Grammar implements GrammarInterface
14
{
15
    /**
16
     * @var DriverInterface
17
     */
18
    protected $driver;
19
20
    /**
21
     * @var UtilInterface
22
     */
23
    protected $util;
24
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    protected $trans;
29
30
    /**
31
     * @var ConnectionInterface
32
     */
33
    protected $connection;
34
35
    /**
36
     * The constructor
37
     *
38
     * @param DriverInterface $driver
39
     * @param UtilInterface $util
40
     * @param TranslatorInterface $trans
41
     * @param ConnectionInterface $connection
42
     */
43
    public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans, ConnectionInterface $connection)
44
    {
45
        $this->driver = $driver;
46
        $this->util = $util;
47
        $this->trans = $trans;
48
        $this->connection = $connection;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function escapeId(string $idf)
55
    {
56
        return $idf;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function unescapeId(string $idf)
63
    {
64
        $last = substr($idf, -1);
65
        return str_replace($last . $last, $last, substr($idf, 1, -1));
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function table(string $idf)
72
    {
73
        return $this->escapeId($idf);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function formatForeignKey(ForeignKeyEntity $foreignKey)
80
    {
81
        $database = $foreignKey->database;
82
        $schema = $foreignKey->schema;
83
        $onActions = $this->driver->actions();
84
        $sources = implode(', ', array_map(function ($idf) {
85
            return $this->escapeId($idf);
86
        }, $foreignKey->source));
87
        $targets = implode(', ', array_map(function ($idf) {
88
            return $this->escapeId($idf);
89
        }, $foreignKey->target));
90
91
        $query = " FOREIGN KEY ($sources) REFERENCES ";
92
        if ($database != '' && $database != $this->driver->database()) {
93
            $query .= $this->escapeId($database) . '.';
94
        }
95
        if ($schema != '' && $schema != $this->driver->schema()) {
96
            $query .= $this->escapeId($schema) . '.';
97
        }
98
        $query .= $this->table($foreignKey->table) . " ($targets)";
99
        if (preg_match("~^($onActions)\$~", $foreignKey->onDelete)) {
100
            $query .= " ON DELETE {$foreignKey->onDelete}";
101
        }
102
        if (preg_match("~^($onActions)\$~", $foreignKey->onUpdate)) {
103
            $query .= " ON UPDATE {$foreignKey->onUpdate}";
104
        }
105
106
        return $query;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function buildSelectQuery(TableSelectEntity $select)
113
    {
114
        $query = \implode(', ', $select->fields) . ' FROM ' . $this->table($select->table);
115
        $limit = +$select->limit;
116
        $offset = $select->page ? $limit * $select->page : 0;
117
118
        return 'SELECT' . $this->limit($query, $select->clauses, $limit, $offset, "\n");
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function defaultValue($field)
125
    {
126
        $default = $field->default;
127
        return ($default === null ? '' : ' DEFAULT ' .
128
            (preg_match('~char|binary|text|enum|set~', $field->type) ||
129
            preg_match('~^(?![a-z])~i', $default) ? $this->driver->quote($default) : $default));
0 ignored issues
show
Bug introduced by
The method quote() does not exist on Lagdo\DbAdmin\Driver\DriverInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Lagdo\DbAdmin\Driver\DriverInterface. ( Ignorable by Annotation )

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

129
            preg_match('~^(?![a-z])~i', $default) ? $this->driver->/** @scrutinizer ignore-call */ quote($default) : $default));
Loading history...
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = ' ')
136
    {
137
        return '';
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function convertField(TableFieldEntity $field)
144
    {
145
        return '';
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function unconvertField(TableFieldEntity $field, string $value)
152
    {
153
        return $value;
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function convertFields(array $columns, array $fields, array $select = [])
160
    {
161
        $clause = '';
162
        foreach ($columns as $key => $val) {
163
            if (!empty($select) && !in_array($this->escapeId($key), $select)) {
164
                continue;
165
            }
166
            $as = $this->convertField($fields[$key]);
167
            if ($as) {
168
                $clause .= ', $as AS ' . $this->escapeId($key);
169
            }
170
        }
171
        return $clause;
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function countRowsSql(string $table, array $where, bool $isGroup, array $groups)
178
    {
179
        $query = ' FROM ' . $this->table($table) . ($where ? ' WHERE ' . implode(' AND ', $where) : '');
180
        return ($isGroup && ($this->driver->jush() == 'sql' || count($groups) == 1)
181
            ? 'SELECT COUNT(DISTINCT ' . implode(', ', $groups) . ")$query"
182
            : 'SELECT COUNT(*)' . ($isGroup ? " FROM (SELECT 1$query GROUP BY " . implode(', ', $groups) . ') x' : $query)
183
        );
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    public function sqlForCreateTable(string $table, bool $autoIncrement, string $style)
190
    {
191
        return '';
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function sqlForCreateIndex(string $table, string $type, string $name, string $columns)
198
    {
199
        return '';
200
    }
201
202
    /**
203
     * @inheritDoc
204
     */
205
    public function sqlForUseDatabase(string $database)
206
    {
207
        return '';
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function sqlForForeignKeys(string $table)
214
    {
215
        return '';
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221
    public function sqlForTruncateTable(string $table)
222
    {
223
        return '';
224
    }
225
226
    /**
227
     * @inheritDoc
228
     */
229
    public function sqlForCreateTrigger(string $table)
230
    {
231
        return '';
232
    }
233
234
    /**
235
     * @inheritDoc
236
     */
237
    public function autoIncrement()
238
    {
239
        return '';
240
    }
241
}
242