Python sqlparse.sql() Examples
The following are 30
code examples of sqlparse.sql().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlparse
, or try the search function
.
Example #1
Source File: utils.py From pgcli with BSD 3-Clause "New" or "Revised" License | 8 votes |
def parse_partial_identifier(word): """Attempt to parse a (partially typed) word as an identifier word may include a schema qualification, like `schema_name.partial_name` or `schema_name.` There may also be unclosed quotation marks, like `"schema`, or `schema."partial_name` :param word: string representing a (partially complete) identifier :return: sqlparse.sql.Identifier, or None """ p = sqlparse.parse(word)[0] n_tok = len(p.tokens) if n_tok == 1 and isinstance(p.tokens[0], Identifier): return p.tokens[0] elif p.token_next_by(m=(Error, '"'))[1]: # An unmatched double quote, e.g. '"foo', 'foo."', or 'foo."bar' # Close the double quote, then reparse return parse_partial_identifier(word + '"') else: return None
Example #2
Source File: test_parse.py From codenn with MIT License | 7 votes |
def test_placeholder(self): def _get_tokens(sql): return sqlparse.parse(sql)[0].tokens[-1].tokens t = _get_tokens('select * from foo where user = ?') self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder) self.assertEqual(t[-1].value, '?') t = _get_tokens('select * from foo where user = :1') self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder) self.assertEqual(t[-1].value, ':1') t = _get_tokens('select * from foo where user = :name') self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder) self.assertEqual(t[-1].value, ':name') t = _get_tokens('select * from foo where user = %s') self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder) self.assertEqual(t[-1].value, '%s') t = _get_tokens('select * from foo where user = $a') self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder) self.assertEqual(t[-1].value, '$a')
Example #3
Source File: parseutils.py From litecli with BSD 3-Clause "New" or "Revised" License | 7 votes |
def extract_tables(sql): """Extract the table names from an SQL statment. Returns a list of (schema, table, alias) tuples """ parsed = sqlparse.parse(sql) if not parsed: return [] # INSERT statements must stop looking for tables at the sign of first # Punctuation. eg: INSERT INTO abc (col1, col2) VALUES (1, 2) # abc is the table name, but if we don't stop at the first lparen, then # we'll identify abc, col1 and col2 as table names. insert_stmt = parsed[0].token_first().value.lower() == "insert" stream = extract_from_part(parsed[0], stop_at_punctuation=insert_stmt) return list(extract_table_identifiers(stream))
Example #4
Source File: extract_tables.py From Archery with Apache License 2.0 | 6 votes |
def extract_tables(sql): """Extract the table names from an SQL statment. Returns a list of TableReference namedtuples """ parsed = sqlparse.parse(sql) if not parsed: return () # INSERT statements must stop looking for tables at the sign of first # Punctuation. eg: INSERT INTO abc (col1, col2) VALUES (1, 2) # abc is the table name, but if we don't stop at the first lparen, then # we'll identify abc, col1 and col2 as table names. insert_stmt = parsed[0].token_first().value.lower() == "insert" stream = extract_from_part(parsed[0], stop_at_punctuation=insert_stmt) # Kludge: sqlparse mistakenly identifies insert statements as # function calls due to the parenthesized column list, e.g. interprets # "insert into foo (bar, baz)" as a function call to foo with arguments # (bar, baz). So don't allow any identifiers in insert statements # to have is_function=True identifiers = extract_table_identifiers(stream, allow_functions=not insert_stmt) # In the case 'sche.<cursor>', we get an empty TableReference; remove that return tuple(i for i in identifiers if i.name)
Example #5
Source File: utils.py From mssql-cli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def parse_partial_identifier(word): """Attempt to parse a (partially typed) word as an identifier word may include a schema qualification, like `schema_name.partial_name` or `schema_name.` There may also be unclosed quotation marks, like `"schema`, or `schema."partial_name` :param word: string representing a (partially complete) identifier :return: sqlparse.sql.Identifier, or None """ p = sqlparse.parse(word)[0] n_tok = len(p.tokens) if n_tok == 1 and isinstance(p.tokens[0], Identifier): return p.tokens[0] if p.token_next_by(m=(Error, '"'))[1]: # An unmatched double quote, e.g. '"foo', 'foo."', or 'foo."bar' # Close the double quote, then reparse return parse_partial_identifier(word + '"') return None
Example #6
Source File: sqlcompletion.py From mssql-cli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _allow_join_condition(statement): """ Tests if a join condition should be suggested We need this to avoid bad suggestions when entering e.g. select * from tbl1 a join tbl2 b on a.id = <cursor> So check that the preceding token is a ON, AND, or OR keyword, instead of e.g. an equals sign. :param statement: an sqlparse.sql.Statement :return: boolean """ if not statement or not statement.tokens: return False last_tok = statement.token_prev(len(statement.tokens))[1] return last_tok.value.lower() in ('on', 'and', 'or')
Example #7
Source File: sql_parse.py From incubator-superset with Apache License 2.0 | 6 votes |
def as_create_table( self, table_name: str, schema_name: Optional[str] = None, overwrite: bool = False, method: CtasMethod = CtasMethod.TABLE, ) -> str: """Reformats the query into the create table as query. Works only for the single select SQL statements, in all other cases the sql query is not modified. :param table_name: table that will contain the results of the query execution :param schema_name: schema name for the target table :param overwrite: table_name will be dropped if true :param method: method for the CTA query, currently view or table creation :return: Create table as query """ exec_sql = "" sql = self.stripped() # TODO(bkyryliuk): quote full_table_name full_table_name = f"{schema_name}.{table_name}" if schema_name else table_name if overwrite: exec_sql = f"DROP {method} IF EXISTS {full_table_name};\n" exec_sql += f"CREATE {method} {full_table_name} AS \n{sql}" return exec_sql
Example #8
Source File: sql_metadata.py From sql-metadata with MIT License | 6 votes |
def normalize_likes(sql: str) -> str: """ Normalize and wrap LIKE statements :type sql str :rtype: str """ sql = sql.replace('%', '') # LIKE '%bot' sql = re.sub(r"LIKE '[^\']+'", 'LIKE X', sql) # or all_groups LIKE X or all_groups LIKE X matches = re.finditer(r'(or|and) [^\s]+ LIKE X', sql, flags=re.IGNORECASE) matches = [match.group(0) for match in matches] if matches else None if matches: for match in set(matches): sql = re.sub(r'(\s?' + re.escape(match) + ')+', ' ' + match + ' ...', sql) return sql
Example #9
Source File: sqlcompletion.py From pgcli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _allow_join_condition(statement): """ Tests if a join condition should be suggested We need this to avoid bad suggestions when entering e.g. select * from tbl1 a join tbl2 b on a.id = <cursor> So check that the preceding token is a ON, AND, or OR keyword, instead of e.g. an equals sign. :param statement: an sqlparse.sql.Statement :return: boolean """ if not statement or not statement.tokens: return False last_tok = statement.token_prev(len(statement.tokens))[1] return last_tok.value.lower() in ("on", "and", "or")
Example #10
Source File: tables.py From pgcli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def extract_tables(sql): """Extract the table names from an SQL statment. Returns a list of TableReference namedtuples """ parsed = sqlparse.parse(sql) if not parsed: return () # INSERT statements must stop looking for tables at the sign of first # Punctuation. eg: INSERT INTO abc (col1, col2) VALUES (1, 2) # abc is the table name, but if we don't stop at the first lparen, then # we'll identify abc, col1 and col2 as table names. insert_stmt = parsed[0].token_first().value.lower() == "insert" stream = extract_from_part(parsed[0], stop_at_punctuation=insert_stmt) # Kludge: sqlparse mistakenly identifies insert statements as # function calls due to the parenthesized column list, e.g. interprets # "insert into foo (bar, baz)" as a function call to foo with arguments # (bar, baz). So don't allow any identifiers in insert statements # to have is_function=True identifiers = extract_table_identifiers(stream, allow_functions=not insert_stmt) # In the case 'sche.<cursor>', we get an empty TableReference; remove that return tuple(i for i in identifiers if i.name)
Example #11
Source File: test_parse.py From codenn with MIT License | 5 votes |
def test_sqlite_identifiers(): # Make sure we still parse sqlite style escapes p = sqlparse.parse('[col1],[col2]')[0].tokens assert (len(p) == 1 and isinstance(p[0], sqlparse.sql.IdentifierList) and [id.get_name() for id in p[0].get_identifiers()] == ['[col1]', '[col2]']) p = sqlparse.parse('[col1]+[col2]')[0] types = [tok.ttype for tok in p.flatten()] assert types == [T.Name, T.Operator, T.Name]
Example #12
Source File: test_parse.py From codenn with MIT License | 5 votes |
def test_single_line_comments(sql): p = sqlparse.parse(sql)[0] assert len(p.tokens) == 5 assert p.tokens[-1].ttype == T.Comment.Single
Example #13
Source File: test_parse.py From codenn with MIT License | 5 votes |
def test_quoted_identifier(): t = sqlparse.parse('select x.y as "z" from foo')[0].tokens assert isinstance(t[2], sqlparse.sql.Identifier) assert t[2].get_name() == 'z' assert t[2].get_real_name() == 'y'
Example #14
Source File: parseutils.py From athenacli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def extract_tables(sql): """Extract the table names from an SQL statment. Returns a list of (schema, table, alias) tuples """ parsed = sqlparse.parse(sql) if not parsed: return [] # INSERT statements must stop looking for tables at the sign of first # Punctuation. eg: INSERT INTO abc (col1, col2) VALUES (1, 2) # abc is the table name, but if we don't stop at the first lparen, then # we'll identify abc, col1 and col2 as table names. insert_stmt = parsed[0].token_first().value.lower() == 'insert' stream = extract_from_part(parsed[0], stop_at_punctuation=insert_stmt) return list(extract_table_identifiers(stream))
Example #15
Source File: parseutils.py From athenacli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def find_prev_keyword(sql): """ Find the last sql keyword in an SQL statement Returns the value of the last keyword, and the text of the query with everything after the last keyword stripped """ if not sql.strip(): return None, '' parsed = sqlparse.parse(sql)[0] flattened = list(parsed.flatten()) logical_operators = ('AND', 'OR', 'NOT', 'BETWEEN') for t in reversed(flattened): if t.value == '(' or (t.is_keyword and ( t.value.upper() not in logical_operators)): # Find the location of token t in the original parsed statement # We can't use parsed.token_index(t) because t may be a child token # inside a TokenList, in which case token_index thows an error # Minimal example: # p = sqlparse.parse('select * from foo where bar') # t = list(p.flatten())[-3] # The "Where" token # p.token_index(t) # Throws ValueError: not in list idx = flattened.index(t) # Combine the string values of all tokens in the original list # up to and including the target keyword token t, to produce a # query string with everything after the keyword token removed text = ''.join(tok.value for tok in flattened[:idx+1]) return t, text return None, ''
Example #16
Source File: search_utils.py From mlflow with Apache License 2.0 | 5 votes |
def _does_run_match_clause(cls, run, sed): key_type = sed.get('type') key = sed.get('key') value = sed.get('value') comparator = sed.get('comparator').upper() if cls.is_metric(key_type, comparator): lhs = run.data.metrics.get(key, None) value = float(value) elif cls.is_param(key_type, comparator): lhs = run.data.params.get(key, None) elif cls.is_tag(key_type, comparator): lhs = run.data.tags.get(key, None) elif cls.is_attribute(key_type, comparator): lhs = getattr(run.info, key) else: raise MlflowException("Invalid search expression type '%s'" % key_type, error_code=INVALID_PARAMETER_VALUE) if lhs is None: return False if comparator in cls.CASE_INSENSITIVE_STRING_COMPARISON_OPERATORS: # Change value from sql syntax to regex syntax if comparator == 'ILIKE': value = value.lower() lhs = lhs.lower() if not value.startswith('%'): value = '^' + value if not value.endswith('%'): value = value + '$' value = value.replace('_', '.').replace('%', '.*') return cls.filter_ops.get(comparator)(value, lhs) elif comparator in cls.filter_ops.keys(): return cls.filter_ops.get(comparator)(lhs, value) else: return False
Example #17
Source File: utils.py From mssql-cli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def find_prev_keyword(sql, n_skip=0): """ Find the last sql keyword in an SQL statement Returns the value of the last keyword, and the text of the query with everything after the last keyword stripped """ if not sql.strip(): return None, '' parsed = sqlparse.parse(sql)[0] flattened = list(parsed.flatten()) flattened = flattened[:len(flattened) - n_skip] logical_operators = ('AND', 'OR', 'NOT', 'BETWEEN') for t in reversed(flattened): if t.value == '(' or (t.is_keyword and (t.value.upper() not in logical_operators) ): # Find the location of token t in the original parsed statement # We can't use parsed.token_index(t) because t may be a child token # inside a TokenList, in which case token_index thows an error # Minimal example: # p = sqlparse.parse('select * from foo where bar') # t = list(p.flatten())[-3] # The "Where" token # p.token_index(t) # Throws ValueError: not in list idx = flattened.index(t) # Combine the string values of all tokens in the original list # up to and including the target keyword token t, to produce a # query string with everything after the keyword token removed text = ''.join(tok.value for tok in flattened[:idx + 1]) return t, text return None, '' # Postgresql dollar quote signs look like `$$` or `$tag$`
Example #18
Source File: test_parse.py From codenn with MIT License | 5 votes |
def test_double_quotes_are_identifiers(): p = sqlparse.parse('"foo"')[0].tokens assert len(p) == 1 assert isinstance(p[0], sqlparse.sql.Identifier)
Example #19
Source File: utils.py From mssql-cli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def is_open_quote(sql): """Returns true if the query contains an unclosed quote""" # parsed can contain one or more semi-colon separated commands parsed = sqlparse.parse(sql) return any(_parsed_is_open_quote(p) for p in parsed)
Example #20
Source File: tables.py From mssql-cli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def extract_tables(sql): """Extract the table names from an SQL statment. Returns a list of TableReference namedtuples """ parsed = sqlparse.parse(sql) if not parsed: return () # INSERT statements must stop looking for tables at the sign of first # Punctuation. eg: INSERT INTO abc (col1, col2) VALUES (1, 2) # abc is the table name, but if we don't stop at the first lparen, then # we'll identify abc, col1 and col2 as table names. insert_stmt = parsed[0].token_first().value.lower() == 'insert' stream = extract_from_part(parsed[0], stop_at_punctuation=insert_stmt) # Kludge: sqlparse mistakenly identifies insert statements as # function calls due to the parenthesized column list, e.g. interprets # "insert into foo (bar, baz)" as a function call to foo with arguments # (bar, baz). So don't allow any identifiers in insert statements # to have is_function=True identifiers = extract_table_identifiers(stream, allow_functions=not insert_stmt) # In the case 'sche.<cursor>', we get an empty TableReference; remove that return tuple(i for i in identifiers if i.name)
Example #21
Source File: sql_parse.py From incubator-superset with Apache License 2.0 | 5 votes |
def __init__(self, sql_statement: str): self.sql: str = sql_statement self._tables: Set[Table] = set() self._alias_names: Set[str] = set() self._limit: Optional[int] = None logger.debug("Parsing with sqlparse statement: %s", self.sql) self._parsed = sqlparse.parse(self.stripped()) for statement in self._parsed: self._limit = _extract_limit_from_query(statement)
Example #22
Source File: sql_parse.py From incubator-superset with Apache License 2.0 | 5 votes |
def stripped(self) -> str: return self.sql.strip(" \t\n;")
Example #23
Source File: sql_parse.py From incubator-superset with Apache License 2.0 | 5 votes |
def get_statements(self) -> List[str]: """Returns a list of SQL statements as strings, stripped""" statements = [] for statement in self._parsed: if statement: sql = str(statement).strip(" \n;\t") if sql: statements.append(sql) return statements
Example #24
Source File: sql_metadata.py From sql-metadata with MIT License | 5 votes |
def get_query_tokens(query: str) -> List[sqlparse.sql.Token]: """ :type query str :rtype: list[sqlparse.sql.Token] """ query = preprocess_query(query) parsed = sqlparse.parse(query) # handle empty queries (#12) if not parsed: return [] tokens = TokenList(parsed[0].tokens).flatten() # print([(token.value, token.ttype) for token in tokens]) return [token for token in tokens if token.ttype is not Whitespace]
Example #25
Source File: sql_metadata.py From sql-metadata with MIT License | 5 votes |
def remove_comments_from_sql(sql: str) -> str: """ Removes comments from SQL query :type sql str|None :rtype: str """ return re.sub(r'\s?/\*.+\*/', '', sql)
Example #26
Source File: sql_metadata.py From sql-metadata with MIT License | 5 votes |
def generalize_sql(sql: Optional[str]) -> Optional[str]: """ Removes most variables from an SQL query and replaces them with X or N for numbers. Based on Mediawiki's DatabaseBase::generalizeSQL :type sql str|None :rtype: str """ if sql is None: return None # multiple spaces sql = re.sub(r'\s{2,}', ' ', sql) # MW comments # e.g. /* CategoryDataService::getMostVisited N.N.N.N */ sql = remove_comments_from_sql(sql) # handle LIKE statements sql = normalize_likes(sql) sql = re.sub(r"\\\\", '', sql) sql = re.sub(r"\\'", '', sql) sql = re.sub(r'\\"', '', sql) sql = re.sub(r"'[^\']*'", 'X', sql) sql = re.sub(r'"[^\"]*"', 'X', sql) # All newlines, tabs, etc replaced by single space sql = re.sub(r'\s+', ' ', sql) # All numbers => N sql = re.sub(r'-?[0-9]+', 'N', sql) # WHERE foo IN ('880987','882618','708228','522330') sql = re.sub(r' (IN|VALUES)\s*\([^,]+,[^)]+\)', ' \\1 (XYZ)', sql, flags=re.IGNORECASE) return sql.strip()
Example #27
Source File: test_parse.py From codenn with MIT License | 5 votes |
def test_tokenize(self): sql = 'select * from foo;' stmts = sqlparse.parse(sql) self.assertEqual(len(stmts), 1) self.assertEqual(str(stmts[0]), sql)
Example #28
Source File: utils.py From pgcli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def is_open_quote(sql): """Returns true if the query contains an unclosed quote""" # parsed can contain one or more semi-colon separated commands parsed = sqlparse.parse(sql) return any(_parsed_is_open_quote(p) for p in parsed)
Example #29
Source File: convert.py From sqlitis with MIT License | 5 votes |
def to_sqla(sql): sql = sql.strip() if not sql: raise Exception("Empty SQL string provided") tokens = sqlparse.parse(sql)[0].tokens tokens = remove_whitespace(tokens) return tokens_to_sqla(tokens).render()
Example #30
Source File: parseutils.py From litecli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def find_prev_keyword(sql): """ Find the last sql keyword in an SQL statement Returns the value of the last keyword, and the text of the query with everything after the last keyword stripped """ if not sql.strip(): return None, "" parsed = sqlparse.parse(sql)[0] flattened = list(parsed.flatten()) logical_operators = ("AND", "OR", "NOT", "BETWEEN") for t in reversed(flattened): if t.value == "(" or ( t.is_keyword and (t.value.upper() not in logical_operators) ): # Find the location of token t in the original parsed statement # We can't use parsed.token_index(t) because t may be a child token # inside a TokenList, in which case token_index thows an error # Minimal example: # p = sqlparse.parse('select * from foo where bar') # t = list(p.flatten())[-3] # The "Where" token # p.token_index(t) # Throws ValueError: not in list idx = flattened.index(t) # Combine the string values of all tokens in the original list # up to and including the target keyword token t, to produce a # query string with everything after the keyword token removed text = "".join(tok.value for tok in flattened[: idx + 1]) return t, text return None, ""