AbstractDialect::getSelectSyntax()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 7
Ratio 25.93 %

Importance

Changes 0
Metric Value
dl 7
loc 27
c 0
b 0
f 0
rs 8.5806
cc 4
eloc 16
nc 4
nop 1
1
<?php
2
3
namespace Graze\DataDb\Dialect;
4
5
use Graze\DataDb\Formatter\SyntaxFormatter;
6
use Graze\DataDb\Formatter\SyntaxFormatterInterface;
7
use Graze\DataDb\SourceTableNodeInterface;
8
use Graze\DataDb\TableNodeInterface;
9
use Graze\DataFile\Helper\Builder\BuilderTrait;
10
11
abstract class AbstractDialect implements DialectInterface
12
{
13
    use BuilderTrait;
14
15
    /**
16
     * @var SyntaxFormatterInterface
17
     */
18
    protected $formatter;
19
20
    /**
21
     * MysqlDialect constructor.
22
     *
23
     * @param SyntaxFormatterInterface|null $formatter
24
     */
25
    public function __construct(SyntaxFormatterInterface $formatter = null)
26
    {
27
        $this->formatter = $formatter;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    abstract public function getIdentifierQuote();
34
35
    /**
36
     * @param string $syntax
37
     * @param array  $params
38
     *
39
     * @return string
40
     */
41
    protected function format($syntax, array $params = [])
42
    {
43
        if (!$this->formatter) {
44
            $this->formatter = $this->getBuilder()->build(SyntaxFormatter::class);
45
            $this->formatter->setIdentifierQuote($this->getIdentifierQuote());
46
        }
47
48
        return $this->formatter->format($syntax, $params);
49
    }
50
51
    /**
52
     * @param TableNodeInterface $table
53
     * @param string             $column
54
     *
55
     * @return array
56
     */
57 View Code Duplication
    public function getDropColumn(TableNodeInterface $table, $column)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        return [
60
            $this->format(
61
                'ALTER TABLE {table:schema|q}.{table:table|q} DROP COLUMN {column|q}',
62
                ['table' => $table, 'column' => $column,]
63
            ),
64
            [],
65
        ];
66
    }
67
68
    /**
69
     * @param TableNodeInterface $table
70
     *
71
     * @return array
72
     */
73
    public function getDoesTableExist(TableNodeInterface $table)
74
    {
75
        return [
76
            'SELECT
77
                table_name
78
            FROM
79
                information_schema.tables
80
            WHERE
81
                table_schema = ?
82
                AND table_name = ?',
83
            [
84
                $table->getSchema(),
85
                $table->getTable(),
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @param TableNodeInterface $table
92
     * @param string|null        $where
93
     *
94
     * @return array
95
     */
96 View Code Duplication
    public function getDeleteFromTable(TableNodeInterface $table, $where = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        return [
99
            $this->format(
100
                'DELETE FROM {table:schema|q}.{table:table|q}
101
                {where}',
102
                [
103
                    'table' => $table,
104
                    'where' => ($where ? 'WHERE ' . $where : ''),
105
                ]
106
            ),
107
            [],
108
        ];
109
    }
110
111
    /**
112
     * @param TableNodeInterface $from
113
     * @param TableNodeInterface $to
114
     *
115
     * @return array
116
     */
117
    public function getCopyTable(TableNodeInterface $from, TableNodeInterface $to)
118
    {
119 View Code Duplication
        if ($from->getColumns()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            $fromColumns = implode(',', array_map(function ($column) {
121
                return $this->format('{column|q}', ['column' => $column]);
122
            }, $from->getColumns()));
123
        } else {
124
            $fromColumns = '*';
125
        }
126
127 View Code Duplication
        if ($to->getColumns()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            $toColumns = sprintf(
129
                ' (%s)',
130
                implode(',', array_map(function ($column) {
131
                    return $this->format('{column|q}', ['column' => $column]);
132
                }, $to->getColumns()))
133
            );
134
        } else {
135
            $toColumns = '';
136
        }
137
138
        return [
139
            $this->format(
140
                'INSERT INTO {to:schema|q}.{to:table|q}{toColumns}
141
                SELECT {fromColumns} FROM {from:schema|q}.{from:table|q}',
142
                [
143
                    'from'        => $from,
144
                    'to'          => $to,
145
                    'fromColumns' => $fromColumns,
146
                    'toColumns'   => $toColumns,
147
                ]
148
            ),
149
            [],
150
        ];
151
    }
152
153
    /**
154
     * @param TableNodeInterface $table
155
     *
156
     * @return array [sql, params]
157
     */
158
    public function getSelectSyntax(TableNodeInterface $table)
159
    {
160 View Code Duplication
        if ($table->getColumns()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
            $fromColumns = implode(',', array_map(function ($column) {
162
                return $this->format('{column|q}', ['column' => $column]);
163
            }, $table->getColumns()));
164
        } else {
165
            $fromColumns = '*';
166
        }
167
168
        $where = '';
169
        if ($table instanceof SourceTableNodeInterface && $table->getWhere()) {
170
            $where = ' WHERE (' . $table->getWhere() . ')';
171
        }
172
173
        return [
174
            $this->format(
175
                'SELECT {fromColumns} FROM {from:schema|q}.{from:table|q}{where}',
176
                [
177
                    'from'        => $table,
178
                    'fromColumns' => $fromColumns,
179
                    'where'       => $where,
180
                ]
181
            ),
182
            [],
183
        ];
184
    }
185
186
    /**
187
     * @param TableNodeInterface $table
188
     * @param string[]           $rows
189
     *
190
     * @return array [sql, params]
191
     */
192
    public function getInsertSyntax(TableNodeInterface $table, array $rows)
193
    {
194 View Code Duplication
        if ($table->getColumns()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
            $toColumns = sprintf(
196
                ' (%s)',
197
                implode(',', array_map(function ($column) {
198
                    return $this->format('{column|q}', ['column' => $column]);
199
                }, $table->getColumns()))
200
            );
201
        } else {
202
            $toColumns = '';
203
        }
204
205
        $bind = [];
206
        $insert = [];
207
        foreach ($rows as $row) {
208
            $insert[] = '(' . substr(str_repeat('?,', count($row)), 0, -1) . ')';
209
            $bind = array_merge($bind, array_values($row));
210
        }
211
212
        $insertSql = implode(',', $insert);
213
214
        return [
215
            $this->format(
216
                'INSERT INTO {table:schema|q}.{table:table|q}{toColumns} VALUES {insertSql}',
217
                compact('table', 'toColumns', 'insertSql')
218
            ),
219
            $bind,
220
        ];
221
    }
222
}
223