Skip to content

Commit 4a4f250

Browse files
authored
Merge pull request #31696 from nextcloud/fix/user_ldap-fix-migration-lengthcheck-oracle
Use getLengthExpression to measure field length instead of like
2 parents bdfc656 + 7407a32 commit 4a4f250

File tree

6 files changed

+105
-1
lines changed

6 files changed

+105
-1
lines changed

apps/user_ldap/lib/Migration/Version1120Date20210917155206.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected function getSelectQuery(string $table): IQueryBuilder {
129129
$qb = $this->dbc->getQueryBuilder();
130130
$qb->select('owncloud_name', 'directory_uuid')
131131
->from($table)
132-
->where($qb->expr()->like('owncloud_name', $qb->createNamedParameter(str_repeat('_', 65) . '%'), Types::STRING));
132+
->where($qb->expr()->gt($qb->func()->octetLength('owncloud_name'), '64', IQueryBuilder::PARAM_INT));
133133
return $qb;
134134
}
135135

lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ public function count($count = '', $alias = ''): IQueryFunction {
9494
return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
9595
}
9696

97+
public function octetLength($field, $alias = ''): IQueryFunction {
98+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
99+
$quotedName = $this->helper->quoteColumnName($field);
100+
return new QueryFunction('OCTET_LENGTH(' . $quotedName . ')' . $alias);
101+
}
102+
103+
public function charLength($field, $alias = ''): IQueryFunction {
104+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
105+
$quotedName = $this->helper->quoteColumnName($field);
106+
return new QueryFunction('CHAR_LENGTH(' . $quotedName . ')' . $alias);
107+
}
108+
97109
public function max($field): IQueryFunction {
98110
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
99111
}

lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,16 @@ public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
9191
$separator = $this->connection->quote($separator);
9292
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ', ' . $separator . ')' . $orderByClause);
9393
}
94+
95+
public function octetLength($field, $alias = ''): IQueryFunction {
96+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
97+
$quotedName = $this->helper->quoteColumnName($field);
98+
return new QueryFunction('LENGTHB(' . $quotedName . ')' . $alias);
99+
}
100+
101+
public function charLength($field, $alias = ''): IQueryFunction {
102+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
103+
$quotedName = $this->helper->quoteColumnName($field);
104+
return new QueryFunction('LENGTH(' . $quotedName . ')' . $alias);
105+
}
94106
}

lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ public function greatest($x, $y): IQueryFunction {
4848
public function least($x, $y): IQueryFunction {
4949
return new QueryFunction('MIN(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
5050
}
51+
52+
public function octetLength($field, $alias = ''): IQueryFunction {
53+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
54+
$quotedName = $this->helper->quoteColumnName($field);
55+
return new QueryFunction('LENGTH(CAST(' . $quotedName . ' as BLOB))' . $alias);
56+
}
57+
58+
public function charLength($field, $alias = ''): IQueryFunction {
59+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
60+
$quotedName = $this->helper->quoteColumnName($field);
61+
return new QueryFunction('LENGTH(' . $quotedName . ')' . $alias);
62+
}
5163
}

lib/public/DB/QueryBuilder/IFunctionBuilder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ public function subtract($x, $y): IQueryFunction;
123123
*/
124124
public function count($count = '', $alias = ''): IQueryFunction;
125125

126+
/**
127+
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
128+
* @param string $alias Alias for the length
129+
*
130+
* @return IQueryFunction
131+
* @since 24.0.0
132+
*/
133+
public function octetLength($field, $alias = ''): IQueryFunction;
134+
135+
/**
136+
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
137+
* @param string $alias Alias for the length
138+
*
139+
* @return IQueryFunction
140+
* @since 24.0.0
141+
*/
142+
public function charLength($field, $alias = ''): IQueryFunction;
143+
126144
/**
127145
* Takes the maximum of all rows in a column
128146
*

tests/lib/DB/QueryBuilder/FunctionBuilderTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,56 @@ public function testCount() {
336336
$this->assertGreaterThan(1, $column);
337337
}
338338

339+
public function octetLengthProvider() {
340+
return [
341+
['', 0],
342+
['foobar', 6],
343+
['', 3],
344+
['šđčćž', 10],
345+
];
346+
}
347+
348+
/**
349+
* @dataProvider octetLengthProvider
350+
*/
351+
public function testOctetLength(string $str, int $bytes) {
352+
$query = $this->connection->getQueryBuilder();
353+
354+
$query->select($query->func()->octetLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
355+
$query->from('appconfig')
356+
->setMaxResults(1);
357+
358+
$result = $query->execute();
359+
$column = $result->fetchOne();
360+
$result->closeCursor();
361+
$this->assertEquals($bytes, $column);
362+
}
363+
364+
public function charLengthProvider() {
365+
return [
366+
['', 0],
367+
['foobar', 6],
368+
['', 2],
369+
['šđčćž', 5],
370+
];
371+
}
372+
373+
/**
374+
* @dataProvider charLengthProvider
375+
*/
376+
public function testCharLength(string $str, int $bytes) {
377+
$query = $this->connection->getQueryBuilder();
378+
379+
$query->select($query->func()->charLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
380+
$query->from('appconfig')
381+
->setMaxResults(1);
382+
383+
$result = $query->execute();
384+
$column = $result->fetchOne();
385+
$result->closeCursor();
386+
$this->assertEquals($bytes, $column);
387+
}
388+
339389
private function setUpMinMax($value) {
340390
$query = $this->connection->getQueryBuilder();
341391

0 commit comments

Comments
 (0)