2017-01-16 09:31:04 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-01-16 09:31:04 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-01-16 09:31:04 -05:00
|
|
|
*/
|
|
|
|
|
namespace OC\DB\QueryBuilder\FunctionBuilder;
|
|
|
|
|
|
2024-07-19 06:54:40 -04:00
|
|
|
use OC\DB\Connection;
|
2017-01-16 09:31:04 -05:00
|
|
|
use OC\DB\QueryBuilder\QueryFunction;
|
|
|
|
|
use OC\DB\QueryBuilder\QuoteHelper;
|
|
|
|
|
use OCP\DB\QueryBuilder\IFunctionBuilder;
|
2022-01-04 02:50:10 -05:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2020-11-07 08:06:03 -05:00
|
|
|
use OCP\DB\QueryBuilder\IQueryFunction;
|
2022-01-03 09:04:05 -05:00
|
|
|
use OCP\IDBConnection;
|
2026-01-23 08:50:57 -05:00
|
|
|
use Override;
|
2017-01-16 09:31:04 -05:00
|
|
|
|
|
|
|
|
class FunctionBuilder implements IFunctionBuilder {
|
2024-07-19 06:54:40 -04:00
|
|
|
/** @var IDBConnection|Connection */
|
2022-01-03 09:04:05 -05:00
|
|
|
protected $connection;
|
|
|
|
|
|
2022-01-04 02:50:10 -05:00
|
|
|
/** @var IQueryBuilder */
|
|
|
|
|
protected $queryBuilder;
|
|
|
|
|
|
|
|
|
|
/** @var QuoteHelper */
|
|
|
|
|
protected $helper;
|
|
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder, QuoteHelper $helper) {
|
2022-01-03 09:04:05 -05:00
|
|
|
$this->connection = $connection;
|
2022-01-04 02:50:10 -05:00
|
|
|
$this->queryBuilder = $queryBuilder;
|
2017-01-16 09:31:04 -05:00
|
|
|
$this->helper = $helper;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function md5($input): IQueryFunction {
|
2017-01-16 09:31:04 -05:00
|
|
|
return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')');
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 15:21:35 -05:00
|
|
|
public function concat($x, ...$expr): IQueryFunction {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$list = [];
|
|
|
|
|
foreach ($args as $item) {
|
|
|
|
|
$list[] = $this->helper->quoteColumnName($item);
|
|
|
|
|
}
|
|
|
|
|
return new QueryFunction(sprintf('CONCAT(%s)', implode(', ', $list)));
|
2017-01-16 09:31:04 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-03 09:04:05 -05:00
|
|
|
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
|
2022-01-03 09:04:05 -05:00
|
|
|
$separator = $this->connection->quote($separator);
|
2022-01-03 09:04:05 -05:00
|
|
|
return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
|
2021-12-21 10:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function substring($input, $start, $length = null): IQueryFunction {
|
2017-01-16 09:31:04 -05:00
|
|
|
if ($length) {
|
|
|
|
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')');
|
|
|
|
|
} else {
|
|
|
|
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-12 10:09:35 -04:00
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function sum($field): IQueryFunction {
|
2017-04-12 10:09:35 -04:00
|
|
|
return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')');
|
|
|
|
|
}
|
2017-12-20 09:51:37 -05:00
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function lower($field): IQueryFunction {
|
2017-12-20 09:51:37 -05:00
|
|
|
return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')');
|
|
|
|
|
}
|
2018-04-10 12:30:43 -04:00
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function add($x, $y): IQueryFunction {
|
2018-04-10 12:30:43 -04:00
|
|
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' + ' . $this->helper->quoteColumnName($y));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function subtract($x, $y): IQueryFunction {
|
2018-04-10 12:30:43 -04:00
|
|
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y));
|
|
|
|
|
}
|
2018-06-14 08:32:22 -04:00
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function count($count = '', $alias = ''): IQueryFunction {
|
2018-10-19 10:44:28 -04:00
|
|
|
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
|
2019-10-25 10:55:08 -04:00
|
|
|
$quotedName = $count === '' ? '*' : $this->helper->quoteColumnName($count);
|
|
|
|
|
return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
|
2018-06-14 08:32:22 -04:00
|
|
|
}
|
2019-09-04 10:48:02 -04:00
|
|
|
|
2022-03-24 11:17:37 -04:00
|
|
|
public function octetLength($field, $alias = ''): IQueryFunction {
|
|
|
|
|
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
|
|
|
|
|
$quotedName = $this->helper->quoteColumnName($field);
|
2022-03-24 12:22:41 -04:00
|
|
|
return new QueryFunction('OCTET_LENGTH(' . $quotedName . ')' . $alias);
|
2022-03-24 11:17:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function charLength($field, $alias = ''): IQueryFunction {
|
|
|
|
|
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
|
|
|
|
|
$quotedName = $this->helper->quoteColumnName($field);
|
2022-03-27 18:01:17 -04:00
|
|
|
return new QueryFunction('CHAR_LENGTH(' . $quotedName . ')' . $alias);
|
2022-03-24 11:17:37 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function max($field): IQueryFunction {
|
2019-09-04 10:48:02 -04:00
|
|
|
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function min($field): IQueryFunction {
|
2019-09-04 10:48:02 -04:00
|
|
|
return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')');
|
|
|
|
|
}
|
2019-11-06 05:38:47 -05:00
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function greatest($x, $y): IQueryFunction {
|
2019-11-06 05:38:47 -05:00
|
|
|
return new QueryFunction('GREATEST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 13:17:21 -05:00
|
|
|
public function least($x, $y): IQueryFunction {
|
2019-11-06 05:38:47 -05:00
|
|
|
return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
|
|
|
|
|
}
|
2026-01-23 08:50:57 -05:00
|
|
|
|
|
|
|
|
#[Override]
|
|
|
|
|
public function now(): IQueryFunction {
|
|
|
|
|
return new QueryFunction('NOW()');
|
|
|
|
|
}
|
2017-01-16 09:31:04 -05:00
|
|
|
}
|