simplify ext/compiler.py

Issue #1331 resolved
Mike Bayer repo owner created an issue

you can make a custom compiled object just like this:

class MyThing(ClauseElement):
   pass

def _compiler_dispatch(self, compiler):
    return "mything"
MyThing._compiler_dispatch = _compiler_dispatch

the UserDefinedCompiler should simplify and not create a new UDC object upon dispatch - there's too much overhead in lookup and such. Let's have UDC just receive the parent compiler as an argument and not be stateful - statefulness will be a very rare use case and they can use the parent compiler for state if need be. let's also think about the above as a super quick "i just need a clause element that does XYZ" - just subclass ClauseElement and add a dispatch method - very convenient.

Comments (3)

  1. Mike Bayer reporter
    from sqlalchemy.sql.compiler import compiles
    
    class MyThing(ClauseElement):
       pass
    
    @compiles(MyThing)
    def compile_thing(compiler, element):
       return "mything"
    
    @compiles(MyThing).on('postgres')
    def compile_thing(compiler, element):
       return "pg_mything"
    
  2. Log in to comment