|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Phuria SQL Builder package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Phuria\QueryBuilder\Expression; |
|
13
|
|
|
|
|
14
|
|
|
use Phuria\QueryBuilder\ExprBuilder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class FunctionExpression implements ExpressionInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const FUNC_ASCI = 'ASCI'; |
|
22
|
|
|
const FUNC_BIN = 'BIN'; |
|
23
|
|
|
const FUNC_BIT_LENGTH = 'BIT_LENGTH'; |
|
24
|
|
|
const FUNC_CHAR = 'CHAR'; |
|
25
|
|
|
const FUNC_COALESCE = 'COALESCE'; |
|
26
|
|
|
const FUNC_CONCAT = 'CONCAT'; |
|
27
|
|
|
const FUNC_CONCAT_WS = 'CONCAT_WS'; |
|
28
|
|
|
const FUNC_ELT = 'ELT'; |
|
29
|
|
|
const FUNC_EXPORT_SET = 'EXPORT_SET'; |
|
30
|
|
|
const FUNC_FIELD = 'FIELD'; |
|
31
|
|
|
const FUNC_IFNULL = 'IFNULL'; |
|
32
|
|
|
const FUNC_MAX = 'MAX'; |
|
33
|
|
|
const FUNC_SUM = 'SUM'; |
|
34
|
|
|
const FUNC_YEAR = 'YEAR'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string $functionName |
|
38
|
|
|
*/ |
|
39
|
|
|
private $functionName; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ExpressionInterface $args |
|
43
|
|
|
*/ |
|
44
|
|
|
private $arguments; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var FunctionCallContext|null $context |
|
48
|
|
|
*/ |
|
49
|
|
|
private $context; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $functionName |
|
53
|
|
|
* @param ExpressionInterface $arguments |
|
54
|
|
|
* @param FunctionCallContext|null $context |
|
55
|
|
|
*/ |
|
56
|
11 |
|
public function __construct($functionName, ExpressionInterface $arguments, FunctionCallContext $context = null) |
|
57
|
|
|
{ |
|
58
|
11 |
|
$this->functionName = $functionName; |
|
59
|
11 |
|
$this->arguments = $arguments; |
|
60
|
11 |
|
$this->context = $context; |
|
61
|
11 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function compile() |
|
67
|
|
|
{ |
|
68
|
11 |
|
$expression = $this->arguments; |
|
69
|
|
|
|
|
70
|
11 |
|
if ($expression instanceof ExprBuilder) { |
|
71
|
|
|
$expression = $expression->getWrappedExpression(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
11 |
|
if ($expression instanceof ExpressionCollection) { |
|
75
|
4 |
|
$expression = $expression->changeSeparator(', '); |
|
76
|
4 |
|
} |
|
77
|
|
|
|
|
78
|
11 |
|
return $this->functionName . '(' . $expression->compile() . $this->compileHints() . ')'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
11 |
|
private function compileHints() |
|
85
|
|
|
{ |
|
86
|
11 |
|
if (null === $this->context) { |
|
87
|
10 |
|
return ''; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
$compiledHints = []; |
|
91
|
|
|
|
|
92
|
1 |
|
foreach ($this->context->getCallHints() as $hint) { |
|
93
|
1 |
|
$compiledHints[] = $hint->compile(); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return implode('', $compiledHints); |
|
97
|
|
|
} |
|
98
|
|
|
} |