Columns

Module for SQL column constructors.

class networkdisk.sql.columns.AbstractColumn(dialect, sqltype=None, container_name=None)

An abstract SQL column.

Parameters:
dialectDialect

The SQL dialect to use, represented as a Dialect object for finding dialect-wise classes.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

container_namestr or None, default=None

The name of the container (_e.g._, table, view, query) containing the column, if any, or None (default) otherwise.

Attributes:
dialectDialect

The SQL dialect to use, represented as a Dialect object for finding dialect-wise classes.

container_namestr or None, default=None

The name of the container (_e.g._, table, view, query) containing the column, if any, or None (default) otherwise.

Methods

qformat(context=None, force=None)

class networkdisk.sql.columns.CaseColumn(dialect, first, *conditions_and_targets, last=None, sqltype=None)

An SQL filter column: “CASE [col] [WHEN col THEN col]⁺ [ELSE col]”.

Parameters:
dialectDialect

The SQL dialect to use.

firstAbstractColumn or tuple

A single column col (yielding “CASE [col]…”) or a pair (cond, col) where cond is a condition and col is a column (yielding “CASE WHEN [cond] THEN [col]…”).

conditions_and_targetsiterable

An iterable of pairs (cond, col) where col is a column and cond is a condition. If first is a single column, then cond might be substituted with a value, to be replace by the condition col.eq(cond). Each element yields a “WHEN [cond] THEN [col]” statement.

lastAbstractColumn or tuple

A single column (yielding “… ELSE [col]”) or a element of the same kind as condition and col is a column (yielding “… WHEN [cond] THEN [col]” as if it were at the end of the conditions_and_targets iterable).

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

Attributes:
conditionstuple

The tuple of condition on which to filter.

on_targetAbstractColumn or None

The column to filter (if of the form “CASE [col] …”) or None otherwise.

elseAbstractColumn or None

The default column to use if no filter matched (in the case “ELSE [col]”) or None otherwise.

class networkdisk.sql.columns.CastColumn(dialect, sqltype, target)

An SQL cast column (“CAST([col] AS [type])”).

Parameters:
dialectDialect

The SQL dialect to use.

sqltypestr

The type to which to cast. This type is used as encoder key as well.

targetAbstractColumn

The column to cast.

class networkdisk.sql.columns.ConstantColumn(dialect, constant, sqltype=None)

A concrete SQL constant column (_e.g._, NULL, 1, *).

Parameters:
dialectDialect

The SQL dialect to use.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

constantany

The value of the constant.

Attributes:
constantany

The value of the constant.

class networkdisk.sql.columns.EncodedColumn(dialect, encoder=None, container_name=None, sqltype=None)

An abstract SQL column with associated encoding.

Parameters:
dialectDialect

The SQL dialect to use.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

encoderstr or None

A key of the encoderFunctions mapping.

container_name: str or None

The name of the column container.

Notes

Only the two above properties are relevant for this extension of AbstractColumn. However, for serialization motivations, the encoder is specified as a key of the encoderFunctions mapping.

Attributes:
encode
decode
encoderstr or None, default=None

The key of the encoderFunctions mapping that defines the encoder and the decoder. The None key (default) correspond to identity encoding.

class networkdisk.sql.columns.NullColumn(dialect)

The special NULL constant column.

Notes

This class should be a singleton class.

class networkdisk.sql.columns.OperatorColumn(dialect, transform, *target, decoder=None, distinct=False, sqltype=<function common_type_in_tuple>)

An SQL column resulting from operations on columns.

Parameters:
dialectDialect

The SQL dialect to use.

transformstr

The plain SQL function to apply (_e.g._, “LOWER”, “COUNT”, “SUM”).

*targetiterable

The referenced columns on which to apply the transformation.

Attributes:
decode
namestr

The operation to perform on target columns (_e.g._, “+”, “-”, “*”, “/”).

class networkdisk.sql.columns.PlaceholderColumn(dialect, for_column=None, sqltype=None, encoder=None, name=None)

A placeholder, namely an SQL value column with implicit value.

Parameters:
dialectDialect

The SQL dialect to use.

for_columnAbstractColumn or None, default=None

A column targetted by the value column (_e.g._, when the intention is to insert the given value in a table, the corresponding table column is referenced here) or None. If given, its “encoder” attribute, if any, is taken as fallback if the parameter encoder has value None. In the same way, the value column is using the for_column “name” attribute, if any and if the parameter name has value None.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

encoderstr or None

A key of encoderFunctions to get the associated encoder and decoder. If not given, the parameter value might be taken from the attribute ‘encoder’ of for_column, if given, if any.

namestr or None

The name of the value column. If not given, the parameter value might be taken from the attribute ‘name’ of for_column, if given, if any.

class networkdisk.sql.columns.QueryColumn(dialect, name, index_in_container, encoder=None, container_name=None, sqltype=None)

A concrete SQL encoded column with a name.

Parameters:
dialectDialect

The SQL dialect to use.

namestr

The column name.

index_in_containerint

The index of the column in the container query.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

encoderstr or None

A key of the encoderFunctions mapping.

container_name: str or None

The name of the column container.

Attributes:
name: str

The column name.

index_in_container:

The container index.

Methods

qformat(query, alias=None, context=None, force=False)

qformat(context=None, force=False)
  • context: either None or a InternalColumns scope from which to recover the context of self and name conflicts using the __getitem__ and sources.get methods. If None the name of self only is returned (self.name) without checking ambiguity. Otherwise, the context query name (if any) is prepended followed by a dot to self name, e.g. “table.col”, when needed. More precisely, this query name is prepended if there exists another column in the scope presented by context has same name. An NetworkDiskError exception is raised when this happen and one of the two following cases hold: self does not have a source query in context, self context query does not have a name.

class networkdisk.sql.columns.StarColumn(dialect)

The special * (STAR) constant pseudo-column.

Notes

This class should be a singleton class.

class networkdisk.sql.columns.TransformColumn(dialect, transform, *target, decoder=None, distinct=False, sqltype=None)

An SQL column resulting from the transformation of another column.

Parameters:
dialectDialect

The SQL dialect to use.

transformstr

The plain SQL function to apply (_e.g._, “LOWER”, “COUNT”, “SUM”).

*targetiterable

The referenced columns on which to apply the transformation.

decoderstr or None, default=None

A key of encoderFunctions mapping that allows to specify how the values of this column can be decoded. Notice that values are never encoded for this column.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

distinctbool, default=False

Whether the transformation should be applied on distinct values of the columns from target.

Attributes:
decode
namestr

The name of the column, which equals the transformation function.

encoderstr or None

A key of encoderFunctions mapping that allows to specify how the values of this column can be decoded. Notice that values are never encoded for this column.

distinctbool, default=False

Whether the transformation should be applied on distinct values of the columns from target.

class networkdisk.sql.columns.TriggerColumn(dialect, target, new=True)

A column referencing another to be used in triggers (_e.g._, NEW.c).

Parameters:
dialectDialect

The SQL dialect to use.

targetAbstractColumn

The referenced column.

newbool, default=True

Whether the trigger column is a NEW or OLD column (default is NEW).

Attributes:
targetAbstractColumn

The referenced column.

newbool, default=True

Whether the trigger column is a NEW or OLD column (default is NEW).

class networkdisk.sql.columns.TupleColumn(dialect, *target, sqltype=None)

An SQL tuple column formed by a tuple of SQL columns.

Parameters:
dialectDialect

The SQL dialect to use.

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

*targetiterable

The columns to gather in the tuple. Elements which are not columns (_i.e._, which do not implement the qformat method) are considered as values, and thus replaced by the corresponding ConstantColumn within the tuple.

sqltypestr or None, default=None

The sql type of the column.

Attributes:
encode
decode
encodertuple

A tuple of keys (str or None) of the encoderFunctions mapping.

targettuple

The tuple of gathered columns.

Methods

qformat(context=None, force=False, distinct=False)

class networkdisk.sql.columns.ValueColumn(dialect, value, for_column=None, sqltype=None, encoder=None, name=None)

An SQL encoded column with a value.

Parameters:
dialectDialect

The SQL dialect to use.

valueany

The value associated with the column. Any object is accepted, although it should be encodable according to the encode function associated with the encoder parameter (see below).

sqltypestr or None, default=None

The type (e.g. “int”, “text” of the column).

for_columnAbstractColumn or None, default=None

A column targetted by the value column (_e.g._, when the intention is to insert the given value in a table, the corresponding table column is referenced here) or None. If given, its “encoder” attribute, if any, is taken as fallback if the parameter encoder has value None. In the same way, the value column is using the for_column “name” attribute, if any and if the parameter name has value None.

encoderstr or None

A key of encoderFunctions to get the associated encoder and decoder. If not given, the parameter value might be taken from the attribute ‘encoder’ of for_column, if given, if any.

namestr or None

The name of the value column. If not given, the parameter value might be taken from the attribute ‘name’ of for_column, if given, if any.

Notes

The difference with the ConstantColumn class is that here the value is not hard coded within queries using the column, but a placeholder is used instead. This allows in particular to perform ‘executemany’ queries with different values for the column.

Attributes:
valueany

The value associated with the column.

for_columnAbstractColumn or None

A related column or None.

Methods

__eq__(other)