fix(mssql): apply limit and set alias for functions (#9644)

This commit is contained in:
Daniel Vaz Gaspar
2020-04-27 09:23:08 +01:00
committed by GitHub
parent 5e4c291913
commit 516bdf6db1
3 changed files with 120 additions and 3 deletions

View File

@@ -18,7 +18,14 @@ import logging
from typing import List, Optional, Set
import sqlparse
from sqlparse.sql import Identifier, IdentifierList, remove_quotes, Token, TokenList
from sqlparse.sql import (
Function,
Identifier,
IdentifierList,
remove_quotes,
Token,
TokenList,
)
from sqlparse.tokens import Keyword, Name, Punctuation, String, Whitespace
from sqlparse.utils import imt
@@ -247,3 +254,39 @@ class ParsedQuery:
for i in statement.tokens:
str_res += str(i.value)
return str_res
def set_alias(self) -> str:
"""
Returns a new query string where all functions have alias.
This is particularly necessary for MSSQL engines.
:return: String with new aliased SQL query
"""
new_sql = ""
changed_counter = 1
for token in self._parsed[0].tokens:
# Identifier list (list of columns)
if isinstance(token, IdentifierList) and token.ttype is None:
for i, identifier in enumerate(token.get_identifiers()):
# Functions are anonymous on MSSQL
if isinstance(identifier, Function) and not identifier.has_alias():
identifier.value = (
f"{identifier.value} AS"
f" {identifier.get_real_name()}_{changed_counter}"
)
changed_counter += 1
new_sql += str(identifier.value)
# If not last identifier
if i != len(list(token.get_identifiers())) - 1:
new_sql += ", "
# Just a lonely function?
elif isinstance(token, Function) and token.ttype is None:
if not token.has_alias():
token.value = (
f"{token.value} AS {token.get_real_name()}_{changed_counter}"
)
new_sql += str(token.value)
# Nothing to change, assemble what we have
else:
new_sql += str(token.value)
return new_sql