1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Robin Appelman <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OC\DB\QueryBuilder\FunctionBuilder; |
25
|
|
|
|
26
|
|
|
use OC\DB\QueryBuilder\QueryFunction; |
27
|
|
|
use OC\DB\QueryBuilder\QuoteHelper; |
28
|
|
|
use OCP\DB\QueryBuilder\IFunctionBuilder; |
29
|
|
|
|
30
|
|
|
class FunctionBuilder implements IFunctionBuilder { |
31
|
|
|
/** @var QuoteHelper */ |
32
|
|
|
protected $helper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ExpressionBuilder constructor. |
36
|
|
|
* |
37
|
|
|
* @param QuoteHelper $helper |
38
|
|
|
*/ |
39
|
|
|
public function __construct(QuoteHelper $helper) { |
40
|
|
|
$this->helper = $helper; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function md5($input) { |
44
|
|
|
return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function concat($x, $y) { |
48
|
|
|
return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function substring($input, $start, $length = null) { |
52
|
|
|
if ($length) { |
53
|
|
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')'); |
54
|
|
|
} else { |
55
|
|
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function sum($field) { |
60
|
|
|
return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function lower($field) { |
64
|
|
|
return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function add($x, $y) { |
68
|
|
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' + ' . $this->helper->quoteColumnName($y)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function subtract($x, $y) { |
72
|
|
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function count($count, $alias = '') { |
76
|
|
|
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : ''; |
77
|
|
|
return new QueryFunction('COUNT(' . $this->helper->quoteColumnName($count) . ')' . $alias); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function max($field) { |
81
|
|
|
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function min($field) { |
85
|
|
|
return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|