Nihey Takizawa
Nihey Takizawa

Reputation: 817

How do I query UUID values without having to explicitly cast them to string in HTSQL

I have a table that is indexed by a UUID field, using postgres' uuid-ossp extension, everything running on a python application. Every time I filter the UUID field I have to explicitly cast the uuid field:

htsql = HTSQL('pgsql:///my_database')
htsql.produce("/product.filter(string(id) = '00002094-b1c3-11e3-92b8-6bb3666a756f')")

If I do not cast the id I have the following error:

htsql.produce("/product.filter(id = '00002094-b1c3-11e3-92b8-6bb3666a756f')")

Cannot coerce values of types (opaque, untyped) to a common type
While translating:
    /product.filter(id = '00002094-b1c3-11e3-92b8-6bb3666a756f')

It seems HTSQL do not recognize UUID type. So my question is:

Is there any way to tell HTSQL how to recognize a UUID? Can I do this on the command line interface as well?

Upvotes: 1

Views: 767

Answers (1)

Nihey Takizawa
Nihey Takizawa

Reputation: 817

By looking at HTSQL's source code, my current solution to this very problem is using its instrospection classes to tell HTSQL to recognize UUIDs as Text:

class IntrospectPGSQLUUIDDomain(IntrospectPGSQLDomain):                    
    call(('pg_catalog', 'uuid'))                                           

    @classmethod                                                           
    def __enabled__(cls):                                                  
        return True                                                        

    def __call__(self):                                                    
        return TextDomain()

And then telling HTSQL to instrospect it after creating the database connection:

htsql = HTSQL('pgsql:///my_database')                      
with htsql:                                                         
    introspect() 

This solves my problem on the python application, but I still have to figure out a way to do this on the command line.

Upvotes: 1

Related Questions