Passed
Push — main ( 61fcfb...6f7eb5 )
by Thierry
01:40
created

Grammar::sqlForCreateTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
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
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
13
14
abstract class Grammar implements GrammarInterface
15
{
16
    /**
17
     * @var DriverInterface
18
     */
19
    protected $driver;
20
21
    /**
22
     * @var UtilInterface
23
     */
24
    protected $util;
25
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    protected $trans;
30
31
    /**
32
     * @var ConnectionInterface
33
     */
34
    protected $connection;
35
36
    /**
37
     * The constructor
38
     *
39
     * @param DriverInterface $driver
40
     * @param UtilInterface $util
41
     * @param TranslatorInterface $trans
42
     * @param ConnectionInterface $connection
43
     */
44
    public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans, ConnectionInterface $connection)
45
    {
46
        $this->driver = $driver;
47
        $this->util = $util;
48
        $this->trans = $trans;
49
        $this->connection = $connection;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function escapeId(string $idf)
56
    {
57
        return $idf;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function unescapeId(string $idf)
64
    {
65
        $last = substr($idf, -1);
66
        return str_replace($last . $last, $last, substr($idf, 1, -1));
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function table(string $idf)
73
    {
74
        return $this->escapeId($idf);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function formatForeignKey(ForeignKeyEntity $foreignKey)
81
    {
82
        $database = $foreignKey->database;
83
        $schema = $foreignKey->schema;
84
        $onActions = $this->driver->actions();
85
        $sources = implode(', ', array_map(function ($idf) {
86
            return $this->escapeId($idf);
87
        }, $foreignKey->source));
88
        $targets = implode(', ', array_map(function ($idf) {
89
            return $this->escapeId($idf);
90
        }, $foreignKey->target));
91
92
        $query = " FOREIGN KEY ($sources) REFERENCES ";
93
        if ($database != '' && $database != $this->driver->database()) {
94
            $query .= $this->escapeId($database) . '.';
95
        }
96
        if ($schema != '' && $schema != $this->driver->schema()) {
97
            $query .= $this->escapeId($schema) . '.';
98
        }
99
        $query .= $this->table($foreignKey->table) . " ($targets)";
100
        if (preg_match("~^($onActions)\$~", $foreignKey->onDelete)) {
101
            $query .= " ON DELETE {$foreignKey->onDelete}";
102
        }
103
        if (preg_match("~^($onActions)\$~", $foreignKey->onUpdate)) {
104
            $query .= " ON UPDATE {$foreignKey->onUpdate}";
105
        }
106
107
        return $query;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function quote($string)
114
    {
115
        return $this->connection->quote($string);
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function buildSelectQuery(TableSelectEntity $select)
122
    {
123
        $isGroup = (count($select->group) < count($select->fields));
124
        $query = '';
125
        if ($this->driver->jush() === 'sql' && ($select->page) &&
126
            ($select->limit) && !empty($select->group) && $isGroup) {
127
            $query = 'SQL_CALC_FOUND_ROWS ';
128
        }
129
        $query .= \implode(', ', $select->fields) . "\nFROM " . $this->table($select->table);
130
        $limit = +$select->limit;
131
        $offset = $select->page ? $limit * $select->page : 0;
132
133
        return 'SELECT' . $this->limit($query, $select->clauses, $limit, $offset, "\n");
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function applySqlFunction(string $function, string $column)
140
    {
141
        return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" :
142
            ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function defaultValue($field)
149
    {
150
        $default = $field->default;
151
        return ($default === null ? "" : " DEFAULT " .
152
            (preg_match('~char|binary|text|enum|set~', $field->type) ||
153
            preg_match('~^(?![a-z])~i', $default) ? $this->quote($default) : $default));
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = " ")
160
    {
161
        return "";
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function limitToOne(string $table, string $query, string $where, string $separator = "\n")
168
    {
169
        return $this->limit($query, $where, 1, 0, $separator);
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function convertField(TableFieldEntity $field)
176
    {
177
        return '';
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function unconvertField(TableFieldEntity $field, string $value)
184
    {
185
        return $value;
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function convertFields(array $columns, array $fields, array $select = [])
192
    {
193
        $clause = "";
194
        foreach ($columns as $key => $val) {
195
            if (!empty($select) && !in_array($this->escapeId($key), $select)) {
196
                continue;
197
            }
198
            $as = $this->convertField($fields[$key]);
199
            if ($as) {
200
                $clause .= ", $as AS " . $this->escapeId($key);
201
            }
202
        }
203
        return $clause;
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function countRowsSql(string $table, array $where, bool $isGroup, array $groups)
210
    {
211
        $query = " FROM " . $this->table($table) . ($where ? " WHERE " . implode(" AND ", $where) : "");
212
        return ($isGroup && ($this->driver->jush() == "sql" || count($groups) == 1)
213
            ? "SELECT COUNT(DISTINCT " . implode(", ", $groups) . ")$query"
214
            : "SELECT COUNT(*)" . ($isGroup ? " FROM (SELECT 1$query GROUP BY " . implode(", ", $groups) . ") x" : $query)
215
        );
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221
    public function sqlForCreateTable(string $table, bool $autoIncrement, string $style)
222
    {
223
        return '';
224
    }
225
226
    /**
227
     * @inheritDoc
228
     */
229
    public function sqlForCreateIndex(string $table, string $type, string $name, string $columns)
230
    {
231
        return '';
232
    }
233
234
    /**
235
     * @inheritDoc
236
     */
237
    public function sqlForUseDatabase(string $database)
238
    {
239
        return "";
240
    }
241
242
    /**
243
     * @inheritDoc
244
     */
245
    public function sqlForForeignKeys(string $table)
246
    {
247
        return '';
248
    }
249
250
    /**
251
     * @inheritDoc
252
     */
253
    public function sqlForTruncateTable(string $table)
254
    {
255
        return '';
256
    }
257
258
    /**
259
     * @inheritDoc
260
     */
261
    public function sqlForCreateTrigger(string $table)
262
    {
263
        return '';
264
    }
265
266
    /**
267
     * @inheritDoc
268
     */
269
    public function autoIncrement()
270
    {
271
        return "";
272
    }
273
}
274