SqlFormat
- class SqlFormat(dialect, query, right_margin=80, indent=4)
Renders a query object into a SQL string using
%splaceholders (DB-APIformatstyle). Not typically instantiated directly — useSQL.format(query),MYSQL.format(query), orMSSQL.format(query).- Parameters:
dialect – A
Dialectconstant identifying the SQL dialect.query – The query object to render (any
SqlObjwith a.sql()method).right_margin – Maximum line width before wrapping. Default
80.indent – Number of spaces per indent level. Default
4.
- get_sql()
Returns the formatted SQL string with placeholders.
- Return type:
str
qb = SQL.select().from_table('users').column('name').where('id', '=', 42) fmt = SQL.format(qb) fmt.get_sql() # 'SELECT name\nFROM users\nWHERE id = %s'
- get_args()
Returns the collected parameter values. A list for positional styles, a dict for named styles.
- Return type:
list or dict
fmt.get_args() # [42]
- get_sqlargs()
Returns
(sql, args)tuple, suitable for passing directly to a DB-API cursor.- Return type:
tuple[str, list | dict]
cur.execute(*SQL.format(qb).get_sqlargs())
- is_empty()
Returns
Trueif no SQL was generated.- Return type:
bool
- param(value_obj)
Emit a placeholder and collect the argument. Called internally by
SqlValueandSqlNamedValue.- Parameters:
value_obj – A
SqlValueorSqlNamedValueinstance.
- write_query(query)
Write a
SqlQueryas a parenthesized, indented subquery.- Parameters:
query – A
SqlQueryinstance to render.
- comment(comment)
Write a SQL comment block. Each line of the comment string is emitted prefixed with
-- ``, followed by a newline. If *comment* is ``Noneor empty, nothing is written.- Parameters:
comment – A string (may contain newlines), or
None.
- class SqlFormatQmark(dialect, query, right_margin=80, indent=4)
Renders using
?placeholders (DB-APIqmarkstyle). Not typically instantiated directly — useSQLITE.format(query). Arguments collected as a list.Inherits all methods from
SqlFormat.
- class SqlFormatNumeric(dialect, query, right_margin=80, indent=4)
Renders using
:1,:2placeholders (DB-APInumericstyle). Not typically instantiated directly — useORACLE.format_numeric(query). Arguments collected as a list.Inherits all methods from
SqlFormat.
- class SqlFormatNamed(dialect, query, right_margin=80, indent=4)
Renders using
:nameplaceholders (DB-APInamedstyle). Not typically instantiated directly — useORACLE.format(query). Arguments collected as a dict.Requires all parameters to be
SqlNamedValueinstances. RaisesTypeErrorif a positionalSqlValueis encountered.Inherits all methods from
SqlFormat.
- class SqlFormatPyformat(dialect, query, right_margin=80, indent=4)
Renders using
%(name)splaceholders (DB-APIpyformatstyle). Not typically instantiated directly — useSQL.format_pyformat(query)orMYSQL.format_pyformat(query). Arguments collected as a dict.Requires all parameters to be
SqlNamedValueinstances. RaisesTypeErrorif a positionalSqlValueis encountered.Inherits all methods from
SqlFormat.