On Apr 7, 2011, at 1:25 AM, Philipp Rautenberg wrote:
> The following code is DB specific:
>
> import sqlalchemy
> # ...
> ergebnis = session.query(
> my_object.attr1).filter(sa.and_(
> my_object.attr2 != 'NaN')).all() # PostgreSQL
> # my_object.attr2 != None)).all() # sQLite
>
> With PostgreSQL it is "'NaN'", with SQLite "None" (without single
> quotes). Is there a SQLAlchemy-way to do this backend independant?
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import ColumnElement
from sqlalchemy.types import NULLTYPE
class Nan(ColumnElement):
type = NULLTYPE
@compiles(Nan, "postgresql")
def pg_nan(elem, compiler, **kw):
return "NaN"
@compiles(Nan, "sqlite")
def sl_nan(elem, compiler, **kw):
return "None"
from sqlalchemy import select
from sqlalchemy.dialects import postgresql, sqlite
print select(["one", "two", "three"]).where("one" !=
Nan()).compile(dialect=postgresql.dialect())
print select(["one", "two", "three"]).where("one" !=
Nan()).compile(dialect=sqlite.dialect())
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.