To maintain a single codebase for multiple database dialects, use sqlglot to build generic SQL constructs and then use SQLAlchemyConnection._transpile_query to convert them to the current connection's dialect.
Approach 1: General SQL Clause
Use sqlglot to build an expression, then pass the resulting SQL string to _transpile_query.
Approach 2: Dialect-Specific Source
If you have a complex query written in a specific dialect (e.g., DuckDB), use sqlglot.parse_one(query, read='dialect_name').sql() to generate a standard SQL string, which can then be transpiled to other dialects via _transpile_query.
from sqlglot import select, condition
from sql.connection import SQLAlchemyConnection
from sqlalchemy import create_engine
# 1. Setup connection
conn = SQLAlchemyConnection(engine=create_engine(url="sqlite://"))
# 2. Create generic SQL using sqlglot
where = condition("x=1").and_("y=1")
general_sql = select("*").from_("y").where(where).sql()
# 3. Transpile to the connection's dialect
transpiled = conn._transpile_query(general_sql)