Files
SQLmem/src/sqlmem/_sql.py
T

28 lines
955 B
Python

"""SQL identifier quoting.
Table and column names are interpolated into statements as raw strings, so a
name with a space, a reserved word, or an embedded quote would break the query
(and is a latent injection vector). These helpers quote identifiers safely. The
in-memory cache is SQLite, so it uses double-quote style; the source DB is quoted
in its configured dialect (e.g. T-SQL ``[brackets]``).
"""
from collections.abc import Iterable
from sqlglot import exp
def quote(name: str) -> str:
"""Quote an identifier for the in-memory SQLite cache."""
return '"' + name.replace('"', '""') + '"'
def quote_list(names: Iterable[str]) -> str:
"""Comma-join SQLite-quoted identifiers."""
return ", ".join(quote(n) for n in names)
def quote_source(name: str, dialect: str) -> str:
"""Quote an identifier for the source DB in its dialect (e.g. T-SQL ``[x]``)."""
return exp.to_identifier(name, quoted=True).sql(dialect=dialect)