wmjie / ibm-db

Automatically exported from code.google.com/p/ibm-db
0 stars 0 forks source link

ibm_db_sa - special handling in visit_typeclause prevents casting to varchar #104

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In ibm_db_sa/base.py, what is the purpose of the special handling code in 
visit_typeclause? With it in place, "cast(xxx, String(5))" in Python code 
results in just "xxx", instead of the intended "CAST(xxx AS VARCHAR(5))".

I need casting to varchar to work, otherwise the ibm_db driver will complain 
about:

> ibm_db_dbi::ProgrammingError: [IBM][CLI Driver][DB2/LINUXX8664] SQL0418N  A 
statement contains a use of an untyped parameter marker, the DEFAULT keyword, 
or a null value that is not valid.

More specifically, if I have a CASE statement in the WHERE clause, I found that 
I need to add casting to any one of the result expression, to get rid of the 
aforementioned error, e.g.

SELECT *
FROM food
WHERE
    CASE
        WHEN (end_date IS NULL) THEN CAST('N/A' AS VARCHAR(10))
        WHEN (current_date > end_date) THEN 'Edible'
        ELSE 'Expired'
    END = 'Edible'

Currently, I have to comment out the visit_typeclause method for this to work.

Original issue reported on code.google.com by sok...@gmail.com on 6 Apr 2012 at 3:12

GoogleCodeExporter commented 9 years ago

Original comment by rahul.pr...@in.ibm.com on 12 Apr 2012 at 6:09

GoogleCodeExporter commented 9 years ago
visit_typeclause() is no more override in ibm_db_sa-0.3.0.

Original comment by rahul.pr...@in.ibm.com on 4 Mar 2013 at 5:30

GoogleCodeExporter commented 9 years ago
In ibm_db_sa-0.3.0, the same code was just migrated from visit_typeclause() to 
visit_cast():

    def visit_cast(self, cast, **kw):
        type_ = cast.typeclause.type

        # TODO: verify that CAST shouldn't be called with
        # other types, I was able to CAST against VARCHAR
        # for example
        if isinstance(type_, (
                    sa_types.DateTime, sa_types.Date, sa_types.Time,
                    sa_types.DECIMAL)):
            return super(DB2Compiler, self).visit_cast(cast, **kw)
        else:
            return self.process(cast.clause)

So, casting to varchar still doesn't work. I will send a pull request to the 
github repo instead.

Original comment by sok...@gmail.com on 7 Jul 2013 at 7:43