cardinal_pythonlib.sqlalchemy.arrow_types
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.
SQLAlchemy type to hold a Python Arrow date/time. Uses a DATETIME or similar type in the database.
- class cardinal_pythonlib.sqlalchemy.arrow_types.ArrowMicrosecondType(*args, **kwargs)[source]
Based on ArrowType from SQLAlchemy-Utils, but copes with fractional seconds under MySQL 5.6.4+.
Construct a
TypeDecorator
.Arguments sent here are passed to the constructor of the class assigned to the
impl
class level attribute, assuming theimpl
is a callable, and the resulting object is assigned to theself.impl
instance attribute (thus overriding the class attribute of the same name).If the class level
impl
is not a callable (the unusual case), it will be assigned to the same instance attribute ‘as-is’, ignoring those arguments passed to the constructor.Subclasses can override this to customize the generation of
self.impl
entirely.- impl
alias of
DateTime
- load_dialect_impl(dialect: DefaultDialect) TypeEngine [source]
Return a
TypeEngine
object corresponding to a dialect.This is an end-user override hook that can be used to provide differing types depending on the given dialect. It is used by the
TypeDecorator
implementation oftype_engine()
to help determine what type should ultimately be returned for a givenTypeDecorator
.By default returns
self.impl
.
- process_bind_param(value: Any, dialect: DefaultDialect) datetime | None [source]
Receive a bound parameter value to be converted to DateTime.
- process_literal_param(value: Any, dialect: DefaultDialect) str [source]
Receive a literal parameter value to be rendered inline within a statement.
Note
This method is called during the SQL compilation phase of a statement, when rendering a SQL string. Unlike other SQL compilation methods, it is passed a specific Python value to be rendered as a string. However it should not be confused with the
_types.TypeDecorator.process_bind_param()
method, which is the more typical method that processes the actual value passed to a particular parameter at statement execution time.Custom subclasses of
_types.TypeDecorator
should override this method to provide custom behaviors for incoming data values that are in the special case of being rendered as literals.The returned string will be rendered into the output string.
- process_result_value(value: Any, dialect: DefaultDialect) Arrow | None [source]
Receive a result-row column value to be converted to Arrow
- property python_type: type
Return the Python type object expected to be returned by instances of this type, if known.
Basically, for those types which enforce a return type, or are known across the board to do such for all common DBAPIs (like
int
for example), will return that type.If a return type is not defined, raises
NotImplementedError
.Note that any type also accommodates NULL in SQL which means you can also get back
None
from any type in practice.