How to identify WHERE clause column's Value in SQL Queries using Python

59 Views Asked by At

Using python, I am working on grouping of similar SQL queries. To do this I want to identify the columns in WHERE clause from the sql query. Like in below example in WHERE clause total_amount,country,status. I have used sqlparse library. Following is import sqlparse

import sqlparse
sql_query = """
SELECT 
    customers.customer_id,
    customers.first_name,
    customers.last_name,
    orders.order_date
FROM 
    customers
INNER JOIN 
    orders ON customers.customer_id = orders.customer_id
WHERE 
    orders.total_amount > 1000
    AND customers.country = 'USA'
    AND (
        orders.status = 'Shipped'
        OR orders.status = 'In Progress'
    )
ORDER BY 
    orders.order_date DESC
LIMIT 10;
"""

# Parse the SQL query
parsed = sqlparse.parse(sql_query)

# Initialize a list to store WHERE clause conditions
where_conditions = []

# Flag to indicate whether we are inside the WHERE clause
inside_where = False

# Loop through the parsed tokens
for statement in parsed:
    for token in statement.tokens:
        if token.ttype is sqlparse.tokens.Keyword and token.value.upper() == "WHERE":
            inside_where = True
        elif inside_where and token.ttype is not sqlparse.tokens.Whitespace:
            # Ignore whitespace within the WHERE clause
            where_conditions.append(token.value.strip())
        elif inside_where and token.ttype is sqlparse.tokens.Keyword and token.value.upper() == "AND":
            # Treat "AND" as a separator between conditions
            where_conditions.append("AND")

# Join and print the WHERE clause conditions
where_clause = ' '.join(where_conditions)
print(where_clause)

But where_clause is NOT printing anything. How to resolve this?

0

There are 0 best solutions below