Skip to content

Commit 8e73f29

Browse files
come-ncbackportbot-nextcloud[bot]
authored andcommitted
Add octetLength and charLength to function builder, and tests
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 884ea3e commit 8e73f29

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

apps/user_ldap/lib/Migration/Version1120Date20210917155206.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ protected function handleIDs(string $table, bool $emitHooks) {
127127

128128
protected function getSelectQuery(string $table): IQueryBuilder {
129129
$qb = $this->dbc->getQueryBuilder();
130-
$lengthExpr = $this->dbc->getDatabasePlatform()->getLengthExpression('owncloud_name');
131130
$qb->select('owncloud_name', 'directory_uuid')
132131
->from($table)
133-
->where($qb->expr()->gt($qb->createFunction($lengthExpr), '64', IQueryBuilder::PARAM_INT));
132+
->where($qb->expr()->gt($qb->func()->octetLength('owncloud_name'), '64', IQueryBuilder::PARAM_INT));
134133
return $qb;
135134
}
136135

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

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

82+
public function octetLength($field, $alias = ''): IQueryFunction {
83+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
84+
$quotedName = $this->helper->quoteColumnName($field);
85+
return new QueryFunction('LENGTHB(' . $quotedName . ')' . $alias);
86+
}
87+
88+
public function charLength($field, $alias = ''): IQueryFunction {
89+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
90+
$quotedName = $this->helper->quoteColumnName($field);
91+
return new QueryFunction('LENGTH(' . $quotedName . ')' . $alias);
92+
}
93+
8294
public function max($field): IQueryFunction {
8395
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
8496
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ public function greatest($x, $y): IQueryFunction {
3838
public function least($x, $y): IQueryFunction {
3939
return new QueryFunction('MIN(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
4040
}
41+
42+
public function octetLength($field, $alias = ''): IQueryFunction {
43+
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
44+
$quotedName = $this->helper->quoteColumnName($field);
45+
return new QueryFunction('LENGTH(CAST(' . $quotedName . ' as BLOB))' . $alias);
46+
}
4147
}

lib/public/DB/QueryBuilder/IFunctionBuilder.php

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

110+
/**
111+
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
112+
* @param string $alias Alias for the length
113+
*
114+
* @return IQueryFunction
115+
* @since 24.0.0
116+
*/
117+
public function octetLength($field, $alias = ''): IQueryFunction;
118+
119+
/**
120+
* @param string|ILiteral|IParameter|IQueryFunction $field The input to be measured
121+
* @param string $alias Alias for the length
122+
*
123+
* @return IQueryFunction
124+
* @since 24.0.0
125+
*/
126+
public function charLength($field, $alias = ''): IQueryFunction;
127+
110128
/**
111129
* Takes the maximum of all rows in a column
112130
*

tests/lib/DB/QueryBuilder/FunctionBuilderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,52 @@ public function testCount() {
145145
$this->assertGreaterThan(1, $column);
146146
}
147147

148+
public function octetLengthProvider() {
149+
return [
150+
['', 0],
151+
['foobar', 6],
152+
['', 3],
153+
['šđčćž', 10],
154+
];
155+
}
156+
157+
/**
158+
* @dataProvider octetLengthProvider
159+
*/
160+
public function testOctetLength(string $str, int $bytes) {
161+
$query = $this->connection->getQueryBuilder();
162+
163+
$query->select($query->func()->octetLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
164+
165+
$result = $query->execute();
166+
$column = $result->fetchOne();
167+
$result->closeCursor();
168+
$this->assertEquals($bytes, $column);
169+
}
170+
171+
public function charLengthProvider() {
172+
return [
173+
['', 0],
174+
['foobar', 6],
175+
['', 2],
176+
['šđčćž', 5],
177+
];
178+
}
179+
180+
/**
181+
* @dataProvider charLengthProvider
182+
*/
183+
public function testCharLength(string $str, int $bytes) {
184+
$query = $this->connection->getQueryBuilder();
185+
186+
$query->select($query->func()->charLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
187+
188+
$result = $query->execute();
189+
$column = $result->fetchOne();
190+
$result->closeCursor();
191+
$this->assertEquals($bytes, $column);
192+
}
193+
148194
private function setUpMinMax($value) {
149195
$query = $this->connection->getQueryBuilder();
150196

0 commit comments

Comments
 (0)