Skip to content

Commit

Permalink
Merge pull request #87 from oroinc/cast_uuid_with_tests_2.x
Browse files Browse the repository at this point in the history
UUID support for CAST
  • Loading branch information
x86demon authored Sep 22, 2023
2 parents 612a68c + db4efbf commit d120066
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Available functions:
* `ROUND(value, ?precision)` - Rounds the value to the specified precision (defaults to 0 precision if not specified).
* `CEIL(value)` - Returns the value rounded up.
* `SIGN(expr)` - Returns the sign of the argument.
* `CAST(expr as type)` - Takes an expression of any type and produces a result value of a specified type. Supported types are: `char`, `string`, `text`, `date`, `datetime`, `time`, `int`, `integer`, `bigint`, `decimal`, `boolean`, `binary`.
* `CAST(expr as type)` - Takes an expression of any type and produces a result value of a specified type. Supported types are: `char`, `string`, `text`, `date`, `datetime`, `time`, `int`, `integer`, `bigint`, `decimal`, `boolean`, `binary`, `uuid`.
* `CONCAT_WS` - Concatenate all but the first argument. The first argument is used as the separator string.
* `GROUP_CONCAT` - Returns a concatenated string. GROUP_CONCAT full syntax:
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"phpunit/phpunit": "9.*",
"doctrine/data-fixtures": "^1.3",
"symfony/yaml": "5.*",
"symfony/cache": "5.*",
"squizlabs/php_codesniffer": "3.5.*",
"doctrine/annotations": ">1.0, <2.0",
"symfony/cache": ">=v5.0.0"
"doctrine/annotations": ">1.0, <2.0"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/Oro/ORM/Query/AST/Functions/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Cast extends AbstractPlatformAwareFunctionNode
'json',
'bool',
'boolean',
'binary'
'binary',
'uuid'
];

public function parse(Parser $parser)
Expand Down
6 changes: 5 additions & 1 deletion src/Oro/ORM/Query/AST/Platform/Functions/Mysql/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ public function getSql(SqlWalker $sqlWalker): string
$value = $this->parameters[DqlFunction::PARAMETER_KEY];
$type = $this->parameters[DqlFunction::TYPE_KEY];

if ($type === 'json' && !$sqlWalker->getConnection()->getDatabasePlatform()->hasNativeJsonType()) {
$type = 'text';
}

$type = \strtolower($type);
$isBoolean = $type === 'bool' || $type === 'boolean';
if ($type === 'char') {
$type = 'char(1)';
} elseif ($type === 'string' || $type === 'text' || $type === 'json') {
} elseif ($type === 'string' || $type === 'text' || $type === 'uuid') {
$type = 'char';
} elseif ($type === 'int' || $type === 'integer' || $isBoolean) {
$type = 'signed';
Expand Down
1 change: 0 additions & 1 deletion tests/Oro/Tests/DBAL/Types/PercentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\Type;

use Doctrine\ORM\ORMException;
use Oro\DBAL\Types\PercentType;
use Oro\Tests\Connection\TestUtil;
Expand Down
15 changes: 8 additions & 7 deletions tests/Oro/Tests/ORM/AST/Query/Functions/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

use Doctrine\ORM\Configuration;
use Doctrine\ORM\Query;

use PHPUnit\Framework\Constraint\LogicalOr;
use Symfony\Component\Yaml\Yaml;

use Oro\Tests\Connection\TestUtil;
use Oro\Tests\TestCase;
use PHPUnit\Framework\Constraint\LogicalOr;
use Symfony\Component\Yaml\Yaml;

class FunctionsTest extends TestCase
{
Expand Down Expand Up @@ -65,11 +63,14 @@ public function functionsDataProvider(): array
if (!\is_array($fileData)) {
throw new \RuntimeException(\sprintf('Could not parse file %s', $file));
}
/** @noinspection SlowArrayOperationsInLoopInspection */
$data = \array_merge($data, $fileData);
$data[] = $fileData;
}

if (!$data) {
return [];
}

return $data;
return array_merge(...$data);
}

protected function registerDqlFunction(
Expand Down
33 changes: 20 additions & 13 deletions tests/Oro/Tests/ORM/AST/Query/Functions/fixtures/mysql/cast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,62 @@

#CHAR
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('56366a57-43fa-4f4e-a0a2-66f87d52b601' as uuid) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('56366a57-43fa-4f4e-a0a2-66f87d52b601' AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '56366a57-43fa-4f4e-a0a2-66f87d52b601'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('12' as char) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('12' AS char(1)) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- 1

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as char) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS char(1)) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2'

#STRING
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(12 as string) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(12 AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as string) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-04 05:06:07'

#TEXT
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(12 as text) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(12 AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as text) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-04 05:06:07'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(CONCAT('{\"a\":', f.id, '}') as json) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(CONCAT('{\\\"a\\\":', t0_.id, '}') AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
sql: "SELECT CAST(CONCAT('{\\\"a\\\":', t0_.id, '}') AS json) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '{"a":1}'
- '{"a": 1}'

#DECIMAL
- functions:
Expand All @@ -98,30 +105,30 @@

#DATE
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('2014-01-02 12:13:14' as date) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('2014-01-02 12:13:14' AS date) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-02'

#TIME
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('2014-01-02 12:13:14' as time) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('2014-01-02 12:13:14' AS time) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12:13:14'

#DATETIME
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "datetime" }
dql: "SELECT CAST('2014-01-02 12:13:14' as datetime) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('2014-01-02 12:13:14' AS datetime) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-02 12:13:14'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "datetime" }
dql: "SELECT CAST(f.createdAt as datetime) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS datetime) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,3 @@
sql: SELECT TIMESTAMPDIFF(YEAR, '2016-01-01 00:00:01', '2017-01-01 00:00:00') AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1
expectedResult:
- 0

Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,51 @@

#CHAR
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('56366a57-43fa-4f4e-a0a2-66f87d52b601' as uuid) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('56366a57-43fa-4f4e-a0a2-66f87d52b601' AS uuid) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '56366a57-43fa-4f4e-a0a2-66f87d52b601'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('12' as char) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('12' AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- 1

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as char) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS char) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2'

#STRING
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(12 as string) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(12 AS varchar) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as string) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS varchar) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-04 05:06:07'

#TEXT
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(12 as text) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(12 AS text) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST(f.createdAt as text) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST(t0_.created_at AS text) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
Expand Down Expand Up @@ -100,30 +107,30 @@

#DATE
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('2014-01-02 12:13:14' as date) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('2014-01-02 12:13:14' AS date) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '2014-01-02'

#TIME
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "string" }
dql: "SELECT CAST('2014-01-02 12:13:14' as time) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: "SELECT CAST('2014-01-02 12:13:14' AS time) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '12:13:14'

#DATETIME
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "datetime" }
dql: "SELECT CAST('2014-01-02 12:13:14' as datetime) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: SELECT "timestamp"('2014-01-02 12:13:14') AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1
expectedResult:
- '2014-01-02 12:13:14'

- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "datetime" }
dql: "SELECT CAST(f.createdAt as datetime) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql: SELECT "timestamp"(t0_.created_at) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1
expectedResult:
Expand Down

0 comments on commit d120066

Please sign in to comment.