Passed
Push — master ( ef1328...91a1d1 )
by Bas
13:32 queued 09:25
created

Grammar::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Traits\Macroable;
7
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesAggregates;
8
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesColumns;
9
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesJoins;
10
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesWhereClauses;
11
use LaravelFreelancerNL\Aranguent\Query\Concerns\HasAliases;
12
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException as BindException;
13
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
14
use LaravelFreelancerNL\FluentAQL\Grammar as FluentAqlGrammar;
15
16
/*
17
 * Provides AQL syntax functions
18
 */
19
20
class Grammar extends FluentAqlGrammar
21
{
22
    use CompilesAggregates;
23
    use CompilesColumns;
0 ignored issues
show
introduced by
The trait LaravelFreelancerNL\Aran...oncerns\CompilesColumns requires some properties which are not provided by LaravelFreelancerNL\Aranguent\Query\Grammar: $aggregate, $distinct, $from, $joins, $table
Loading history...
24
    use CompilesJoins;
0 ignored issues
show
introduced by
The trait LaravelFreelancerNL\Aran...\Concerns\CompilesJoins requires some properties which are not provided by LaravelFreelancerNL\Aranguent\Query\Grammar: $type, $table
Loading history...
25
    use CompilesWhereClauses;
0 ignored issues
show
Bug introduced by
The trait LaravelFreelancerNL\Aran...ns\CompilesWhereClauses requires the property $wheres which is not provided by LaravelFreelancerNL\Aranguent\Query\Grammar.
Loading history...
26
    use HasAliases;
0 ignored issues
show
introduced by
The trait LaravelFreelancerNL\Aran...ery\Concerns\HasAliases requires some properties which are not provided by LaravelFreelancerNL\Aranguent\Query\Grammar: $from, $variables
Loading history...
27
    use Macroable;
28
29
    public $name;
30
31
    /**
32
     * The grammar table prefix.
33
     *
34
     * @var string
35
     */
36
    protected $tablePrefix = '';
37
38
    /**
39
     * The grammar table prefix.
40
     *
41
     * @var null|int
42
     */
43
    protected $offset = null;
44
45
    /**
46
     * The components that make up a select clause.
47
     *
48
     * @var array
49
     */
50
    protected $selectComponents = [
51
        'from',
52
        'variables',
53
        'joins',
54
        'wheres',
55
        'groups',
56
        'aggregate',
57
        'havings',
58
        'orders',
59
        'offset',
60
        'limit',
61
        'columns',
62
    ];
63
64
    protected $operatorTranslations = [
65
        '='          => '==',
66
        '<>'         => '!=',
67
        '<=>'        => '==',
68
        'rlike'      => '=~',
69
        'not rlike'  => '!~',
70
        'regexp'     => '=~',
71
        'not regexp' => '!~',
72
    ];
73
74
    protected $whereTypeOperators = [
75
        'In'    => 'IN',
76
        'NotIn' => 'NOT IN',
77
    ];
78
79
    /**
80
     * Get the format for database stored dates.
81
     *
82
     * @return string
83
     */
84 71
    public function getDateFormat()
85
    {
86 71
        return 'Y-m-d\TH:i:s.v\Z';
87
    }
88
89
    /**
90
     * Get the grammar specific operators.
91
     *
92
     * @return array
93
     */
94 9
    public function getOperators()
95
    {
96 9
        return $this->comparisonOperators;
97
    }
98
99 97
    protected function prefixTable($table)
100
    {
101 97
        return $this->tablePrefix . $table;
102
    }
103
104
    /**
105
     * Compile an insert statement into AQL.
106
     *
107
     * @param Builder $builder
108
     * @param array   $values
109
     *
110
     * @throws BindException
111
     *
112
     * @return Builder
113
     */
114 71
    public function compileInsert(Builder $builder, array $values)
115
    {
116 71
        if (Arr::isAssoc($values)) {
117 10
            $values = [$values];
118
        }
119 71
        $table = $this->prefixTable($builder->from);
120
121 71
        if (empty($values)) {
122
            $builder->aqb = $builder->aqb->insert('{}', $table);
123
124
            return $builder;
125
        }
126
127 71
        $builder->aqb = $builder->aqb->let('values', $values)
128 71
            ->for('value', 'values')
129 71
            ->insert('value', $table)
130 71
            ->return('NEW._key');
131
132 71
        return $builder;
133
    }
134
135
    /**
136
     * Compile an insert and get ID statement into SQL.
137
     *
138
     * @param Builder $builder
139
     * @param array   $values
140
     *
141
     * @throws BindException
142
     *
143
     * @return Builder
144
     */
145 10
    public function compileInsertGetId(Builder $builder, $values)
146
    {
147 10
        return $this->compileInsert($builder, $values);
148
    }
149
150
    /**
151
     * Compile a select query into AQL.
152
     *
153
     * @param Builder $builder
154
     *
155
     * @return Builder
156
     */
157 93
    public function compileSelect(Builder $builder)
158
    {
159
//        if ($builder->unions && $builder->aggregate) {
160
//            return $this->compileUnionAggregate($builder);
161
//        }
162
163
        // To compile the query, we'll spin through each component of the query and
164
        // see if that component exists. If it does we'll just call the compiler
165
        // function for the component which is responsible for making the SQL.
166
167 93
        $builder = $this->compileComponents($builder);
168
169
//        if ($builder->unions) {
170
//            $sql = $this->wrapUnion($sql).' '.$this->compileUnions($builder);
171
//        }
172
173 93
        return $builder;
174
    }
175
176
    /**
177
     * Compile the components necessary for a select clause.
178
     *
179
     * @param Builder $builder
180
     *
181
     * @return Builder
182
     */
183 93
    protected function compileComponents(Builder $builder)
184
    {
185 93
        foreach ($this->selectComponents as $component) {
186
            // To compile the query, we'll spin through each component of the query and
187
            // see if that component exists. If it does we'll just call the compiler
188
            // function for the component which is responsible for making the SQL.
189
190 93
            if (isset($builder->$component) && !is_null($builder->$component)) {
191 93
                $method = 'compile' . ucfirst($component);
192
193 93
                $builder = $this->$method($builder, $builder->$component);
194
            }
195
        }
196
197 93
        return $builder;
198
    }
199
200
    /**
201
     * Compile the "from" portion of the query -> FOR in AQL.
202
     *
203
     * @param Builder $builder
204
     * @param string  $table
205
     *
206
     * @return Builder
207
     */
208 93
    protected function compileFrom(Builder $builder, $table)
209
    {
210 93
        $table = $this->prefixTable($table);
211 93
        $alias = $this->registerTableAlias($table);
212
213 93
        $builder->aqb = $builder->aqb->for($alias, $table);
214
215 93
        return $builder;
216
    }
217
218
    /**
219
     * @param  Builder  $builder
220
     * @param  array $variables
221
     * @return Builder
222
     */
223 93
    protected function compileVariables(Builder $builder, array $variables)
224
    {
225 93
        if (! empty($variables)) {
226 1
            foreach ($variables as $variable => $data) {
227 1
                $builder->aqb = $builder->aqb->let($variable, $data);
228
            }
229
        }
230
231 93
        return $builder;
232
    }
233
234
    /**
235
     * Compile the "order by" portions of the query.
236
     *
237
     * @param Builder $builder
238
     * @param array   $orders
239
     *
240
     * @return Builder
241
     */
242 2
    protected function compileOrders(Builder $builder, $orders)
243
    {
244 2
        if (!empty($orders)) {
245 2
            $orders = $this->compileOrdersToFlatArray($builder, $orders);
246 2
            $builder->aqb = $builder->aqb->sort(...$orders);
247
248 2
            return $builder;
249
        }
250
251
        return $builder;
252
    }
253
254
    /**
255
     * Compile the query orders to an array.
256
     *
257
     * @param Builder $builder
258
     * @param array   $orders
259
     *
260
     * @return array
261
     */
262 2
    protected function compileOrdersToFlatArray(Builder $builder, $orders)
263
    {
264 2
        $flatOrders = [];
265
266 2
        foreach ($orders as $order) {
267 2
            if (!isset($order['type']) || $order['type'] != 'Raw') {
268 1
                $order['column'] = $this->normalizeColumn($builder, $order['column']);
269
            }
270
271 2
            $flatOrders[] = $order['column'];
272
273 2
            if (isset($order['direction'])) {
274 1
                $flatOrders[] = $order['direction'];
275
            }
276
        }
277
278 2
        return $flatOrders;
279
    }
280
281
    /**
282
     * Compile the "offset" portions of the query.
283
     * We are handling this first by saving the offset which will be used by the FluentAQL's limit function.
284
     *
285
     * @param Builder $builder
286
     * @param int     $offset
287
     *
288
     * @return Builder
289
     */
290 2
    protected function compileOffset(Builder $builder, $offset)
291
    {
292 2
        $this->offset = (int) $offset;
293
294 2
        return $builder;
295
    }
296
297
    /**
298
     * Compile the "limit" portions of the query.
299
     *
300
     * @param Builder $builder
301
     * @param int     $limit
302
     *
303
     * @return Builder
304
     */
305 42
    protected function compileLimit(Builder $builder, $limit)
306
    {
307 42
        if ($this->offset !== null) {
308 2
            $builder->aqb = $builder->aqb->limit((int) $this->offset, (int) $limit);
309
310 2
            return $builder;
311
        }
312 40
        $builder->aqb = $builder->aqb->limit((int) $limit);
313
314 40
        return $builder;
315
    }
316
317
318
    /**
319
     * Compile an update statement into SQL.
320
     *
321
     * @param Builder $builder
322
     * @param array   $values
323
     *
324
     * @return Builder
325
     */
326 15
    public function compileUpdate(Builder $builder, array $values)
327
    {
328
329 15
        $table = $this->prefixTable($builder->from);
330 15
        $tableAlias = $this->generateTableAlias($table);
331
332 15
        $builder->aqb = $builder->aqb->for($tableAlias, $table);
333
334
        //Fixme: joins?
335 15
        $builder = $this->compileWheres($builder);
336
337 15
        $builder->aqb = $builder->aqb->update($tableAlias, $values, $table);
338
339 15
        return $builder;
340
    }
341
342
    /**
343
     * Compile a delete statement into SQL.
344
     *
345
     * @SuppressWarnings(PHPMD.CamelCaseParameterName)
346
     * @SuppressWarnings(PHPMD.CamelCaseVariableName)
347
     *
348
     * @param Builder $builder
349
     * @param null    $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
350
     *
351
     * @return Builder
352
     */
353 6
    public function compileDelete(Builder $builder, $id = null)
354
    {
355 6
        $table = $this->prefixTable($builder->from);
356 6
        $tableAlias = $this->generateTableAlias($table);
357
358
359 6
        if (!is_null($id)) {
0 ignored issues
show
introduced by
The condition is_null($id) is always true.
Loading history...
360 1
            $builder->aqb = $builder->aqb->remove((string) $id, $table);
361
362 1
            return $builder;
363
        }
364
365 6
        $builder->aqb = $builder->aqb->for($tableAlias, $table);
366
367
        //Fixme: joins?
368 6
        $builder = $this->compileWheres($builder);
369
370 6
        $builder->aqb = $builder->aqb->remove($tableAlias, $table);
371
372 6
        return $builder;
373
    }
374
375
    /**
376
     * Compile the random statement into SQL.
377
     *
378
     * @param Builder $builder
379
     *
380
     * @return FunctionExpression;
381
     */
382 1
    public function compileRandom(Builder $builder)
383
    {
384 1
        return $builder->aqb->rand();
385
    }
386
387
    /**
388
     * Get the value of a raw expression.
389
     *
390
     * @param  \Illuminate\Database\Query\Expression  $expression
391
     * @return string
392
     */
393 1
    public function getValue($expression)
394
    {
395 1
        return $expression->getValue();
396
    }
397
}
398