#$Id: sahack4echo.py 1106 2007-03-01 17:53:39Z sdobrev $
# -*- coding: cp1251 -*-
from hacksrc import hacksrc
from sqlalchemy import sql, ansisql

def accept_visitor(self, visitor):
    try: visitor.tablevel += 1
    except: pass
    self._old_accept_visitor( visitor)
    try: visitor.tablevel -= 1
    except: pass

Select = sql.Select
Select._old_accept_visitor = Select.accept_visitor
Select.accept_visitor = accept_visitor

ANSICompiler = ansisql.ANSICompiler
ANSICompiler.tablevel = -1

#tabbing1 = '\\n"+self.tablevel*4*" "+'
tabbing = '\\n"+tabbing'

for func in ANSICompiler.visit_select, ANSICompiler.visit_compound_select:
    hacksrc( func,
        [
        ( '):\n', '''):
        tabbing = self.tablevel*4*" "
''', 1)
    ] + [
        ('\\n'+word, word )     for word in 'SELECT FROM WHERE ORDER GROUP HAVING'.split()    #kill pre-newlines
    ] + [
        (' '+word, word )       for word in 'SELECT FROM WHERE ORDER GROUP HAVING'.split()    #kill pre-spaces
    ] + [
        ('"'+word, '"'+tabbing+'+"'+word )   for word in 'SELECT FROM WHERE ORDER GROUP HAVING'.split()
    ] + [
#        ('"("', '"(\\n"', 5 ),

        ('")"', '"'+tabbing+'[:-4]+")"', 5 ),

        ('cs.keyword', '"'+tabbing+' + cs.keyword + "'+tabbing ),      #visit_compound_select
    ] + 0*[   #newline at end ?
        ('self.for_update_clause(select)', 'self.for_update_clause(select) +"\\n"'), #visit_select
        ('self.visit_select_postclauses(cs)', 'self.visit_select_postclauses(cs) +"\\n"'), #visit_compound_select
    ],

    None, allow_not_found=1,
#    debug=1,
)

#hacksrc( ANSICompiler.limit_clause, [

# vim:ts=4:sw=4:expandtab
