SQL statements API

PGMob uses internal classes that wrap plaintext SQL queries and ensure safe parameter validation. These classes are eventually decoded by adapters into SQL statements with appropriate syntax, parameters and quotation.

class pgmob.sql.Composable

Common interface for SQL-like objects

compose() Composed

Method utilized by an adapter to retrieve a Composed object that contains a list of objects and is ready to be iterated upon. All iterated objects are guaranteed to be one of: SQL, Literal, Identifier.

Returns:

a composed iterable object

Return type:

Composed

class pgmob.sql.Composed(*args: Composable)

Composed object. Finalized state of co-joined query parts. Can be iterated upon to retrieve the individual parts in proper order.

compose() Composed

Method utilized by an adapter to retrieve a Composed object that contains a list of objects and is ready to be iterated upon. All iterated objects are guaranteed to be one of: SQL, Literal, Identifier.

Returns:

a composed iterable object

Return type:

Composed

class pgmob.sql.Identifier(identifier: str)

A database Identifier. Ensures proper quoting for identifiers.

Parameters:

identifier (str) – identifier name

compose() Composed

Method utilized by an adapter to retrieve a Composed object that contains a list of objects and is ready to be iterated upon. All iterated objects are guaranteed to be one of: SQL, Literal, Identifier.

Returns:

a composed iterable object

Return type:

Composed

value() object

Retrieve the underlying value of a Composable: a query string, identifier name or a value of a literal. Not supported on Composed objects, which need to be iterated upon first before calling .value() on the yielded Composable objects.

Returns:

the underlying object in a Composable

Return type:

object

class pgmob.sql.Literal(literal: object)

A Literal query parameter. Ensures proper quoting for literal values.

Parameters:

literal (object) – literal value

compose() Composed

Method utilized by an adapter to retrieve a Composed object that contains a list of objects and is ready to be iterated upon. All iterated objects are guaranteed to be one of: SQL, Literal, Identifier.

Returns:

a composed iterable object

Return type:

Composed

value() object

Retrieve the underlying value of a Composable: a query string, identifier name or a value of a literal. Not supported on Composed objects, which need to be iterated upon first before calling .value() on the yielded Composable objects.

Returns:

the underlying object in a Composable

Return type:

object

class pgmob.sql.SQL(statement: str)

A sql statememt

Parameters:

statement (str) – query text

compose() Composed

Method utilized by an adapter to retrieve a Composed object that contains a list of objects and is ready to be iterated upon. All iterated objects are guaranteed to be one of: SQL, Literal, Identifier.

Returns:

a composed iterable object

Return type:

Composed

format(*args: Composable, **kwargs: Composable) Composed

Format a query that contains a string formatter argument notation. Format values can be one of: Identifier, Literal.

Parameters:
  • *args – for {} or {0} notation

  • **kwargs – for {keyword} notation

Returns:

a composed query object

Return type:

Composed

Example

sql = SQL(“SELECT {column} FROM {table} WHERE id = {value}”).format(

column=Identifier(“col1”), table=Identifier(“mytable”), value=Literal(1)

)

join(iter: Sequence[Composable]) Composed

Joins basic query building blocks, such as SQL, Literal, Identifier.

Parameters:

iter – an iterable containing Composable elements

Returns:

a composed query object

Return type:

Composed

Example

sql = SQL(“SELECT col FROM “) + SQL(“.”).join(Identifier(“myschema”),Identifier(“mytable”))

value() object

Retrieve the underlying value of a Composable: a query string, identifier name or a value of a literal. Not supported on Composed objects, which need to be iterated upon first before calling .value() on the yielded Composable objects.

Returns:

the underlying object in a Composable

Return type:

object