Skip to content

Commit a56c37f

Browse files
authored
Merge pull request #4 from pfilsx/array_types
add array types
2 parents 5427a61 + d226517 commit a56c37f

File tree

12 files changed

+517
-6
lines changed

12 files changed

+517
-6
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@ PostgreSQL Doctrine
88
Description
99
------------
1010

11-
Provides extended Doctrine DBAL and Doctrine migration classes to allow you to use PostgreSQL
12-
specific features such as [enums](https://www.postgresql.org/docs/current/datatype-enum.html) or JSON(B) with Doctrine.
11+
Provides extended Doctrine and Doctrine migrations PostgreSQL support with
12+
specific features such as [enums](https://www.postgresql.org/docs/current/datatype-enum.html), arrays and aggregate and JSON(B) functions.
1313

1414
Features
1515
--------
16-
* PostgreSQL enums support in DBAL and migrations
16+
* PostgreSQL enums support in DBAL, ORM and migrations
1717
* PHP8 enum support
1818
* Fix creating [default schema in down migrations for pgsql](https://github.com/doctrine/dbal/issues/1110)
1919
* [JSON(B) functions](https://www.postgresql.org/docs/current/functions-json.html) (in progress)
2020
* JSON(B) types based on object models (in progress, requires symfony/serializer)
2121
* [Trait](src/ORM/Trait/ExistsMethodRepositoryTrait.php) for easy use of [SELECT EXISTS(...)](https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-EXISTS) in your entity repositories
22+
* Aggregate functions with filter condition support
23+
* Array types
2224

2325
Requirement
2426
-----------
2527
* PHP ^8.1
2628
* doctrine/dbal ^3.5.1
2729
* doctrine/migrations ^3.5.2
2830
* symfony/serializer >=5.4.* (optional for json models)
29-
* symfony/property-info >=5.4.* (optional)
31+
* symfony/property-info >=5.4.* (optional for json models)
3032

3133
Installation
3234
------------
@@ -46,14 +48,16 @@ for instructions on how to override the default doctrine classes in your project
4648
Required steps:
4749
1. Register [PostgreSQLDriverMiddleware.php](src/DBAL/Middleware/PostgreSQLDriverMiddleware.php) as driver middleware
4850
2. Register [OrmSchemaProvider.php](src/Migrations/Provider/OrmSchemaProvider.php) as Doctrine\Migrations\Provider\SchemaProvider in Doctrine\Migrations\DependencyFactory
51+
3. Register types and functions on your needs
4952

5053
For Symfony integration see [PostgreSQLDoctrineBundle](https://github.com/pfilsx/PostgreSQLDoctrineBundle)
5154

5255
Documentation
5356
-------------
5457

55-
* [ENUMS](docs/ENUMS.md)
56-
* [Functions](docs/FUNCTIONS-AND-OPERATORS.md)
58+
* [Enums](docs/Enums.md)
59+
* [Functions](docs/Functions-and-Operators.md)
60+
* [Types](docs/Types.md)
5761

5862
License
5963
-------
File renamed without changes.
File renamed without changes.

docs/Types.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Available types
2+
===============
3+
4+
| PostgreSQL type | Register as | Implementation |
5+
|-----------------|-------------|-----------------------------------------------------------------------------------------|
6+
| _bool | bool[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray](../src/DBAL/Type/BooleanArray.php) |
7+
| _int2 | smallint[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray](../src/DBAL/Type/SmallIntArray.php) |
8+
| _int4 | integer[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray](../src/DBAL/Type/IntegerArray.php) |
9+
| _int8 | bigint[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray](../src/DBAL/Type/BigIntArray.php) |
10+
| _text | text[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray](../src/DBAL/Type/TextArray.php) |
11+
12+
Integration with Doctrine
13+
=========================
14+
15+
```php
16+
<?php
17+
18+
use Doctrine\DBAL\Types\Type;
19+
20+
Type::addType('bool[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray');
21+
Type::addType('smallint[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray');
22+
Type::addType('integer[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray');
23+
Type::addType('bigint[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray');
24+
Type::addType('text[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray');
25+
26+
// ...
27+
28+
$platform = $em->getConnection()->getDatabasePlatform();
29+
$platform->registerDoctrineTypeMapping('bool[]','bool[]');
30+
$platform->registerDoctrineTypeMapping('_bool','bool[]');
31+
$platform->registerDoctrineTypeMapping('integer[]','integer[]');
32+
$platform->registerDoctrineTypeMapping('_int4','integer[]');
33+
$platform->registerDoctrineTypeMapping('bigint[]','bigint[]');
34+
$platform->registerDoctrineTypeMapping('_int8','bigint[]');
35+
$platform->registerDoctrineTypeMapping('text[]','text[]');
36+
$platform->registerDoctrineTypeMapping('_text','text[]');
37+
```
38+
39+
Integration with Symfony
40+
=========================
41+
```yaml
42+
# config/packages/doctrine.yaml
43+
doctrine:
44+
dbal:
45+
types:
46+
bool[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray
47+
smallint[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray
48+
integer[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray
49+
bigint[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray
50+
text[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray
51+
52+
mapping_types:
53+
bool[]: bool[]
54+
_bool: bool[]
55+
smallint[]: smallint[]
56+
_int2: smallint[]
57+
integer[]: integer[]
58+
_int4: integer[]
59+
bigint[]: bigint[]
60+
_int8: bigint[]
61+
text[]: text[]
62+
_text: text[]
63+
# or
64+
connections:
65+
connection_name:
66+
mapping_types:
67+
bool[]: bool[]
68+
_bool: bool[]
69+
smallint[]: smallint[]
70+
_int2: smallint[]
71+
integer[]: integer[]
72+
_int4: integer[]
73+
bigint[]: bigint[]
74+
_int8: bigint[]
75+
text[]: text[]
76+
_text: text[]
77+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use Doctrine\DBAL\Types\Type;
10+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
11+
use Pfilsx\PostgreSQLDoctrine\Tools\ArrayTypeTool;
12+
13+
abstract class AbstractArrayType extends Type
14+
{
15+
/**
16+
* @param array<string, mixed> $column
17+
* @param AbstractPlatform $platform
18+
*
19+
* @throws \Doctrine\DBAL\Exception
20+
*
21+
* @return string
22+
*/
23+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
24+
{
25+
return $platform->getDoctrineTypeMapping($this->getName());
26+
}
27+
28+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
29+
{
30+
if ($value === null) {
31+
return null;
32+
}
33+
34+
if (!\is_array($value)) {
35+
throw new ConversionException(\sprintf('Invalid value type. Expected "array", "%s" provided.', \get_debug_type($value)));
36+
}
37+
38+
return ArrayTypeTool::convertPHPArrayToDatabaseArrayString($value, static::getArrayType());
39+
}
40+
41+
/**
42+
* @param mixed $value
43+
* @param AbstractPlatform $platform
44+
*
45+
* @throws ConversionException
46+
*
47+
* @return null|bool[]|int[]|string[]
48+
*/
49+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?array
50+
{
51+
if ($value === null) {
52+
return null;
53+
}
54+
55+
if (!\is_string($value)) {
56+
throw new ConversionException(\sprintf('Invalid database value type. Expected "string", "%s" provided.', \get_debug_type($value)));
57+
}
58+
59+
return ArrayTypeTool::convertDatabaseArrayStringToPHPArray($value, static::getArrayType());
60+
}
61+
62+
abstract protected static function getArrayType(): ArrayTypeEnum;
63+
}

src/DBAL/Type/BigIntArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
8+
9+
/**
10+
* Implementation of PostgreSql BIGINT[] data type.
11+
*
12+
* @see https://www.postgresql.org/docs/current/arrays.html
13+
*/
14+
class BigIntArray extends AbstractArrayType
15+
{
16+
protected static function getArrayType(): ArrayTypeEnum
17+
{
18+
return ArrayTypeEnum::BigIntArray;
19+
}
20+
21+
public function getName(): string
22+
{
23+
return 'bigint[]';
24+
}
25+
}

src/DBAL/Type/BooleanArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
8+
9+
/**
10+
* Implementation of PostgreSql BOOL[] data type.
11+
*
12+
* @see https://www.postgresql.org/docs/current/arrays.html
13+
*/
14+
class BooleanArray extends AbstractArrayType
15+
{
16+
protected static function getArrayType(): ArrayTypeEnum
17+
{
18+
return ArrayTypeEnum::BooleanArray;
19+
}
20+
21+
public function getName(): string
22+
{
23+
return 'bool[]';
24+
}
25+
}

src/DBAL/Type/IntegerArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
8+
9+
/**
10+
* Implementation of PostgreSql INTEGER[] data type.
11+
*
12+
* @see https://www.postgresql.org/docs/current/arrays.html
13+
*/
14+
class IntegerArray extends AbstractArrayType
15+
{
16+
protected static function getArrayType(): ArrayTypeEnum
17+
{
18+
return ArrayTypeEnum::IntArray;
19+
}
20+
21+
public function getName(): string
22+
{
23+
return 'integer[]';
24+
}
25+
}

src/DBAL/Type/SmallIntArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
8+
9+
/**
10+
* Implementation of PostgreSql SMALLINT[] data type.
11+
*
12+
* @see https://www.postgresql.org/docs/current/arrays.html
13+
*/
14+
class SmallIntArray extends AbstractArrayType
15+
{
16+
protected static function getArrayType(): ArrayTypeEnum
17+
{
18+
return ArrayTypeEnum::SmallIntArray;
19+
}
20+
21+
public function getName(): string
22+
{
23+
return 'smallint[]';
24+
}
25+
}

src/DBAL/Type/TextArray.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type;
6+
7+
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum;
8+
9+
/**
10+
* Implementation of PostgreSql TEXT[] data type.
11+
*
12+
* @see https://www.postgresql.org/docs/current/arrays.html
13+
*/
14+
class TextArray extends AbstractArrayType
15+
{
16+
protected static function getArrayType(): ArrayTypeEnum
17+
{
18+
return ArrayTypeEnum::TextArray;
19+
}
20+
21+
public function getName(): string
22+
{
23+
return 'text[]';
24+
}
25+
}

0 commit comments

Comments
 (0)