GenericBuilder::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 6/3/14
5
 * Time: 12:07 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Sql\QueryBuilder\Builder;
12
13
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
14
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
15
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
16
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
17
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
18
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
19
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
20
21
/**
22
 * Class Generic.
23
 */
24
class GenericBuilder implements BuilderInterface
25
{
26
    /**
27
     * The placeholder parameter bag.
28
     *
29
     * @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter
30
     */
31
    private $placeholderWriter;
32
33
    /**
34
     * The Where writer.
35
     *
36
     * @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter
37
     */
38
    private $whereWriter;
39
40
    /**
41
     * The SQL formatter.
42
     *
43
     * @var \NilPortugues\Sql\QueryFormatter\Formatter
44
     */
45
    private $sqlFormatter;
46
47
    /**
48
     * Class namespace for the query pretty output formatter.
49
     * Required to create the instance only if required.
50
     *
51
     * @var string
52
     */
53
    private $sqlFormatterClass = 'NilPortugues\Sql\QueryFormatter\Formatter';
54
55
    /**
56
     * Array holding the writers for each query part. Methods are called upon request and stored in
57
     * the $queryWriterInstances array.
58
     *
59
     * @var array
60
     */
61
    private $queryWriterArray = [
62
        'SELECT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createSelectWriter',
63
        'INSERT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createInsertWriter',
64
        'UPDATE' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUpdateWriter',
65
        'DELETE' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createDeleteWriter',
66
        'INTERSECT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createIntersectWriter',
67
        'MINUS' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createMinusWriter',
68
        'UNION' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUnionWriter',
69
        'UNION ALL' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUnionAllWriter',
70
    ];
71
72
    /**
73
     * Array that stores instances of query writers.
74
     *
75
     * @var array
76
     */
77
    private $queryWriterInstances = [
78
        'SELECT' => null,
79
        'INSERT' => null,
80
        'UPDATE' => null,
81
        'DELETE' => null,
82
        'INTERSECT' => null,
83
        'MINUS' => null,
84
        'UNION' => null,
85
        'UNION ALL' => null,
86
    ];
87
88
    /**
89
     * Creates writers.
90
     */
91
    public function __construct()
92
    {
93
        $this->placeholderWriter = WriterFactory::createPlaceholderWriter();
94
    }
95
96
    /**
97
     * @param string $table
98
     * @param array  $columns
99
     *
100
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\Select
101
     */
102
    public function select($table = null, array $columns = null)
103
    {
104
        return $this->injectBuilder(QueryFactory::createSelect($table, $columns));
105
    }
106
107
    /**
108
     * @param \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
109
     *
110
     *@return \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
111
     */
112
    protected function injectBuilder(AbstractBaseQuery $query)
113
    {
114
        return $query->setBuilder($this);
115
    }
116
117
    /**
118
     * @param string $table
119
     * @param array  $values
120
     *
121
     *@return AbstractBaseQuery
122
     */
123
    public function insert($table = null, array $values = null)
124
    {
125
        return $this->injectBuilder(QueryFactory::createInsert($table, $values));
126
    }
127
128
    /**
129
     * @param string $table
130
     * @param array  $values
131
     *
132
     *@return AbstractBaseQuery
133
     */
134
    public function update($table = null, array $values = null)
135
    {
136
        return $this->injectBuilder(QueryFactory::createUpdate($table, $values));
137
    }
138
139
    /**
140
     * @param string $table
141
     *
142
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\Delete
143
     */
144
    public function delete($table = null)
145
    {
146
        return $this->injectBuilder(QueryFactory::createDelete($table));
147
    }
148
149
    /**
150
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\Intersect
151
     */
152
    public function intersect()
153
    {
154
        return QueryFactory::createIntersect();
155
    }
156
157
    /**
158
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\Union
159
     */
160
    public function union()
161
    {
162
        return QueryFactory::createUnion();
163
    }
164
165
    /**
166
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll
167
     */
168
    public function unionAll()
169
    {
170
        return QueryFactory::createUnionAll();
171
    }
172
173
    /**
174
     * @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $first
175
     * @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $second
176
     *
177
     * @return \NilPortugues\Sql\QueryBuilder\Manipulation\Minus
178
     */
179
    public function minus(Select $first, Select $second)
180
    {
181
        return QueryFactory::createMinus($first, $second);
182
    }
183
184
    /**
185
     * @return array
186
     */
187
    public function getValues()
188
    {
189
        return $this->placeholderWriter->get();
190
    }
191
192
    /**
193
     * Returns a SQL string in a readable human-friendly format.
194
     *
195
     * @param QueryInterface $query
196
     *
197
     * @return string
198
     */
199
    public function writeFormatted(QueryInterface $query)
200
    {
201
        if (null === $this->sqlFormatter) {
202
            $this->sqlFormatter = (new \ReflectionClass($this->sqlFormatterClass))->newInstance();
203
        }
204
205
        return $this->sqlFormatter->format($this->write($query));
206
    }
207
208
    /**
209
     * @param QueryInterface $query
210
     * @param bool           $resetPlaceholders
211
     *
212
     * @return string
213
     *
214
     * @throws \RuntimeException
215
     */
216
    public function write(QueryInterface $query, $resetPlaceholders = true)
217
    {
218
        if ($resetPlaceholders) {
219
            $this->placeholderWriter->reset();
220
        }
221
222
        $queryPart = $query->partName();
223
224
        if (false === empty($this->queryWriterArray[$queryPart])) {
225
            $this->createQueryObject($queryPart);
226
227
            return $this->queryWriterInstances[$queryPart]->write($query);
228
        }
229
230
        throw new \RuntimeException('Query builder part not defined.');
231
    }
232
233
    /**
234
     * @param Select $select
235
     *
236
     * @return string
237
     */
238
    public function writeJoin(Select $select)
239
    {
240
        if (null === $this->whereWriter) {
241
            $this->whereWriter = WriterFactory::createWhereWriter($this, $this->placeholderWriter);
242
        }
243
244
        $sql = ($select->getJoinType()) ? "{$select->getJoinType()} " : '';
245
        $sql .= 'JOIN ';
246
        $sql .= $this->writeTableWithAlias($select->getTable());
247
        $sql .= ' ON ';
248
        $sql .= $this->whereWriter->writeWhere($select->getJoinCondition());
249
250
        return $sql;
251
    }
252
253
    /**
254
     * @param Table $table
255
     *
256
     * @return string
257
     */
258
    public function writeTableWithAlias(Table $table)
259
    {
260
        $alias = ($table->getAlias()) ? " AS {$this->writeTableAlias($table->getAlias())}" : '';
261
        $schema = ($table->getSchema()) ? "{$table->getSchema()}." : '';
262
263
        return $schema.$this->writeTableName($table).$alias;
264
    }
265
266
    /**
267
     * @param $alias
268
     *
269
     * @return mixed
270
     */
271
    public function writeTableAlias($alias)
272
    {
273
        return $alias;
274
    }
275
276
    /**
277
     * Returns the table name.
278
     *
279
     * @param Table $table
280
     *
281
     * @return string
282
     */
283
    public function writeTableName(Table $table)
284
    {
285
        return $table->getName();
286
    }
287
288
    /**
289
     * @param string $alias
290
     *
291
     * @return string
292
     */
293
    public function writeColumnAlias($alias)
294
    {
295
        return "'{$alias}'";
296
    }
297
298
    /**
299
     * @param Table $table
300
     *
301
     * @return string
302
     */
303
    public function writeTable(Table $table)
304
    {
305
        $schema = ($table->getSchema()) ? "{$table->getSchema()}." : '';
306
307
        return $schema.$this->writeTableName($table);
308
    }
309
310
    /**
311
     * @param array $values
312
     *
313
     * @return array
314
     */
315
    public function writeValues(array &$values)
316
    {
317
        \array_walk(
318
            $values,
319
            function (&$value) {
320
                $value = $this->writePlaceholderValue($value);
321
            }
322
        );
323
324
        return $values;
325
    }
326
327
    /**
328
     * @param $value
329
     *
330
     * @return string
331
     */
332
    public function writePlaceholderValue($value)
333
    {
334
        return $this->placeholderWriter->add($value);
335
    }
336
337
    /**
338
     * @param $operator
339
     *
340
     * @return string
341
     */
342
    public function writeConjunction($operator)
343
    {
344
        return ' '.$operator.' ';
345
    }
346
347
    /**
348
     * @return string
349
     */
350
    public function writeIsNull()
351
    {
352
        return ' IS NULL';
353
    }
354
355
    /**
356
     * @return string
357
     */
358
    public function writeIsNotNull()
359
    {
360
        return ' IS NOT NULL';
361
    }
362
363
    /**
364
     * Returns the column name.
365
     *
366
     * @param Column $column
367
     *
368
     * @return string
369
     */
370
    public function writeColumnName(Column $column)
371
    {
372
        $name = $column->getName();
373
374
        if ($name === Column::ALL) {
375
            return $this->writeColumnAll();
376
        }
377
378
        return $name;
379
    }
380
381
    /**
382
     * @return string
383
     */
384
    protected function writeColumnAll()
385
    {
386
        return '*';
387
    }
388
389
    /**
390
     * @param string $queryPart
391
     */
392
    protected function createQueryObject($queryPart)
393
    {
394
        if (null === $this->queryWriterInstances[$queryPart]) {
395
            $this->queryWriterInstances[$queryPart] = \call_user_func_array(
396
                \explode('::', $this->queryWriterArray[$queryPart]),
397
                [$this, $this->placeholderWriter]
398
            );
399
        }
400
    }
401
}
402