PostgresGrammar::whereNotNull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
namespace Bosnadev\Database\Query\Grammars;
3
4
use Illuminate\Database\Query\Builder;
5
use Illuminate\Database\Query\Grammars\PostgresGrammar as LaravelPostgresGrammar;
6
7
/**
8
 * Class PostgresGrammar
9
 *
10
 * @package Bosnadev\Database\Query\Grammars
11
 */
12
class PostgresGrammar extends LaravelPostgresGrammar
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $jsonOperators = [
18
        '->',
19
        '->>',
20
        '#>',
21
        '#>>',
22
    ];
23
24
    /**
25
     * @param string $value
26
     *
27
     * @return string
28
     */
29 12
    protected function wrapValue($value)
30
    {
31 12
        if ($value === '*') {
32
            return $value;
33
        }
34
35
        // If querying hstore
36 12
        if (preg_match('/\[(.*?)\]/', $value, $match)) {
37 3
            return (string)str_replace(array('[', ']'), '', $match[1]);
38
        }
39
40
        // If querying json column
41 9
        foreach ($this->jsonOperators as $operator) {
42 9
            if (stripos($value, $operator)) {
43 9
                list($value, $key) = explode($operator, $value, 2);
44 9
                return parent::wrapValue($value) . $operator . $key;
45
            }
46 3
        }
47
48
        return parent::wrapValue($value);
49
    }
50
51
    /**
52
     * Compile a "where null" clause.
53
     *
54
     * @param  Builder $query
55
     * @param  array   $where
56
     *
57
     * @return string
58
     */
59 3
    protected function whereNull(Builder $query, $where)
60
    {
61 3
        return '(' . $this->wrap($where['column']) . ') is null';
62
    }
63
64
    /**
65
     * Compile a "where not null" clause.
66
     *
67
     * @param  Builder $query
68
     * @param  array   $where
69
     *
70
     * @return string
71
     */
72 3
    protected function whereNotNull(Builder $query, $where)
73
    {
74 3
        return '(' . $this->wrap($where['column']) . ') is not null';
75
    }
76
}
77