|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BigShark\SQLToBuilder\Converter; |
|
4
|
|
|
|
|
5
|
|
|
class WhereConverter extends Converter implements ConverterInterface |
|
6
|
|
|
{ |
|
7
|
81 |
|
public function convert($where) |
|
8
|
|
|
{ |
|
9
|
81 |
|
$i = 0; |
|
10
|
81 |
|
$w = []; |
|
11
|
81 |
|
foreach ($where as $key => $item) { |
|
12
|
78 |
|
if ('colref' === $item['expr_type']) { |
|
13
|
78 |
|
$w[$i]['col'] = $this->getValueWithoutQuotes($item); |
|
14
|
78 |
|
} elseif ('const' === $item['expr_type']) { |
|
15
|
66 |
|
$w[$i]['value'] = $this->getValueWithoutInvertedCommas($item['base_expr']); |
|
16
|
78 |
|
} elseif ('in-list' === $item['expr_type']) { |
|
17
|
12 |
|
$callback = [$this, 'getValueWithoutInvertedCommas']; |
|
18
|
12 |
|
$w[$i]['value'] = array_map($callback, array_column($item['sub_tree'], 'base_expr')); |
|
19
|
78 |
|
} elseif ('operator' === $item['expr_type']) { |
|
20
|
78 |
|
$upper = strtoupper($item['base_expr']); |
|
21
|
78 |
|
if ('NOT' === $upper) { |
|
22
|
12 |
|
$w[$i]['not'] = true; |
|
23
|
78 |
|
} elseif ('OR' !== $upper && 'AND' !== $upper) { |
|
24
|
78 |
|
$w[$i]['operator'] = $item['base_expr']; |
|
25
|
33 |
|
} elseif ('OR' === $upper || 'AND' === $upper) { |
|
26
|
33 |
|
$i++; |
|
27
|
48 |
|
$w[$i]['connector'] = $upper; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
81 |
|
$result = []; |
|
33
|
81 |
|
if (is_array($w) && count($w) > 0) { |
|
34
|
78 |
|
foreach ($w as $where) { |
|
35
|
78 |
|
if (isset($where['connector']) && 'OR' === $where['connector']) { |
|
36
|
18 |
|
$where['where'] = 'orWhere'; |
|
37
|
|
|
} else { |
|
38
|
78 |
|
$where['where'] = 'where'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
78 |
|
if (isset($where['not'])) { |
|
42
|
12 |
|
$where['not'] = 'Not'; |
|
43
|
|
|
} else { |
|
44
|
66 |
|
$where['not'] = ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
78 |
|
if ('IN' === strtoupper($where['operator'])) { |
|
48
|
12 |
|
$result[] = $this->format($where['where'].$where['not'].'In', [$where['col'], $where['value']]); |
|
49
|
66 |
|
} elseif ('IS' === strtoupper($where['operator']) && 'NULL' === strtoupper($where['value'])) { |
|
50
|
15 |
|
$result[] = $this->format($where['where'].$where['not'].'Null', [$where['col']]); |
|
51
|
|
|
} else { |
|
52
|
60 |
|
$result[] = $this->format($where['where'], [$where['col'], $where['operator'], $where['value']]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
81 |
|
return $result; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|