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

Add Interval<-->TimeDelta support #10

Closed aklaver closed 8 years ago

aklaver commented 9 years ago

Well this was fun. Interval(INTERVAL) is a many headed beast in SQLAlchemy. The patch adds code to deal with the fact that what interval type is used is dependent on the database dialect. For those cases(postgresql, oracle) where there is native support use the native version and fake a python_type(datetime.timedelta) as that is not set in the native types. For the generic type it is set an so just use it, as it is datetime.timedelta also. This deals with the from_sql case. For the to_sql case set the interval type using the dialect information from connection. To make all this happen create INTERVAL_MAP as lookup for dialect and interval type.

onyxfish commented 9 years ago

Hey that's crazy, but it looks good to me. Can you tell me more about this addition:

+                if py_type is float:
+                    sql_column.type.asdecimal = True
aklaver commented 9 years ago

That was in the previous pull request also, though I see it did not make it in. This is to deal with the fact that agate.Number() does not accept floats. In the SQLAlchemy world both the SQLA types REAL and FLOAT have a python_type of float. Further they both have an attribute asdecimal(FYI, the SQLA DECIMAL has asdecimal also, it is set True by default ). By default that is set False. If you set is=True, which is what the above does then you convert FLOAT and REAL to decimal.Decimal with a default to ten decimal places. So what the above does is automatically convert float values to decimal.Decimal.

onyxfish commented 9 years ago

Ah, gotcha. As it happens agate 1.1.0 will support floats natively (be internalizing the type conversion that the user had to do themselves before). However, where there is a solution that's closer to the data source, I definitely prefer it.

I'll merge this in when I get a free moment. Thanks!

onyxfish commented 8 years ago

Got this merged in. Am now putting in updates for agate 1.1.0 then I'll push a release. Thanks again, Adrian!