ExaFormatter#

class pyexasol.ExaFormatter(connection)[source]#

Bases: Formatter

pyexasol.ExaFormatter is a subclass of string.Formatter designed to prevent SQL injections in Exasol dynamic SQL queries.

Note:

It introduces set of placeholders to prevent SQL injections specifically in Exasol dynamic SQL queries. It also completely disables format_spec section of standard formatting since it has no use in context of SQL queries and may cause more harm than good.

You may access these functions using .format property of connection object. Example:

Examples:

>>> C = pyexasol.connect(...)
... print(C.format.escape('abc'))
convert_field(value, conversion)[source]#
classmethod escape(val)[source]#

Takes a raw value and converts it into an and escaped string.

Parameters:

val – Value to be escaped.

Returns:

A string where all single quotes ' have been replaced with two single quotes ''.

classmethod escape_ident(val)[source]#

Takes a raw value and converts it into an and escaped string.

Parameters:

val – Value to be escaped.

Returns:

A string where all double quotes " have been replaced with two double quotes "".

classmethod escape_like(val)[source]#

Escape LIKE-patterns.

Parameters:

val – Value to be escaped.

Returns:

A string where all double quotes \ have been replaced with \\, where % have been replaced with \%, where _ have been replaced with \_.

format_field(value, format_spec)[source]#
classmethod quote(val)[source]#

Escapes a string using pyexasol.ExaFormatter.escape() and wraps it in single quotes '.

classmethod quote_ident(val)[source]#

Escapes an object or a tuple of objects using pyexasol.ExaFormatter.escape_ident() and wraps it in double quotes ".

Parameters:

val – Raw identifier(s) to be escaped.

Returns:

The formatted and quoted identifier, or joined identifiers if a tuple was provided.

Return type:

str

classmethod safe_decimal(val)[source]#

Convert a decimal safely to string.

Parameters:

val – Decimal value to convert.

Returns:

Validates identifier as string.

Raises:

ValueError – If value is not valid.

safe_decimal_regexp = re.compile('^(\\+|-)?[0-9]+(\\.[0-9]+)?$')#
classmethod safe_float(val)[source]#

Convert a float safely to string.

Parameters:

val – Float value to convert.

Returns:

Validates identifier as string.

Raises:

ValueError – If value is not valid, e.g.: +infinity or -infinity.

safe_float_regexp = re.compile('^(\\+|-)?[0-9]+(\\.[0-9]+((e|E)(\\+|-)[0-9]+)?)?$')#
classmethod safe_ident(val)[source]#

Convert a raw identifier safely.

Parameters:

val (str or tuple) – Raw identifier(s).

Returns:

Validates identifier as string.

Raises:

ValueError If passed values is not a valid identifier (e.g. contains spaces) –

Warning

It puts it into SQL query without any quotting.

safe_ident_regexp = re.compile('^[A-Za-z_][A-Za-z0-9_]*$')#