1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder\Traits; |
3
|
|
|
|
4
|
|
|
use Kir\MySQL\Builder\Expr\OrderBySpecification; |
5
|
|
|
|
6
|
|
|
trait OrderByBuilder { |
7
|
|
|
use AbstractDB; |
8
|
|
|
|
9
|
|
|
/** @var array<int, array{string, string}> */ |
|
|
|
|
10
|
|
|
private $orderBy = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string|OrderBySpecification $expression |
14
|
|
|
* @param string&('ASC'|'DESC') $direction |
|
|
|
|
15
|
|
|
* @return $this |
16
|
|
|
*/ |
17
|
|
|
public function orderBy($expression, string $direction = 'ASC') { |
18
|
|
|
if($expression instanceof OrderBySpecification) { |
19
|
|
|
foreach($expression->getFields() as $field) { |
20
|
|
|
$this->addOrder($field[0], $field[1]); |
21
|
|
|
} |
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
$this->addOrder($expression, $direction); |
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $fieldName |
30
|
|
|
* @param array<int, int|float|string> $values |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function orderByValues(string $fieldName, array $values) { |
34
|
|
|
$expr = []; |
35
|
|
|
foreach(array_values($values) as $idx => $value) { |
36
|
|
|
$expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", [$value, $idx]); |
37
|
|
|
} |
38
|
|
|
$this->orderBy[] = [sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), implode("\n\t\t", $expr)), 'ASC']; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $fieldName |
44
|
|
|
* @param array<int, int|float|string> $values |
45
|
|
|
* @return |
46
|
|
|
*/ |
47
|
|
|
public function getOrderBy() { |
48
|
|
|
return $this->orderBy; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Removed all order information from the current layer. This dies not affect sub-selects. |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function resetOrderBy() { |
57
|
|
|
$this->orderBy = []; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $query |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function buildOrder(string $query): string { |
66
|
|
|
if(!count($this->orderBy)) { |
67
|
|
|
return $query; |
68
|
|
|
} |
69
|
|
|
$query .= "ORDER BY\n"; |
70
|
|
|
$arr = []; |
71
|
|
|
foreach($this->orderBy as [$expression, $direction]) { |
72
|
|
|
$arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
73
|
|
|
} |
74
|
|
|
return $query.implode(",\n", $arr)."\n"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|array<int, mixed> $expression |
79
|
|
|
* @param string&('ASC'|'DESC') $direction |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
private function addOrder($expression, string $direction): void { |
82
|
|
|
$direction = $this->fixDirection($direction); |
83
|
|
|
if(is_array($expression)) { |
84
|
|
|
if(count($expression) < 1) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
$expr = (string) $expression[0]; |
88
|
|
|
$expression = $this->db()->quoteExpression($expr, array_slice($expression, 1)); |
89
|
|
|
} |
90
|
|
|
$this->orderBy[] = [$expression, $direction]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $direction |
95
|
|
|
* @return string&('ASC'|'DESC') |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
private function fixDirection(string $direction): string { |
98
|
|
|
return strtoupper($direction) !== 'ASC' ? 'DESC' : 'ASC'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|