Passed
Push — master ( a9b098...de9fe8 )
by Joas
14:04 queued 13s
created

MySqlExpressionBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A castColumn() 0 6 2
A __construct() 0 5 1
A iLike() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OC\DB\QueryBuilder\ExpressionBuilder;
27
28
use OC\DB\ConnectionAdapter;
29
use OC\DB\QueryBuilder\QueryFunction;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\DB\QueryBuilder\IQueryFunction;
32
33
class MySqlExpressionBuilder extends ExpressionBuilder {
34
35
	/** @var string */
36
	protected $collation;
37
38
	/**
39
	 * @param ConnectionAdapter $connection
40
	 * @param IQueryBuilder $queryBuilder
41
	 */
42
	public function __construct(ConnectionAdapter $connection, IQueryBuilder $queryBuilder) {
43
		parent::__construct($connection, $queryBuilder);
44
45
		$params = $connection->getInner()->getParams();
46
		$this->collation = $params['collation'] ?? (($params['charset'] ?? 'utf8') . '_general_ci');
47
	}
48
49
	/**
50
	 * @inheritdoc
51
	 */
52
	public function iLike($x, $y, $type = null): string {
53
		$x = $this->helper->quoteColumnName($x);
54
		$y = $this->helper->quoteColumnName($y);
55
		return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y);
56
	}
57
58
	/**
59
	 * Returns a IQueryFunction that casts the column to the given type
60
	 *
61
	 * @param string|IQueryFunction $column
62
	 * @param mixed $type One of IQueryBuilder::PARAM_*
63
	 * @psalm-param IQueryBuilder::PARAM_* $type
64
	 * @return IQueryFunction
65
	 */
66
	public function castColumn($column, $type): IQueryFunction {
67
		switch ($type) {
68
			case IQueryBuilder::PARAM_STR:
69
				return new QueryFunction('CAST(' . $this->helper->quoteColumnName($column) . ' AS CHAR)');
70
			default:
71
				return parent::castColumn($column, $type);
72
		}
73
	}
74
}
75