ðïļQuery builder & formatter
SnowDDL builds a lot of raw SQL queries. In order to simplify this task while keeping the full flexibility of SQL, SnowDDL provides query builder and query formatter via its engine.
Query builder
Query builder is used to build complex SQL queries from small fragments. Each fragment can be added to the current line or it may start a new line.
For example, consider the following query fragments:
Final query:
Example code:
In order to create a new empty query builder, call function .query_builder()
of engine class.
Alternatively, you may create an instance of class SnowDDLQueryBuilder
directly.
Methods
append(sql, params=None)
Appends a fragment to the current line. Optionally formats it using placeholders and dictionary with params.append_nl(sql, params=None)
Starts a new line and append a fragment to it. Optionally formats it using placeholders and dictionary with params.fragment_count()
Returns a number of fragments across all lines.add_short_hash(comment)
Part of short hash feature. Takes the comment for object and adds a short hash at the end. Returns updated comment.compare_short_hash(comment)
Part of short hash feature. Extracts short hash from existing comment and compares it with expected short hash. ReturnsTrue
if hashes are the same, returnsFalse
otherwise.__str__()
Query builder objects can be used as normal strings.
Formatter
SnowDDL formatter uses its own custom syntax for placeholders and supports a wide range of placeholder types.
Syntax
Only named placeholders are supported. Positional placeholders are not allowed on purpose. It is important to reduce the amount of accidental mistakes.
The placeholder syntax is: {name:type}
Type is optional. Default type is :s
(string value).
Usage notes
Query formatting is performed only when params
are explicitly defined as dict
. It allows you to use your own query formatter if it is necessary.
All placeholders defined in SQL query text must be present in params
, there are no "defaults". It helps to prevent typos and accidental damage associated with it.
If you pass list of values for placeholder instead of single value, it will be represented as concatenated comma-separated string of individually formatted values, which is useful for IN (val1, val2, val3)
syntax.
Placeholder types
:s
- common value, enclosed in single quotes and escaped NULL value is returned asNULL
without quotes normally used for VARCHAR, TIMESTAMP, etc.:d
- safe decimal value, validated and formatted "as is" normally used for NUMBER, raw integer values in LIMIT, etc.:f
- safe float value, validated and formatted "as is" used for FLOAT values only, supports optional exponent:b
- safe boolean value, validated and formatted asTRUE
orFALSE
string used for BOOLEAN values only:i
- identifier, enclosed in double quotes and escaped used for identifiers of all types:ia
- identifier, but additionally enclosed in single quotes and escaped used for arguments of table functions which accept identifiers in this form:r
- raw value, formatted "as is", not validated, dangerous (!) used to add dynamic query fragments which cannot be constructed by other means:lf
- LIKE pattern looking for full match (LIKE '{val}'
):ls
- LIKE pattern looking for match starting with (LIKE '{val}%'
):le
- LIKE pattern looking for match ending with (LIKE '%{val}'
):lse
- LIKE pattern looking for match starting and ending with (LIKE '{start}%{end}'
) accepts tuple with two elements
Example of SQL with placeholders:
Query formatting code:
Result:
Overloading
You may extend or replace standard formatter by overloading SnowDDLFormatter
class and by explicitly setting it into overloaded property .formatter
of SnowDDLEngine
.
Last updated