-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from pfilsx/dev
Dev
- Loading branch information
Showing
12 changed files
with
517 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
Available types | ||
=============== | ||
|
||
| PostgreSQL type | Register as | Implementation | | ||
|-----------------|-------------|-----------------------------------------------------------------------------------------| | ||
| _bool | bool[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray](../src/DBAL/Type/BooleanArray.php) | | ||
| _int2 | smallint[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray](../src/DBAL/Type/SmallIntArray.php) | | ||
| _int4 | integer[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray](../src/DBAL/Type/IntegerArray.php) | | ||
| _int8 | bigint[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray](../src/DBAL/Type/BigIntArray.php) | | ||
| _text | text[] | [Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray](../src/DBAL/Type/TextArray.php) | | ||
|
||
Integration with Doctrine | ||
========================= | ||
|
||
```php | ||
<?php | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
|
||
Type::addType('bool[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray'); | ||
Type::addType('smallint[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray'); | ||
Type::addType('integer[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray'); | ||
Type::addType('bigint[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray'); | ||
Type::addType('text[]', 'Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray'); | ||
|
||
// ... | ||
|
||
$platform = $em->getConnection()->getDatabasePlatform(); | ||
$platform->registerDoctrineTypeMapping('bool[]','bool[]'); | ||
$platform->registerDoctrineTypeMapping('_bool','bool[]'); | ||
$platform->registerDoctrineTypeMapping('integer[]','integer[]'); | ||
$platform->registerDoctrineTypeMapping('_int4','integer[]'); | ||
$platform->registerDoctrineTypeMapping('bigint[]','bigint[]'); | ||
$platform->registerDoctrineTypeMapping('_int8','bigint[]'); | ||
$platform->registerDoctrineTypeMapping('text[]','text[]'); | ||
$platform->registerDoctrineTypeMapping('_text','text[]'); | ||
``` | ||
|
||
Integration with Symfony | ||
========================= | ||
```yaml | ||
# config/packages/doctrine.yaml | ||
doctrine: | ||
dbal: | ||
types: | ||
bool[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\BooleanArray | ||
smallint[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\SmallIntArray | ||
integer[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\IntegerArray | ||
bigint[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\BigIntArray | ||
text[]: Pfilsx\PostgreSQLDoctrine\DBAL\Type\TextArray | ||
|
||
mapping_types: | ||
bool[]: bool[] | ||
_bool: bool[] | ||
smallint[]: smallint[] | ||
_int2: smallint[] | ||
integer[]: integer[] | ||
_int4: integer[] | ||
bigint[]: bigint[] | ||
_int8: bigint[] | ||
text[]: text[] | ||
_text: text[] | ||
# or | ||
connections: | ||
connection_name: | ||
mapping_types: | ||
bool[]: bool[] | ||
_bool: bool[] | ||
smallint[]: smallint[] | ||
_int2: smallint[] | ||
integer[]: integer[] | ||
_int4: integer[] | ||
bigint[]: bigint[] | ||
_int8: bigint[] | ||
text[]: text[] | ||
_text: text[] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\Type; | ||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
use Pfilsx\PostgreSQLDoctrine\Tools\ArrayTypeTool; | ||
|
||
abstract class AbstractArrayType extends Type | ||
{ | ||
/** | ||
* @param array<string, mixed> $column | ||
* @param AbstractPlatform $platform | ||
* | ||
* @throws \Doctrine\DBAL\Exception | ||
* | ||
* @return string | ||
*/ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getDoctrineTypeMapping($this->getName()); | ||
} | ||
|
||
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!\is_array($value)) { | ||
throw new ConversionException(\sprintf('Invalid value type. Expected "array", "%s" provided.', \get_debug_type($value))); | ||
} | ||
|
||
return ArrayTypeTool::convertPHPArrayToDatabaseArrayString($value, static::getArrayType()); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param AbstractPlatform $platform | ||
* | ||
* @throws ConversionException | ||
* | ||
* @return null|bool[]|int[]|string[] | ||
*/ | ||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?array | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!\is_string($value)) { | ||
throw new ConversionException(\sprintf('Invalid database value type. Expected "string", "%s" provided.', \get_debug_type($value))); | ||
} | ||
|
||
return ArrayTypeTool::convertDatabaseArrayStringToPHPArray($value, static::getArrayType()); | ||
} | ||
|
||
abstract protected static function getArrayType(): ArrayTypeEnum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
|
||
/** | ||
* Implementation of PostgreSql BIGINT[] data type. | ||
* | ||
* @see https://www.postgresql.org/docs/current/arrays.html | ||
*/ | ||
class BigIntArray extends AbstractArrayType | ||
{ | ||
protected static function getArrayType(): ArrayTypeEnum | ||
{ | ||
return ArrayTypeEnum::BigIntArray; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'bigint[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
|
||
/** | ||
* Implementation of PostgreSql BOOL[] data type. | ||
* | ||
* @see https://www.postgresql.org/docs/current/arrays.html | ||
*/ | ||
class BooleanArray extends AbstractArrayType | ||
{ | ||
protected static function getArrayType(): ArrayTypeEnum | ||
{ | ||
return ArrayTypeEnum::BooleanArray; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'bool[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
|
||
/** | ||
* Implementation of PostgreSql INTEGER[] data type. | ||
* | ||
* @see https://www.postgresql.org/docs/current/arrays.html | ||
*/ | ||
class IntegerArray extends AbstractArrayType | ||
{ | ||
protected static function getArrayType(): ArrayTypeEnum | ||
{ | ||
return ArrayTypeEnum::IntArray; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'integer[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
|
||
/** | ||
* Implementation of PostgreSql SMALLINT[] data type. | ||
* | ||
* @see https://www.postgresql.org/docs/current/arrays.html | ||
*/ | ||
class SmallIntArray extends AbstractArrayType | ||
{ | ||
protected static function getArrayType(): ArrayTypeEnum | ||
{ | ||
return ArrayTypeEnum::SmallIntArray; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'smallint[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Type; | ||
|
||
use Pfilsx\PostgreSQLDoctrine\Enum\ArrayTypeEnum; | ||
|
||
/** | ||
* Implementation of PostgreSql TEXT[] data type. | ||
* | ||
* @see https://www.postgresql.org/docs/current/arrays.html | ||
*/ | ||
class TextArray extends AbstractArrayType | ||
{ | ||
protected static function getArrayType(): ArrayTypeEnum | ||
{ | ||
return ArrayTypeEnum::TextArray; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'text[]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\PostgreSQLDoctrine\Enum; | ||
|
||
enum ArrayTypeEnum: string | ||
{ | ||
case SmallIntArray = 'smallint'; | ||
case IntArray = 'integer'; | ||
case BigIntArray = 'bigint'; | ||
case TextArray = 'text'; | ||
case BooleanArray = 'boolean'; | ||
} |
Oops, something went wrong.