OracleDB.select: add support for orderby clause specify by tuple

This commit is contained in:
Benjamin Renard 2021-11-18 12:43:02 +01:00
parent 48c1fb085e
commit 0eda55f11c

View file

@ -49,6 +49,19 @@ class OracleDBUnsupportedWHEREClauses(OracleDBException, TypeError):
) )
class OracleDBInvalidOrderByClause(OracleDBException, TypeError):
"""
Raised when trying to select on table with invalid
ORDER BY clause provided
"""
def __init__(self, order_by):
super().__init__(
"Invalid ORDER BY clause: {order_by}. Must be a string or a list of two values (ordering field name and direction)",
order_by=order_by
)
class OracleDB: class OracleDB:
""" Oracle client """ """ Oracle client """
@ -352,7 +365,17 @@ class OracleDB:
return False return False
if order_by: if order_by:
sql += ' ORDER BY {0}'.format(order_by) if isinstance(order_by, str):
sql += ' ORDER BY {0}'.format(order_by)
elif (
isinstance(order_by, (list, tuple)) and len(order_by) == 2 and
isinstance(order_by[0], str) and
isinstance(order_by[1], str) and
order_by[1].upper() in ('ASC', 'UPPER')
):
sql += ' ORDER BY "{0}" {1}'.format(order_by[0], order_by[1].upper())
else:
raise OracleDBInvalidOrderByClause(order_by)
if just_try: if just_try:
log.debug("Just-try mode: execute SELECT query : %s", sql) log.debug("Just-try mode: execute SELECT query : %s", sql)