Thanks for the reply!

On Wed, Oct 6, 2010 at 9:06 AM, Michael Bayer <mike...@zzzcomputing.com> wrote:
> SQlite has no lossless DECIMAL type and SQLAlchemy can't work around that.

Well SQLAlchemy itself suggests workarounds that it doesn't implement,
so here we are...

> The approaches to working around this are:
> 1. store the decimals as strings.  Use String, and place a TypeDecorator
> around it which marshals Decimal objects.

Is it as simple as this?
"""
class DecimalString(sqlalchemy.types.TypeDecorator):
    impl = sqlalchemy.types.String

    def process_bind_param(self, value, dialect):
        return str(value)

    def process_result_value(self, value, dialect):
        return decimal.Decimal(value)
"""

> 2. store the decimals as integers, using a type with a fixed exponent.   Use
> Integer, and place a TypeDecorator around it which multiplies Decimal
> objects upwards by the fixed exponent going in and back down going out.

Is it as simple as this?
"""
class DecimalInt(sqlalchemy.types.TypeDecorator):
    impl = sqlalchemy.types.Integer

    def process_bind_param(self, value, dialect):
        return int(value * 10**4) # basis pt precision

    def process_result_value(self, value, dialect):
        return decimal.Decimal(value / 10**4) # basis pt precision
"""

> 3. stick with the FP program.

Who actually desires this behavior?  I'm not trying to flame here, I
am genuinely curious why that is the preferred default for SQLite.

One more question, if I may.  I suppose what would be ideal would be
to create a type that wrapped a String or Integer if the dialect was
sqlite, but wrapped a Numeric type if the dialect was something a
little more full-fledged.  Possible/difficult/ill-advised to do within
the SQLAlchemy type system?

Well, despite raising the ominous specter of accounting identity
violations, so far I'm really liking the looks of SQLAlchemy.  Thanks
for the help.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to