coolqlite.to_sql

class coolqlite.to_sql.ToSqlConfig(*, configure_datetime_fns: bool = True)

Configure the ToSqlFnRegistry.

configure_datetime_fns: bool = True

Add named functions to convert various datetime types to sql.

class coolqlite.to_sql.ToSqlFnRegistry(*, config: ToSqlConfig = ToSqlConfig(configure_datetime_fns=True))

Dispatch to serializers based on name or type.

Register type-based dispatch as you would a functools.singledispatch():

registry = ToSqlFnRegistry()
@registry.register
def _handle_int(x: int) -> str:
    return str(x)

Or register by name:

registry = ToSqlFnRegistry()

@registry.add_named("reversed-int")
def _reversed_int(x: int) -> str:
    return str(x).reverse()

registry.add_named("upper", str.upper)
add_named(name: str) Callable[[F], F]
add_named(name: str, fn: ToSqliteConversionFn) None

Add a named converter. Usable as a decorator.

class coolqlite.to_sql.UuidAs(*values)

How do you want to serialize a UUID?

Right now only strings are offered, this may be expanded.

STRING = 'string'

coolqlite.to_sql.datetime_

Helpers to convert DateTimes to sqlite types.

Function Name

Default Named Converter

Accepts

aware_as_zulu_string()

z

AwareDateTime, AwareTime if already UTC

aware_as_keeptz_string()

keeptz

AwareDateTime, AwareTime

naive_as_string()

naive

NaiveDateTime, NaiveTime, Date

any_temporal_as_iso()

ANY_temporal

Any of the above

coolqlite.to_sql.datetime_.any_temporal_as_iso(temporal: NaiveDateTime | AwareDateTime | Date | NaiveTime | AwareTime, /) str
coolqlite.to_sql.datetime_.aware_as_zulu_string(temporal: AwareDateTime | AwareTime, /) str

Serialize an datetype.AwareDateTime by adjusting it to UTC, then serializing as an iso-format string with a Z at the end.

coolqlite.to_sql.datetime_.aware_as_keeptz_string(temporal: AwareDateTime | AwareTime, /) str

Serialize an datetype.AwareDateTime as an iso-format string.

coolqlite.to_sql.datetime_.naive_as_string(temporal: NaiveDateTime | NaiveTime | Date, /) str

Serialize a datetype.NaiveDateTime as an iso-format string.