Skip to content

Commit

Permalink
analyse COLLATE operator
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Jun 2, 2023
1 parent e5a7a88 commit 32c17f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Analyser/AnalyserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,16 @@ private function resolveExprType(Expr\Expr $expr, ?AnalyserConditionTypeEnum $co
new Schema\DbType\MixedType(),
true,
);
case Expr\ExprTypeEnum::COLLATE:
assert($expr instanceof Expr\Collate);
$subresult = $this->resolveExprType($expr->expression, $condition);

return new ExprTypeResult(
new Schema\DbType\VarcharType(),
$subresult->isNullable,
null,
$subresult->knowledgeBase,
);
default:
$this->errors[] = new AnalyserError("Unhandled expression type: {$expr::getExprType()->value}");

Expand Down
10 changes: 10 additions & 0 deletions tests/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ private function provideValidOperatorTestData(): iterable
'BINARY 1 + 2',
'BINARY (1 + 2)',
'BINARY NULL',
'"a" COLLATE utf8mb4_unicode_ci',
'name COLLATE utf8mb4_unicode_ci FROM analyser_test',
'id COLLATE utf8mb4_unicode_ci FROM analyser_test',
];

foreach ($exprs as $expr) {
Expand Down Expand Up @@ -1473,6 +1476,7 @@ public function provideValidNullabilityTestData(): iterable
'~(col_vchar IS NOT NULL)',
'col_vchar AND DATE(NOW())',
'col_vchar OR DATE(NOW())',
'col_vchar COLLATE utf8mb4_unicode_ci IS NOT NULL',
];

foreach (['+', '-', '~', 'BINARY'] as $unaryOp) {
Expand Down Expand Up @@ -2184,6 +2188,12 @@ private function provideInvalidColumnTestData(): iterable
'error' => AnalyserErrorMessageBuilder::createUnknownColumnErrorMessage('aaa'),
'DB error code' => MariaDbErrorCodes::ER_BAD_FIELD_ERROR,
];

yield 'unknown column in COLLATE' => [
'query' => 'SELECT aaa COLLATE utf8mb4_unicode_ci FROM analyser_test',
'error' => AnalyserErrorMessageBuilder::createUnknownColumnErrorMessage('aaa'),
'DB error code' => MariaDbErrorCodes::ER_BAD_FIELD_ERROR,
];
}

/** @return iterable<string, array<mixed>> */
Expand Down

0 comments on commit 32c17f4

Please sign in to comment.