-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
int2vector
and oidvector
types used in system catalogs
- Loading branch information
1 parent
b32a1cf
commit 115f724
Showing
4 changed files
with
149 additions
and
10 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
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
71 changes: 71 additions & 0 deletions
71
src/sad_spirit/pg_wrapper/converters/containers/IntegerVectorConverter.php
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,71 @@ | ||
<?php | ||
|
||
/** | ||
* Converter of complex PostgreSQL types and an OO wrapper for PHP's pgsql extension | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to BSD 2-Clause License that is bundled | ||
* with this package in the file LICENSE and available at the URL | ||
* https://raw.githubusercontent.com/sad-spirit/pg-wrapper/master/LICENSE | ||
* | ||
* @package sad_spirit\pg_wrapper | ||
* @copyright 2014-2024 Alexey Borzov | ||
* @author Alexey Borzov <avb@php.net> | ||
* @license https://opensource.org/licenses/BSD-2-Clause BSD 2-Clause license | ||
* @link https://github.com/sad-spirit/pg-wrapper | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace sad_spirit\pg_wrapper\converters\containers; | ||
|
||
use sad_spirit\pg_wrapper\converters\ContainerConverter; | ||
use sad_spirit\pg_wrapper\converters\IntegerConverter; | ||
use sad_spirit\pg_wrapper\exceptions\TypeConversionException; | ||
|
||
/** | ||
* Converter for int2vector and oidvector types used in system catalogs | ||
* | ||
* NB: we do not implement ConnectionAware here as (currently) input routines in Postgres 16+ do not accept | ||
* special numeric literals | ||
*/ | ||
class IntegerVectorConverter extends ContainerConverter | ||
{ | ||
/** @var IntegerConverter */ | ||
private $integerConverter; | ||
|
||
public function __construct() | ||
{ | ||
$this->integerConverter = new IntegerConverter(); | ||
} | ||
|
||
protected function outputNotNull($value): string | ||
{ | ||
if (!\is_array($value)) { | ||
throw TypeConversionException::unexpectedValue($this, 'output', 'array', $value); | ||
} | ||
|
||
return \implode(' ', \array_map(function ($v) { | ||
return $this->integerConverter->outputNotNull($v); | ||
}, $value)); | ||
} | ||
|
||
protected function parseInput(string $native, int &$pos) | ||
{ | ||
$values = []; | ||
|
||
while (null !== $this->nextChar($native, $pos)) { | ||
$length = \strcspn($native, " \n\r\t", $pos); | ||
$values[] = $this->integerConverter->input(\substr($native, $pos, $length)); | ||
$pos += $length; | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function dimensions(): int | ||
{ | ||
return 1; | ||
} | ||
} |
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 | ||
|
||
/** | ||
* Converter of complex PostgreSQL types and an OO wrapper for PHP's pgsql extension | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to BSD 2-Clause License that is bundled | ||
* with this package in the file LICENSE and available at the URL | ||
* https://raw.githubusercontent.com/sad-spirit/pg-wrapper/master/LICENSE | ||
* | ||
* @package sad_spirit\pg_wrapper | ||
* @copyright 2014-2024 Alexey Borzov | ||
* @author Alexey Borzov <avb@php.net> | ||
* @license https://opensource.org/licenses/BSD-2-Clause BSD 2-Clause license | ||
* @link https://github.com/sad-spirit/pg-wrapper | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace sad_spirit\pg_wrapper\tests\converters; | ||
|
||
use sad_spirit\pg_wrapper\converters\containers\IntegerVectorConverter; | ||
use sad_spirit\pg_wrapper\exceptions\TypeConversionException; | ||
|
||
/** | ||
* Unit test for int2vector / oidvector type converter | ||
* | ||
* @extends TypeConverterTestCase<IntegerVectorConverter> | ||
*/ | ||
class IntegerVectorConverterTest extends TypeConverterTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this->converter = new IntegerVectorConverter(); | ||
} | ||
|
||
public function valuesBoth(): array | ||
{ | ||
return [ | ||
[null, null], | ||
['', []], | ||
['1', [1]], | ||
['1 2', [1, 2]] | ||
]; | ||
} | ||
|
||
public function valuesFrom(): array | ||
{ | ||
return [ | ||
[' 1 2 ', [1, 2]], | ||
['1 foo', new TypeConversionException()] | ||
]; | ||
} | ||
|
||
public function valuesTo(): array | ||
{ | ||
return [ | ||
[new TypeConversionException(), [1, 'foo']], | ||
[new TypeConversionException(), [1, null]] | ||
]; | ||
} | ||
} |