Skip to content

TypeConverterFactory

Alexey Borzov edited this page Feb 17, 2025 · 5 revisions

TypeConverterFactory interface

Warning

These docs are outdated and are no longer maintained. The manual is now on pg-wrapper.readthedocs.io

Classes that create type converters implement this interface.

It defines the following methods:

  • getConverterForTypeSpecification(mixed $type): TypeConverter - Returns a converter specified by a given type
  • getConverterForTypeOID(int $oid): TypeConverter - Returns a converter for the type with the given OID
  • getConverterForPHPValue(mixed $value): TypeConverter - Tries to return a converter based on type of $value
  • setConnection(Connection $connection): $this - sets database connection info, to be able to load metadata for a particular database

getConverterForTypeSpecification() is used to get a converter for a user-supplied type (e.g. parameter types for executeParams()), type specification is implementation dependent, arguments accepted by DefaultTypeConverterFactory are described on its page.

getConverterForTypeOID() is used to get converters for query result fields by ResultSet. It passes an OID (PostgreSQL's internal object identifier) for a type as returned by pg_field_type_oid function. Note that we don't use textual type description returned by pg_field_type for the following reasons:

  • Schema information is missing;
  • Internally pg_field_type just loads the whole pg_catalog.pg_type data into memory on the first call and later searches it for type oid. We do the same in DefaultTypeConverterFactory, but load more info and allow caching of the resultant array.

Implementations

The package contains two implementations of TypeConverterFactory: converters\DefaultTypeConverterFactory and converters\StubTypeConverterFactory. getConverterForTypeSpecification() method of the latter will return

  • $type, if it is an instance of TypeConverter. It will be configured with current Connection if it implements ConnectionAware.
  • An instance of converters\StubConverter if $type is anything else.

Its getConverterForTypeOID() and getConverterForPHPValue() also return StubConverter. This can be used to essentially disable type conversion, making package behave like stock pgsql extension.

Clone this wiki locally