Skip to content

Commit

Permalink
add function info for LOWER/UPPER
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Jun 15, 2024
1 parent 92bb88d commit 005b7a0
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Database/FunctionInfo/FunctionInfoRegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function createDefaultFunctionInfos(): array
new FoundRows(),
new IfFunction(),
new GroupConcat(),
new LowerUpper(),
new MaxMin(),
new Now(),
new Round(),
Expand Down
70 changes: 70 additions & 0 deletions src/Database/FunctionInfo/LowerUpper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace MariaStan\Database\FunctionInfo;

use MariaStan\Analyser\AnalyserConditionTypeEnum;
use MariaStan\Analyser\ExprTypeResult;
use MariaStan\Ast\Expr\FunctionCall\FunctionCall;
use MariaStan\Parser\Exception\ParserException;
use MariaStan\Schema\DbType\VarcharType;

use function count;

final class LowerUpper implements FunctionInfo
{
/** @inheritDoc */
public function getSupportedFunctionNames(): array
{
return ['LOWER', 'LCASE', 'UPPER', 'UCASE'];
}

public function getFunctionType(): FunctionTypeEnum
{
return FunctionTypeEnum::SIMPLE;
}

public function checkSyntaxErrors(FunctionCall $functionCall): void
{
$args = $functionCall->getArguments();
$argCount = count($args);

if ($argCount === 1) {
return;
}

throw new ParserException(
FunctionInfoHelper::createArgumentCountErrorMessageFixed(
$functionCall->getFunctionName(),
1,
$argCount,
),
);
}

/** @inheritDoc */
public function getInnerConditions(?AnalyserConditionTypeEnum $condition, array $arguments): array
{
return [$condition];
}

/**
* @inheritDoc
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter
*/
public function getReturnType(
FunctionCall $functionCall,
array $argumentTypes,
?AnalyserConditionTypeEnum $condition,
): ExprTypeResult {
$arg = $argumentTypes[0];

return new ExprTypeResult(
new VarcharType(),
$arg->isNullable,
null,
$arg->knowledgeBase,
);
}
}
8 changes: 8 additions & 0 deletions tests/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ private function provideValidFunctionCallTestData(): iterable
$selects["CEIL({$label1})"] = "SELECT CEIL({$value1})";
$selects["CEILING({$label1})"] = "SELECT CEILING({$value1})";
$selects["CONCAT({$label1})"] = "SELECT CONCAT({$value1})";
$selects["LOWER({$label1})"] = "SELECT LOWER({$value1})";
$selects["LCASE({$label1})"] = "SELECT LCASE({$value1})";
$selects["UPPER({$label1})"] = "SELECT UPPER({$value1})";
$selects["UCASE({$label1})"] = "SELECT UCASE({$value1})";
}

// TODO: figure out the context in which the function is called and adjust the return type accordingly.
Expand Down Expand Up @@ -1630,6 +1634,10 @@ public function provideValidNullabilityTestData(): iterable
'CONCAT(col_vchar) IS NOT NULL',
'CONCAT(col_vchar, col_int) IS NOT NULL',
'CONCAT(col_vchar, col_int) IS NULL',
'LOWER(col_vchar)',
'NOT LCASE(col_vchar)',
'UPPER(col_vchar) IS NULL',
'UCASE(col_vchar) IS NOT NULL',
];

foreach (['COALESCE', 'IFNULL', 'NVL'] as $coalesceFn) {
Expand Down
58 changes: 57 additions & 1 deletion tests/code/Parser/MariaDbParser/invalid/function_calls.test
Original file line number Diff line number Diff line change
Expand Up @@ -1230,4 +1230,60 @@ SELECT CONCAT()
MariaStan\Parser\Exception\ParserException
Function CONCAT takes at least 1 argument, got 0.
#####
1582: Incorrect parameter count in the call to native function 'CONCAT'
1582: Incorrect parameter count in the call to native function 'CONCAT'
-----
SELECT LOWER()
-----
MariaStan\Parser\Exception\ParserException
Function LOWER takes 1 argument, got 0.
#####
1582: Incorrect parameter count in the call to native function 'LOWER'
-----
SELECT LCASE()
-----
MariaStan\Parser\Exception\ParserException
Function LCASE takes 1 argument, got 0.
#####
1582: Incorrect parameter count in the call to native function 'LCASE'
-----
SELECT UPPER()
-----
MariaStan\Parser\Exception\ParserException
Function UPPER takes 1 argument, got 0.
#####
1582: Incorrect parameter count in the call to native function 'UPPER'
-----
SELECT UCASE()
-----
MariaStan\Parser\Exception\ParserException
Function UCASE takes 1 argument, got 0.
#####
1582: Incorrect parameter count in the call to native function 'UCASE'
-----
SELECT LOWER(1, 2)
-----
MariaStan\Parser\Exception\ParserException
Function LOWER takes 1 argument, got 2.
#####
1582: Incorrect parameter count in the call to native function 'LOWER'
-----
SELECT LCASE(1, 2)
-----
MariaStan\Parser\Exception\ParserException
Function LCASE takes 1 argument, got 2.
#####
1582: Incorrect parameter count in the call to native function 'LCASE'
-----
SELECT UPPER(1, 2)
-----
MariaStan\Parser\Exception\ParserException
Function UPPER takes 1 argument, got 2.
#####
1582: Incorrect parameter count in the call to native function 'UPPER'
-----
SELECT UCASE(1, 2)
-----
MariaStan\Parser\Exception\ParserException
Function UCASE takes 1 argument, got 2.
#####
1582: Incorrect parameter count in the call to native function 'UCASE'
71 changes: 71 additions & 0 deletions tests/code/Parser/MariaDbParser/valid/function_calls.test
Original file line number Diff line number Diff line change
Expand Up @@ -6380,4 +6380,75 @@ MariaStan\Ast\Query\SelectQuery\SimpleSelectQuery
)
[isDistinct] => false
[isSqlCalcFoundRows] => false
)
-----
SELECT LOWER(1), LCASE(NULL), UPPER('a'), UCASE(1.1)
-----
MariaStan\Ast\Query\SelectQuery\SimpleSelectQuery
(
[select] => Array
(
[0] => MariaStan\Ast\SelectExpr\RegularExpr
(
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
(
[name] => LOWER
[arguments] => Array
(
[0] => MariaStan\Ast\Expr\LiteralInt
(
[value] => 1
)
)
[isDistinct] => false
)
)
[1] => MariaStan\Ast\SelectExpr\RegularExpr
(
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
(
[name] => LCASE
[arguments] => Array
(
[0] => MariaStan\Ast\Expr\LiteralNull
(
)
)
[isDistinct] => false
)
)
[2] => MariaStan\Ast\SelectExpr\RegularExpr
(
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
(
[name] => UPPER
[arguments] => Array
(
[0] => MariaStan\Ast\Expr\LiteralString
(
[value] => a
[firstConcatPart] => a
)
)
[isDistinct] => false
)
)
[3] => MariaStan\Ast\SelectExpr\RegularExpr
(
[expr] => MariaStan\Ast\Expr\FunctionCall\StandardFunctionCall
(
[name] => UCASE
[arguments] => Array
(
[0] => MariaStan\Ast\Expr\LiteralFloat
(
[value] => 1.1
)
)
[isDistinct] => false
)
)
)
[isDistinct] => false
[isSqlCalcFoundRows] => false
)

0 comments on commit 005b7a0

Please sign in to comment.