neuralib.sqlp.annotation

final class neuralib.sqlp.annotation.CURRENT_DATE[source]

annotated a date field which use current date as its default.

>>> class Example:
...     a: Annotated[datetime.date, CURRENT_DATE]
__init__()[source]
final class neuralib.sqlp.annotation.CURRENT_TIME[source]

annotated a time field which use current time as its default.

>>> class Example:
...     a: Annotated[datetime.time, CURRENT_TIME]
__init__()[source]
final class neuralib.sqlp.annotation.CURRENT_TIMESTAMP[source]

annotated a datetime field which use current datetime as its default.

>>> class Example:
...     a: Annotated[datetime.datetime, CURRENT_TIMESTAMP]
__init__()[source]
final class neuralib.sqlp.annotation.PRIMARY[source]

annotate a field as a primary key.

>>> class Example:
...     a: Annotated[str, PRIMARY]  # style 1
__init__(order=None, conflict=None, auto_increment=False)[source]
Parameters:
  • order (Literal['ASC', 'DESC'] | None)

  • conflict (Literal['rollback', 'abort', 'fail', 'ignore', 'replace'] | None)

final class neuralib.sqlp.annotation.UNIQUE[source]

annotated a field as a unique key.

>>> class Example:
...     a: Annotated[str, UNIQUE]  # style 1
__init__(conflict=None)[source]
Parameters:

conflict (Literal['rollback', 'abort', 'fail', 'ignore', 'replace'] | None)

neuralib.sqlp.annotation.unique(conflict=None)[source]

A decorator to create an unique constraint.

Parameters:

conflict (Literal['rollback', 'abort', 'fail', 'ignore', 'replace'] | None)

neuralib.sqlp.annotation.foreign(*field, update='NO ACTION', delete='NO ACTION')[source]

A decorator to create a foreign constraint.

Common use:

With a foreign table

>>> class ForeignTable:
...     a: Annotated[str, str]
...     b: Annotated[str, str]
  1. mapping one-by-one

>>> class Example:
...     a: Annotated[str, str]
...     b: Annotated[str, str]
...     @foreign(ForeignTable.a, ForeignTable.b)
...     def _foreign(self):
...         return self.a, self.b
  1. by default, with the primary keys for the referred foreign table.

>>> class Example:
...     a: Annotated[str, str]
...     b: Annotated[str, str]
...     @foreign(ForeignTable)
...     def _foreign(self):
...         return self.a, self.b
  1. Self refered.

>>> class Example:
...     a: Annotated[str, str]
...     b: Annotated[str, str]
...     @foreign('a')
...     def _foreign(self):
...         return self.b
Parameters:
  • field – a foreign table or foreign fields.

  • update (Literal['SET NULL', 'SET DEFAULT', 'CASCADE', 'RESTRICT', 'NO ACTION']) – update policy

  • delete (Literal['SET NULL', 'SET DEFAULT', 'CASCADE', 'RESTRICT', 'NO ACTION']) – delete policy

neuralib.sqlp.annotation.check(field=None)[source]

A decorator to make a check constraint.

  1. by a field

>>> class Example:
...     a: str
...     @check('a')
...     def check_a(self):
...         return self.a != ''
  1. by over all

>>> class Example:
...     a: str
...     @check()
...     def check_a(self):
...         return self.a != ''
Parameters:

field (str | None) – field name.