Converting Arguments and Results¶
The PostgreSQL protocol provides both arguments and results in string formats. However, Python uses
objects of many different types, not just strings. pg-purepy can automatically convert between
Python objects and their PostgreSQL string representation, and vice-versa, provided it knows how to.
A converter is a class that tells the protocol machine how to convert objects back and forth.
Built-in converters¶
pg-purepy comes with several builtin converters, for many Python stdlib types and many
PostgreSQL core types.
“Fundamental” built-in types¶
Date/Time types¶
TIMESTAMP WITH TIMEZONEis mapped toOffsetDateTime.TIMESTAMP WITHOUT TIMEZONEis mapped toPlainDateTime.DATEis mapped todatetime.date.TIME WITHOUT TIMEZONEis mapped todatetime.time.TIME WITH TIMEZONEisn’t supported.
Note
Despite the name, PostgreSQL does not store timestamps using timezones. Instead, timestamps are converted to and from UTC during storage/retrieval based on PostgreSQL’s server-set timezone.
Enumeration types¶
You can add support for your own custom enumeration types using EnumConverter.
- class pg_purepy.conversion.EnumConverter(oid, enum_klass, *, use_member_values=False, lowercase_names=True)¶
-
A converter that lets you use Python enums for PostgreSQL enums.
- __init__(oid, enum_klass, *, use_member_values=False, lowercase_names=True)¶
- Parameters:
oid (int) – The PostgreSQL object ID of the enum type.
enum_klass (type[T]) – The Python-side enumeration class.
use_member_values (bool) – If True and the enum values are all strings, then the member values will be used rather than the names.
lowercase_names (bool) – If
use_member_valuesis False, and this is True, then the member names will be lowercased. This is the general PostgreSQL enum convention.
Array Types¶
All built-in types have an array converter included, that will turn lists or tuples (or other ordered sequences) into PostgreSQL arrays.
If you want to convert your own types to/from arrays, you need to register a separate array converter.
hstore¶
The postgresql key-value type (known as hstore) can be added as a converter.
async with ... as pool:
await pool.add_converter_using(get_hstore_converter)
Custom Converters¶
If you need to convert a type that isn’t supported by default, you can create a custom
Converter.
- class pg_purepy.conversion.abc.Converter¶
Bases:
GenericBase class for all conversion classes. Implement this to create a custom converter.
- abstractmethod from_postgres(context, data)¶
Converts
datafrom the PostgreSQL string representation to a Python type.- Parameters:
context (ConversionContext) – The conversion context this converter was invoked in.
data (str) – The raw string data.
- Return type:
IntoType
- Returns:
Any Python object that resulted from the conversion.
- abstractmethod to_postgres(context, data)¶
Converts
datafrom the Python type to the PostgreSQL string representation.- Parameters:
context (ConversionContext) – The conversion context this converter was invoked in.
data (IntoType) – The Python object that needs to be converted.
- Return type:
str
- Returns:
The string data that will be used in a query string.
The conversion context is passed to conversion functions, and contains attributes that may be useful for your conversion.
- class pg_purepy.conversion.abc.ConversionContext(client_encoding, timezone='UTC')¶
Bases:
objectInformation that may be needed during conversion from PostgreSQL types.
Then, you can register converters with a method depending on your API.
- AsyncPostgresConnection.add_converter(converter)¶
Registers a
Converterwith this connection.- Return type:
The high-level API has its own API for converters. See Converters.