Thanks, Michael

i end with the following:

#############
from sqlalchemy.sql.expression import _Function

class year( _Function):
    __visit_name__ = 'year'
    def __init__(self, value):
        self.value = value
        _Function.__init__( self, self.__visit_name__)

def _compiler_dispatch(self, compiler, **kargs):
    val = compiler.preparer.format_column( self.value, use_table=
True)
    if compiler.dialect.name == 'postgres':
        return "extract( %s from %s)" % ( self.name, val)
    elif compiler.dialect.name == 'sqlite':
        format_char = 'Y'
        pfx = 'strftime( "%' + format_char + '",'
        return pfx + ('%s)' % val)
    else:
        return '%s( %s)' % ( self.name, val)

year._compiler_dispatch = _compiler_dispatch
############

cheers,
stefan


On 18 Март, 19:26, "Michael Bayer" <[email protected]> wrote:
> we'll be adding a feature for this soon.  here is a non-public way to do
> it for now which will work throughout 0.5:
>
> from sqlalchemy.sql.expression import ClauseElement
>
> class year(ClauseElement):
>     __visit_name__ = 'year'
>     def __init__(self, value):
>         self.value = value
>
> def _compiler_dispatch(self, compiler):
>     if compiler.dialect.name == 'postgres':
>         return "pg_year(%s)" % self.value
>     else:
>         return "sqlite_year(%s)" % self.value
> year._compiler_dispatch = _compiler_dispatch
>
> che wrote:
>
> > Hi,
>
> > i need to add several functions to all dialects that i'll plan to use
> > in order to be fully database-independent.
> > Does anybody have tips how to achieve this?
> > For example i want to add function year( date) to sqlite and postgres
> > dialects.
>
> > TIA,
> > Stefan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to