wireservice / agate-sql

agate-sql adds SQL read/write support to agate.
https://agate-sql.readthedocs.io
MIT License
18 stars 15 forks source link

Postgres TIMESTAMP not supported #4

Closed aklaver closed 9 years ago

aklaver commented 9 years ago

agate==1.0.0 agate-sql==0.2.0

Seems database specific types are not supported.

In [1]: import agate

In [2]: import agatesql

In [3]: agatesql.patch()

In [4]: plants = agate.Table.from_sql('postgresql://postgres:@localhost/production', 'plant1')

ValueError Traceback (most recent call last)

in () ----> 1 plants = agate.Table.from_sql('postgresql://postgres:@localhost/production', 'plant1') /home/aklaver/py_virt/pandas/lib/python2.7/site-packages/agatesql/table.py in from_sql(cls, connection_or_string, table_name) 56 column_types.append(agate.DateTime()) 57 else: ---> 58 raise ValueError('Unsupported sqlalchemy column type: %s' % sql_type) 59 60 s = select([sql_table]) ValueError: Unsupported sqlalchemy column type:
onyxfish commented 9 years ago

This is correct, I've currently only added support for the most generic SQL types. I'd love it if somebody wanted to add the other type support!

aklaver commented 9 years ago

I can take a run at this. A little playing around got the below to work. I added TEXT to the char types also. This is a ways from a patch, but it is a proof of concept for comparing apples to apples, i.e. the underlying Python type.

for sql_column in sql_table.columns: column_names.append(sql_column.name) sql_type = type(sql_column.type) py_type = sql_column.type.python_type dt = DATETIME()

        if sql_type in [BIGINT, DECIMAL, FLOAT, INTEGER, NUMERIC, REAL, SMALLINT]:
            column_types.append(agate.Number())
        elif sql_type is BOOLEAN:
            column_types.append(agate.Boolean())
        elif sql_type in [CHAR, NCHAR, VARCHAR, NVARCHAR, TEXT]:
            column_types.append(agate.Text())
        elif sql_type is DATE:
            column_types.append(agate.Date())
        elif py_type is dt.python_type:
            column_types.append(agate.DateTime())
        else:
            raise ValueError('Unsupported sqlalchemy column type: %s' % sql_type)
onyxfish commented 9 years ago

See PR #5.

onyxfish commented 9 years ago

Agate conversions are now based on sqlalchemy's Python type, so theoretically all SQL types are now supported. (Except for time deltas, see #2.)