Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process_result_value callback for column type is not handled #106

Open
alvassin opened this issue Mar 18, 2020 · 1 comment
Open

process_result_value callback for column type is not handled #106

alvassin opened this issue Mar 18, 2020 · 1 comment

Comments

@alvassin
Copy link
Contributor

I need TypeDecorator for my data column, it is handled by SQLAlchemy + psycopg2 correctly. Is it possible to make it work with asyncpgsa?

import asyncio
from datetime import datetime

from asyncpgsa import PG
from pytz import timezone
from sqlalchemy import (
    Column, DateTime, Integer, MetaData, Table, TypeDecorator, create_engine
)


DB_URL = 'postgresql://user:hackme@0.0.0.0/db'


class DateTime_(TypeDecorator):
    impl = DateTime

    def __init__(self):
        TypeDecorator.__init__(self, timezone=True)

    def process_bind_param(self, value, dialect):
        if value is not None:
            return datetime.fromtimestamp(value, timezone('UTC'))

    def process_result_value(self, value, dialect):
        return int(value.timestamp())


metadata = MetaData()
example_table = Table('example', metadata,
                      Column('id', Integer, primary_key=True),
                      Column('some_date', DateTime_))

engine = create_engine(DB_URL)

# Create table & add row
metadata.create_all(engine)
engine.execute(example_table.insert().values({
    'some_date': int(datetime.now().timestamp())
}))

# psycopg2 with sqlalchemy handles process_result_value correctly
rows = engine.execute(example_table.select()).fetchall()
assert isinstance(rows[0]['some_date'], int)


# asyncpgsa does not handle process_result_value callback
async def main():
    db = PG()
    await db.init(DB_URL)
    rows = await db.fetch(example_table.select())
    assert isinstance(rows[0]['some_date'], datetime)  # True
    assert isinstance(rows[0]['some_date'], int)  # False!


asyncio.run(main())

Perhaps such callbacks should be called in SAConnection.execute?

@nhumrich
Copy link
Contributor

This sounds awesome. It sounds doable. I dont have much time to work in this currently. Would you be willing to start a pull request with the changes? Would be happy to guide you in the right direction if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants