cardinal_pythonlib.sqlalchemy.sqlfunc


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Functions to operate on SQL clauses for SQLAlchemy Core.

class cardinal_pythonlib.sqlalchemy.sqlfunc.extract_day_of_month(*clauses, **kwargs)[source]

Implements an SQLAlchemy extract_day() function (to extract the day of the month from a date/datetime expression). See extract_year().

Construct a FunctionElement.

Parameters:
  • *clauses – list of column expressions that form the arguments of the SQL function call.

  • **kwargs – additional kwargs are typically consumed by subclasses.

See also

func

Function

class cardinal_pythonlib.sqlalchemy.sqlfunc.extract_month(*clauses, **kwargs)[source]

Implements an SQLAlchemy extract_month() function. See extract_year().

Construct a FunctionElement.

Parameters:
  • *clauses – list of column expressions that form the arguments of the SQL function call.

  • **kwargs – additional kwargs are typically consumed by subclasses.

See also

func

Function

class cardinal_pythonlib.sqlalchemy.sqlfunc.extract_year(*clauses, **kwargs)[source]

Implements an SQLAlchemy extract_year() function, to extract the year from a date/datetime column.

YEAR, or func.year(), is specific to some DBs, e.g. MySQL. So is EXTRACT, or func.extract(); https://modern-sql.com/feature/extract.

This function therefore implements an extract_year function across multiple databases.

Use this as:

from cardinal_pythonlib.sqlalchemy.sqlfunc import extract_year

… then use extract_year() in an SQLAlchemy SELECT expression.

Here’s an example from CamCOPS:

select_fields = [
    literal(cls.__tablename__).label("task"),
    extract_year(cls._when_added_batch_utc).label("year"),
    extract_month(cls._when_added_batch_utc).label("month"),
    func.count().label("num_tasks_added"),
]

Construct a FunctionElement.

Parameters:
  • *clauses – list of column expressions that form the arguments of the SQL function call.

  • **kwargs – additional kwargs are typically consumed by subclasses.

See also

func

Function

cardinal_pythonlib.sqlalchemy.sqlfunc.extract_year_default(element: ClauseElement, compiler: SQLCompiler, **kw) NoReturn[source]

Default implementation of :func:, which raises NotImplementedError.

cardinal_pythonlib.sqlalchemy.sqlfunc.fail_unknown_dialect(compiler: SQLCompiler, task: str) NoReturn[source]

Raise NotImplementedError in relation to a dialect for which a function hasn’t been implemented (with a helpful error message).

cardinal_pythonlib.sqlalchemy.sqlfunc.fetch_processed_single_clause(element: ClauseElement, compiler: SQLCompiler) str[source]

Takes a clause element that must have a single clause, and converts it to raw SQL text.