Skip to content

Commit

Permalink
visit_select needs more testing with limit clause #20
Browse files Browse the repository at this point in the history
  • Loading branch information
sandan committed Sep 30, 2016
1 parent fb6c9a8 commit 2ff952b
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions sqlalchemy_teradata/compiler.py
Expand Up @@ -9,52 +9,12 @@
from sqlalchemy import exc
from sqlalchemy import schema as sa_schema
from sqlalchemy.types import Unicode



from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import Select
from sqlalchemy import exc, sql
from sqlalchemy import create_engine


@compiles(Select, 'teradata')
def compile_select(element, compiler, **kw):
"""
"""

if not getattr(element, '_window_visit', None):
if element._limit is not None or element._offset is not None:
limit, offset = element._limit, element._offset

orderby=compiler.process(element._order_by_clause)
if orderby:
element = element._generate()
element._window_visit=True
#element._limit = None
#element._offset = None cant set to none...

# add a ROW NUMBER() OVER(ORDER BY) column
element = element.column(sql.literal_column('ROW NUMBER() OVER (ORDER BY %s)' % orderby).label('rownum')).order_by(None)

# wrap into a subquery
limitselect = sql.select([c for c in element.alias().c if c.key != 'rownum'])

limitselect._window_visit=True
limitselect._is_wrapper=True

if offset is not None:
limitselect.append_whereclause(sql.column('rownum') > offset)
if limit is not None:
limitselect.append_whereclause(sql.column('rownum') <= (limit + offset))
else:
limitselect.append_whereclause(sql.column("rownum") <= limit)

element = limitselect

kw['iswrapper'] = getattr(element, '_is_wrapper', False)
return compiler.visit_select(element, **kw)

class TeradataCompiler(compiler.SQLCompiler):

def __init__(self, dialect, statement, column_keys=None, inline=False, **kwargs):
Expand All @@ -73,9 +33,8 @@ def get_select_precolumns(self, select, **kwargs):
pre = select._distinct and "DISTINCT " or ""

#TODO: decide whether we can replace this with the recipe...
if (select._limit and select._offset is None):
pre+= "TOP %d " % (select._limit)

if (select._limit is not None and select._offset is None):
pre += "TOP %d " % (select._limit)

return pre

Expand Down Expand Up @@ -648,3 +607,42 @@ def visit_BOOLEAN(self, type_, **kw):

def visit_BYTEINT(self, type_, **kw):
return 'BYTEINT'



#@compiles(Select, 'teradata')
#def compile_select(element, compiler, **kw):
# """
# """
#
# if not getattr(element, '_window_visit', None):
# if element._limit is not None or element._offset is not None:
# limit, offset = element._limit, element._offset
#
# orderby=compiler.process(element._order_by_clause)
# if orderby:
# element = element._generate()
# element._window_visit=True
# #element._limit = None
# #element._offset = None cant set to none...
#
# # add a ROW NUMBER() OVER(ORDER BY) column
# element = element.column(sql.literal_column('ROW NUMBER() OVER (ORDER BY %s)' % orderby).label('rownum')).order_by(None)
#
# # wrap into a subquery
# limitselect = sql.select([c for c in element.alias().c if c.key != 'rownum'])
#
# limitselect._window_visit=True
# limitselect._is_wrapper=True
#
# if offset is not None:
# limitselect.append_whereclause(sql.column('rownum') > offset)
# if limit is not None:
# limitselect.append_whereclause(sql.column('rownum') <= (limit + offset))
# else:
# limitselect.append_whereclause(sql.column("rownum") <= limit)
#
# element = limitselect
#
# kw['iswrapper'] = getattr(element, '_is_wrapper', False)
# return compiler.visit_select(element, **kw)

0 comments on commit 2ff952b

Please sign in to comment.