fix(mssql): reverts #9644 and displays a better error msg (#9752)

This commit is contained in:
Daniel Vaz Gaspar
2020-05-14 17:00:02 +01:00
committed by GitHub
parent 4427d65717
commit 3cc540019f
4 changed files with 36 additions and 115 deletions

View File

@@ -20,14 +20,7 @@ from urllib import parse
import sqlparse
from dataclasses import dataclass
from sqlparse.sql import (
Function,
Identifier,
IdentifierList,
remove_quotes,
Token,
TokenList,
)
from sqlparse.sql import Identifier, IdentifierList, remove_quotes, Token, TokenList
from sqlparse.tokens import Keyword, Name, Punctuation, String, Whitespace
from sqlparse.utils import imt
@@ -284,39 +277,3 @@ 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