Skip to content

Commit 4af7c40

Browse files
committed
refactor: adjusted adapter to recent client changes
- improved types mapping - reduced complexity of loaders - added benchamarks - fixed tin bugs on website
1 parent 21ce0ec commit 4af7c40

File tree

59 files changed

+2352
-803
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2352
-803
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\ETL\Adapter\PostgreSql;
6+
7+
use Flow\ETL\Adapter\PostgreSql\Exception\TypeMappingException;
8+
use Flow\ETL\Row\Entry;
9+
use Flow\ETL\Row\Entry\{BooleanEntry, DateEntry, DateTimeEntry, EnumEntry, FloatEntry, HTMLElementEntry, HTMLEntry, IntegerEntry, JsonEntry, ListEntry, MapEntry, StringEntry, StructureEntry, TimeEntry, UuidEntry, XMLElementEntry, XMLEntry};
10+
use Flow\PostgreSql\Client\TypedValue;
11+
use Flow\PostgreSql\Client\Types\PostgreSqlType;
12+
13+
/**
14+
* Maps ETL Entry types to PostgreSQL types.
15+
*
16+
* Users can customize the mapping by passing overrides to the constructor.
17+
* Entry classes not in the map will throw TypeMappingException.
18+
*
19+
* Example usage:
20+
* ```php
21+
* // Use defaults
22+
* $map = new EntryTypesMap();
23+
*
24+
* // Override specific types
25+
* $map = new EntryTypesMap([
26+
* IntegerEntry::class => PostgreSqlType::INT2,
27+
* ListEntry::class => PostgreSqlType::TEXT_ARRAY,
28+
* ]);
29+
* ```
30+
*/
31+
final readonly class EntryTypesMap
32+
{
33+
/**
34+
* Default mapping of Entry classes to PostgreSQL types.
35+
*
36+
* @var array<class-string<Entry<mixed>>, PostgreSqlType>
37+
*/
38+
public const array DEFAULT_TYPES = [
39+
StringEntry::class => PostgreSqlType::TEXT,
40+
IntegerEntry::class => PostgreSqlType::INT8,
41+
FloatEntry::class => PostgreSqlType::FLOAT8,
42+
BooleanEntry::class => PostgreSqlType::BOOL,
43+
DateEntry::class => PostgreSqlType::DATE,
44+
DateTimeEntry::class => PostgreSqlType::TIMESTAMPTZ,
45+
TimeEntry::class => PostgreSqlType::TIME,
46+
UuidEntry::class => PostgreSqlType::UUID,
47+
JsonEntry::class => PostgreSqlType::JSONB,
48+
XMLEntry::class => PostgreSqlType::XML,
49+
XMLElementEntry::class => PostgreSqlType::XML,
50+
HTMLEntry::class => PostgreSqlType::TEXT,
51+
HTMLElementEntry::class => PostgreSqlType::TEXT,
52+
EnumEntry::class => PostgreSqlType::TEXT,
53+
ListEntry::class => PostgreSqlType::JSONB,
54+
MapEntry::class => PostgreSqlType::JSONB,
55+
StructureEntry::class => PostgreSqlType::JSONB,
56+
];
57+
58+
/**
59+
* @var array<class-string<Entry<mixed>>, PostgreSqlType>
60+
*/
61+
private array $typeMap;
62+
63+
/**
64+
* @param array<class-string<Entry<mixed>>, PostgreSqlType> $overrides Entry class to PostgreSqlType mappings that override defaults
65+
*/
66+
public function __construct(array $overrides = [])
67+
{
68+
$this->typeMap = \array_merge(self::DEFAULT_TYPES, $overrides);
69+
}
70+
71+
/**
72+
* Maps an Entry to a TypedValue suitable for PostgreSQL queries.
73+
*
74+
* @param Entry<mixed> $entry
75+
*
76+
* @throws TypeMappingException when entry type is not in the map
77+
*/
78+
public function mapEntry(Entry $entry) : ?TypedValue
79+
{
80+
if ($entry->value() === null) {
81+
return null;
82+
}
83+
84+
$entryClass = $entry::class;
85+
86+
if (!\array_key_exists($entryClass, $this->typeMap)) {
87+
throw TypeMappingException::ambiguousEntryType($entryClass);
88+
}
89+
90+
return new TypedValue($entry->value(), $this->typeMap[$entryClass]);
91+
}
92+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\ETL\Adapter\PostgreSql\Exception;
6+
7+
final class TypeMappingException extends RuntimeException
8+
{
9+
public static function ambiguousEntryType(string $entryClass) : self
10+
{
11+
return new self(\sprintf(
12+
'Entry type "%s" is ambiguous and cannot be automatically mapped to a PostgreSQL type. Use withColumnType() to specify the target type explicitly.',
13+
$entryClass
14+
));
15+
}
16+
17+
public static function unsupportedEntryType(string $entryClass) : self
18+
{
19+
return new self(\sprintf(
20+
'Entry type "%s" is not supported for PostgreSQL mapping. Use withColumnType() to specify the target type explicitly or provide a custom EntryTypeMapper.',
21+
$entryClass
22+
));
23+
}
24+
}

0 commit comments

Comments
 (0)