Skip to content

Commit 005b7a0

Browse files
committed
add function info for LOWER/UPPER
1 parent 92bb88d commit 005b7a0

File tree

5 files changed

+207
-1
lines changed

5 files changed

+207
-1
lines changed

src/Database/FunctionInfo/FunctionInfoRegistryFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function createDefaultFunctionInfos(): array
2828
new FoundRows(),
2929
new IfFunction(),
3030
new GroupConcat(),
31+
new LowerUpper(),
3132
new MaxMin(),
3233
new Now(),
3334
new Round(),
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MariaStan\Database\FunctionInfo;
6+
7+
use MariaStan\Analyser\AnalyserConditionTypeEnum;
8+
use MariaStan\Analyser\ExprTypeResult;
9+
use MariaStan\Ast\Expr\FunctionCall\FunctionCall;
10+
use MariaStan\Parser\Exception\ParserException;
11+
use MariaStan\Schema\DbType\VarcharType;
12+
13+
use function count;
14+
15+
final class LowerUpper implements FunctionInfo
16+
{
17+
/** @inheritDoc */
18+
public function getSupportedFunctionNames(): array
19+
{
20+
return ['LOWER', 'LCASE', 'UPPER', 'UCASE'];
21+
}
22+
23+
public function getFunctionType(): FunctionTypeEnum
24+
{
25+
return FunctionTypeEnum::SIMPLE;
26+
}
27+
28+
public function checkSyntaxErrors(FunctionCall $functionCall): void
29+
{
30+
$args = $functionCall->getArguments();
31+
$argCount = count($args);
32+
33+
if ($argCount === 1) {
34+
return;
35+
}
36+
37+
throw new ParserException(
38+
FunctionInfoHelper::createArgumentCountErrorMessageFixed(
39+
$functionCall->getFunctionName(),
40+
1,
41+
$argCount,
42+
),
43+
);
44+
}
45+
46+
/** @inheritDoc */
47+
public function getInnerConditions(?AnalyserConditionTypeEnum $condition, array $arguments): array
48+
{
49+
return [$condition];
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter
55+
*/
56+
public function getReturnType(
57+
FunctionCall $functionCall,
58+
array $argumentTypes,
59+
?AnalyserConditionTypeEnum $condition,
60+
): ExprTypeResult {
61+
$arg = $argumentTypes[0];
62+
63+
return new ExprTypeResult(
64+
new VarcharType(),
65+
$arg->isNullable,
66+
null,
67+
$arg->knowledgeBase,
68+
);
69+
}
70+
}

tests/Analyser/AnalyserTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ private function provideValidFunctionCallTestData(): iterable
862862
$selects["CEIL({$label1})"] = "SELECT CEIL({$value1})";
863863
$selects["CEILING({$label1})"] = "SELECT CEILING({$value1})";
864864
$selects["CONCAT({$label1})"] = "SELECT CONCAT({$value1})";
865+
$selects["LOWER({$label1})"] = "SELECT LOWER({$value1})";
866+
$selects["LCASE({$label1})"] = "SELECT LCASE({$value1})";
867+
$selects["UPPER({$label1})"] = "SELECT UPPER({$value1})";
868+
$selects["UCASE({$label1})"] = "SELECT UCASE({$value1})";
865869
}
866870

867871
// TODO: figure out the context in which the function is called and adjust the return type accordingly.
@@ -1630,6 +1634,10 @@ public function provideValidNullabilityTestData(): iterable
16301634
'CONCAT(col_vchar) IS NOT NULL',
16311635
'CONCAT(col_vchar, col_int) IS NOT NULL',
16321636
'CONCAT(col_vchar, col_int) IS NULL',
1637+
'LOWER(col_vchar)',
1638+
'NOT LCASE(col_vchar)',
1639+
'UPPER(col_vchar) IS NULL',
1640+
'UCASE(col_vchar) IS NOT NULL',
16331641
];
16341642

16351643
foreach (['COALESCE', 'IFNULL', 'NVL'] as $coalesceFn) {

tests/code/Parser/MariaDbParser/invalid/function_calls.test

+57-1
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,60 @@ SELECT CONCAT()
12301230
MariaStan\Parser\Exception\ParserException
12311231
Function CONCAT takes at least 1 argument, got 0.
12321232
#####
1233-
1582: Incorrect parameter count in the call to native function 'CONCAT'
1233+
1582: Incorrect parameter count in the call to native function 'CONCAT'
1234+
-----
1235+
SELECT LOWER()
1236+
-----
1237+
MariaStan\Parser\Exception\ParserException
1238+
Function LOWER takes 1 argument, got 0.
1239+
#####
1240+
1582: Incorrect parameter count in the call to native function 'LOWER'
1241+
-----
1242+
SELECT LCASE()
1243+
-----
1244+
MariaStan\Parser\Exception\ParserException
1245+
Function LCASE takes 1 argument, got 0.
1246+
#####
1247+
1582: Incorrect parameter count in the call to native function 'LCASE'
1248+
-----
1249+
SELECT UPPER()
1250+
-----
1251+
MariaStan\Parser\Exception\ParserException
1252+
Function UPPER takes 1 argument, got 0.
1253+
#####
1254+
1582: Incorrect parameter count in the call to native function 'UPPER'
1255+
-----
1256+
SELECT UCASE()
1257+
-----
1258+
MariaStan\Parser\Exception\ParserException
1259+
Function UCASE takes 1 argument, got 0.
1260+
#####
1261+
1582: Incorrect parameter count in the call to native function 'UCASE'
1262+
-----
1263+
SELECT LOWER(1, 2)
1264+
-----
1265+
MariaStan\Parser\Exception\ParserException
1266+
Function LOWER takes 1 argument, got 2.
1267+
#####
1268+
1582: Incorrect parameter count in the call to native function 'LOWER'
1269+
-----
1270+
SELECT LCASE(1, 2)
1271+
-----
1272+
MariaStan\Parser\Exception\ParserException
1273+
Function LCASE takes 1 argument, got 2.
1274+
#####
1275+
1582: Incorrect parameter count in the call to native function 'LCASE'
1276+
-----
1277+
SELECT UPPER(1, 2)
1278+
-----
1279+
MariaStan\Parser\Exception\ParserException
1280+
Function UPPER takes 1 argument, got 2.
1281+
#####
1282+
1582: Incorrect parameter count in the call to native function 'UPPER'
1283+
-----
1284+
SELECT UCASE(1, 2)
1285+
-----
1286+
MariaStan\Parser\Exception\ParserException
1287+
Function UCASE takes 1 argument, got 2.
1288+
#####
1289+
1582: Incorrect parameter count in the call to native function 'UCASE'

tests/code/Parser/MariaDbParser/valid/function_calls.test

+71
Original file line numberDiff line numberDiff line change
@@ -6380,4 +6380,75 @@ MariaStan\Ast\Query\SelectQuery\SimpleSelectQuery
63806380
)
63816381
[isDistinct] => false
63826382
[isSqlCalcFoundRows] => false
6383+
)
6384+
-----
6385+
SELECT LOWER(1), LCASE(NULL), UPPER('a'), UCASE(1.1)
6386+
-----
6387+
MariaStan\Ast\Query\SelectQuery\SimpleSelectQuery
6388+
(
6389+
[select] => Array
6390+
(
6391+
[0] => MariaStan\Ast\SelectExpr\RegularExpr
6392+
(
6393+
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
6394+
(
6395+
[name] => LOWER
6396+
[arguments] => Array
6397+
(
6398+
[0] => MariaStan\Ast\Expr\LiteralInt
6399+
(
6400+
[value] => 1
6401+
)
6402+
)
6403+
[isDistinct] => false
6404+
)
6405+
)
6406+
[1] => MariaStan\Ast\SelectExpr\RegularExpr
6407+
(
6408+
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
6409+
(
6410+
[name] => LCASE
6411+
[arguments] => Array
6412+
(
6413+
[0] => MariaStan\Ast\Expr\LiteralNull
6414+
(
6415+
)
6416+
)
6417+
[isDistinct] => false
6418+
)
6419+
)
6420+
[2] => MariaStan\Ast\SelectExpr\RegularExpr
6421+
(
6422+
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
6423+
(
6424+
[name] => UPPER
6425+
[arguments] => Array
6426+
(
6427+
[0] => MariaStan\Ast\Expr\LiteralString
6428+
(
6429+
[value] => a
6430+
[firstConcatPart] => a
6431+
)
6432+
)
6433+
[isDistinct] => false
6434+
)
6435+
)
6436+
[3] => MariaStan\Ast\SelectExpr\RegularExpr
6437+
(
6438+
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
6439+
(
6440+
[name] => UCASE
6441+
[arguments] => Array
6442+
(
6443+
[0] => MariaStan\Ast\Expr\LiteralFloat
6444+
(
6445+
[value] => 1.1
6446+
)
6447+
)
6448+
[isDistinct] => false
6449+
)
6450+
)
6451+
)
6452+
[isDistinct] => false
6453+
[isSqlCalcFoundRows] => false
63836454
)

0 commit comments

Comments
 (0)