Add octetLength and charLength to function builder, and tests

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/31696/head
Côme Chilliet 2022-03-24 16:17:37 +07:00 committed by Vincent Petry (Rebase PR Action)
parent 7dc3eb1e9e
commit efebbacca4
5 changed files with 83 additions and 2 deletions

@ -127,10 +127,9 @@ class Version1120Date20210917155206 extends SimpleMigrationStep {
protected function getSelectQuery(string $table): IQueryBuilder {
$qb = $this->dbc->getQueryBuilder();
$lengthExpr = $this->dbc->getDatabasePlatform()->getLengthExpression('owncloud_name');
$qb->select('owncloud_name', 'directory_uuid')
->from($table)
->where($qb->expr()->gt($qb->createFunction($lengthExpr), '64', IQueryBuilder::PARAM_INT));
->where($qb->expr()->gt($qb->func()->octetLength('owncloud_name'), '64', IQueryBuilder::PARAM_INT));
return $qb;
}

@ -94,6 +94,18 @@ class FunctionBuilder implements IFunctionBuilder {
return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
}
public function octetLength($field, $alias = ''): IQueryFunction {
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
$quotedName = $this->helper->quoteColumnName($field);
return new QueryFunction('LENGTHB(' . $quotedName . ')' . $alias);
}
public function charLength($field, $alias = ''): IQueryFunction {
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
$quotedName = $this->helper->quoteColumnName($field);
return new QueryFunction('LENGTH(' . $quotedName . ')' . $alias);
}
public function max($field): IQueryFunction {
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
}

@ -48,4 +48,10 @@ class SqliteFunctionBuilder extends FunctionBuilder {
public function least($x, $y): IQueryFunction {
return new QueryFunction('MIN(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
}
public function octetLength($field, $alias = ''): IQueryFunction {
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
$quotedName = $this->helper->quoteColumnName($field);
return new QueryFunction('LENGTH(CAST(' . $quotedName . ' as BLOB))' . $alias);
}
}

@ -123,6 +123,24 @@ interface IFunctionBuilder {
*/
public function count($count = '', $alias = ''): IQueryFunction;
/**
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
* @param string $alias Alias for the length
*
* @return IQueryFunction
* @since 24.0.0
*/
public function octetLength($field, $alias = ''): IQueryFunction;
/**
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
* @param string $alias Alias for the length
*
* @return IQueryFunction
* @since 24.0.0
*/
public function charLength($field, $alias = ''): IQueryFunction;
/**
* Takes the maximum of all rows in a column
*

@ -336,6 +336,52 @@ class FunctionBuilderTest extends TestCase {
$this->assertGreaterThan(1, $column);
}
public function octetLengthProvider() {
return [
['', 0],
['foobar', 6],
['fé', 3],
['šđčćž', 10],
];
}
/**
* @dataProvider octetLengthProvider
*/
public function testOctetLength(string $str, int $bytes) {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->octetLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
$result = $query->execute();
$column = $result->fetchOne();
$result->closeCursor();
$this->assertEquals($bytes, $column);
}
public function charLengthProvider() {
return [
['', 0],
['foobar', 6],
['fé', 2],
['šđčćž', 5],
];
}
/**
* @dataProvider charLengthProvider
*/
public function testCharLength(string $str, int $bytes) {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->charLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
$result = $query->execute();
$column = $result->fetchOne();
$result->closeCursor();
$this->assertEquals($bytes, $column);
}
private function setUpMinMax($value) {
$query = $this->connection->getQueryBuilder();