|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Umbrellio\Postgres\Compilers\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Schema\Grammars\Grammar; |
|
8
|
|
|
use Illuminate\Support\Fluent; |
|
9
|
|
|
use Umbrellio\Postgres\Schema\Blueprint; |
|
10
|
|
|
|
|
11
|
|
|
trait WheresBuilder |
|
12
|
|
|
{ |
|
13
|
2 |
|
protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, array $where = []): string |
|
14
|
|
|
{ |
|
15
|
2 |
|
return call_user_func_array('sprintf', array_merge( |
|
16
|
2 |
|
[str_replace('?', '%s', $where['sql'])], |
|
17
|
2 |
|
static::wrapValues($where['bindings']) |
|
18
|
2 |
|
)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
2 |
|
protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, array $where): string |
|
22
|
|
|
{ |
|
23
|
2 |
|
return implode(' ', [ |
|
24
|
2 |
|
$grammar->wrap($where['column']), |
|
25
|
2 |
|
$where['operator'], |
|
26
|
2 |
|
static::wrapValue($where['value']), |
|
27
|
2 |
|
]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
protected static function whereColumn(Grammar $grammar, Blueprint $blueprint, array $where): string |
|
31
|
|
|
{ |
|
32
|
3 |
|
return implode(' ', [ |
|
33
|
3 |
|
$grammar->wrap($where['first']), |
|
34
|
3 |
|
$where['operator'], |
|
35
|
3 |
|
$grammar->wrap($where['second']), |
|
36
|
3 |
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
protected static function whereIn(Grammar $grammar, Blueprint $blueprint, array $where = []): string |
|
40
|
|
|
{ |
|
41
|
3 |
|
if (! empty($where['values'])) { |
|
42
|
2 |
|
return implode(' ', [ |
|
43
|
2 |
|
$grammar->wrap($where['column']), |
|
44
|
2 |
|
'in', |
|
45
|
2 |
|
'(' . implode(',', static::wrapValues($where['values'])) . ')', |
|
46
|
2 |
|
]); |
|
47
|
|
|
} |
|
48
|
1 |
|
return '0 = 1'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, array $where = []): string |
|
52
|
|
|
{ |
|
53
|
2 |
|
if (! empty($where['values'])) { |
|
54
|
1 |
|
return implode(' ', [ |
|
55
|
1 |
|
$grammar->wrap($where['column']), |
|
56
|
1 |
|
'not in', |
|
57
|
1 |
|
'(' . implode(',', static::wrapValues($where['values'])) . ')', |
|
58
|
1 |
|
]); |
|
59
|
|
|
} |
|
60
|
1 |
|
return '1 = 1'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
protected static function whereNull(Grammar $grammar, Blueprint $blueprint, array $where): string |
|
64
|
|
|
{ |
|
65
|
4 |
|
return implode(' ', [$grammar->wrap($where['column']), 'is null']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
protected static function whereNotNull(Grammar $grammar, Blueprint $blueprint, array $where): string |
|
69
|
|
|
{ |
|
70
|
2 |
|
return implode(' ', [$grammar->wrap($where['column']), 'is not null']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, array $where): string |
|
74
|
|
|
{ |
|
75
|
2 |
|
return implode(' ', [ |
|
76
|
2 |
|
$grammar->wrap($where['column']), |
|
77
|
2 |
|
$where['not'] ? 'not between' : 'between', |
|
78
|
2 |
|
static::wrapValue(reset($where['values'])), |
|
79
|
2 |
|
'and', |
|
80
|
2 |
|
static::wrapValue(end($where['values'])), |
|
81
|
2 |
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
protected static function wrapValues(array $values = []): array |
|
85
|
|
|
{ |
|
86
|
5 |
|
return collect($values)->map(function ($value) { |
|
|
|
|
|
|
87
|
4 |
|
return static::wrapValue($value); |
|
88
|
5 |
|
})->toArray(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
protected static function wrapValue($value) |
|
92
|
|
|
{ |
|
93
|
9 |
|
if (is_string($value)) { |
|
94
|
3 |
|
return "'{$value}'"; |
|
95
|
|
|
} |
|
96
|
7 |
|
return (int) $value; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
18 |
|
protected static function removeLeadingBoolean(string $value): string |
|
100
|
|
|
{ |
|
101
|
18 |
|
return preg_replace('/and |or /i', '', $value, 1); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
21 |
|
private static function build(Grammar $grammar, Blueprint $blueprint, Fluent $command): array |
|
105
|
|
|
{ |
|
106
|
21 |
|
return collect($command->get('wheres')) |
|
107
|
21 |
|
->map(function ($where) use ($grammar, $blueprint) { |
|
108
|
18 |
|
return implode(' ', [ |
|
109
|
18 |
|
$where['boolean'], |
|
110
|
18 |
|
'(' . static::{"where{$where['type']}"}($grammar, $blueprint, $where) . ')', |
|
111
|
18 |
|
]); |
|
112
|
21 |
|
}) |
|
113
|
21 |
|
->all(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|