def test_get_query_tokens(): assert get_query_tokens("") == [] tokens = get_query_tokens("SELECT * FROM foo") assert len(tokens) == 4 assert tokens[0].ttype is DML assert str(tokens[0]) == 'SELECT' assert tokens[2].ttype is Keyword assert str(tokens[2]) == 'FROM'
def is_wildcard_query(query): """ Checks if provided query selects using a * wildcard :type query str :rtype bool """ if not is_select_query(query): return False query = preprocess_query(query) tokens = get_query_tokens(query) last_token = None for token in tokens: if token.ttype is Wildcard: # print([query, token, 'last token', last_token]) # check what was before the wildcard # count(*) ? if last_token and str(last_token) not in ['(']: return True else: last_token = token return False
def query_has_having_clause(query): """ Checks if provided query uses HAVING clause :type query str :rtype bool """ if not is_select_query(query): return False query = preprocess_query(query) tokens = get_query_tokens(query) for token in tokens: if token.ttype is Keyword and str(token).upper() == 'HAVING': return True return False
def raw_ast(self): sql_metadata.get_query_tokens(self.sql)