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: _ColumnExpressionOrLiteralArgument[Any])[source]
Implements an SQLAlchemy
extract_day()
function (to extract the day of the month from a date/datetime expression). Seeextract_year()
.Construct a
FunctionElement
.- Parameters:
See also
func
Function
- class cardinal_pythonlib.sqlalchemy.sqlfunc.extract_month(*clauses: _ColumnExpressionOrLiteralArgument[Any])[source]
Implements an SQLAlchemy
extract_month()
function. Seeextract_year()
.Construct a
FunctionElement
.- Parameters:
See also
func
Function
- class cardinal_pythonlib.sqlalchemy.sqlfunc.extract_year(*clauses: _ColumnExpressionOrLiteralArgument[Any])[source]
Implements an SQLAlchemy
extract_year()
function, to extract the year from a date/datetime column.YEAR
, orfunc.year()
, is specific to some DBs, e.g. MySQL. So isEXTRACT
, orfunc.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 SQLAlchemySELECT
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:
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
.