neuralib.sqlp.table

class neuralib.sqlp.table.Field[source]

A SQL column, a field in a table.

table: type

associated table

name: str

column/field name

raw_type: type

origin field type

sql_type: type

column/field type. Should be supported by SQL

f_value: Any

default value.

not_null: bool

Is it not NULL.

annotated: list[Any]

Alias for field number 6

property table_name: str
property has_default: bool
property is_primary: bool
get_primary()[source]
Return type:

PRIMARY | None

property is_unique: bool
get_unique()[source]
Return type:

UNIQUE | None

get_annotation(annotation_type)[source]
Parameters:

annotation_type (type[T])

Return type:

T | None

static __new__(_cls, table, name, raw_type, sql_type, f_value=<object object>, not_null=True, annotated=())

Create new instance of Field(table, name, raw_type, sql_type, f_value, not_null, annotated)

Parameters:
  • table (type)

  • name (str)

  • raw_type (type)

  • sql_type (type)

  • f_value (Any)

  • not_null (bool)

  • annotated (list[Any])

neuralib.sqlp.table.table_name(table)[source]

the name of the table.

Parameters:

table (type[T])

Return type:

str

neuralib.sqlp.table.table_field_names(table)[source]

list of the name for each field in the table.

Parameters:

table (type[T])

Return type:

list[str]

neuralib.sqlp.table.table_fields(table)[source]

list fields in the table.

Parameters:

table (type[T])

Return type:

list[Field]

neuralib.sqlp.table.table_field(table, field)[source]

Get field by the name in the table.

Parameters:
  • table (type[T])

  • field (str)

Returns:

Raises:

RuntimeError – no such field.

Return type:

Field

neuralib.sqlp.table.table_primary_fields(table)[source]

list of the name for each primary field in the table.

Parameters:

table (type[T])

Return type:

list[Field]

class neuralib.sqlp.table.UniqueConstraint[source]

UniqueConstraint(name, table, fields, conflict)

name: str

constraint name

table: type

associated table

fields: list[str]

associated fields

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

Alias for field number 3

static __new__(_cls, name, table, fields, conflict)

Create new instance of UniqueConstraint(name, table, fields, conflict)

Parameters:
  • name (str)

  • table (type)

  • fields (list[str])

  • conflict (CONFLICT_POLICY | None)

neuralib.sqlp.table.table_unique_fields(table)[source]

list of the name for each unique field in the table.

Parameters:

table (type[T])

Return type:

list[UniqueConstraint]

neuralib.sqlp.table.make_unique_constraint(table, prop, conflict=None)[source]
Parameters:
  • table (Table)

  • prop (callable)

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

Return type:

UniqueConstraint

class neuralib.sqlp.table.ForeignConstraint[source]

SQL foreign constraint.

name: str

constraint name

table: type

associated table

fields: list[str]

associated fields

foreign_table: type

referred foreign table

foreign_fields: list[str]

referred foreign fields

on_update: Literal['SET NULL', 'SET DEFAULT', 'CASCADE', 'RESTRICT', 'NO ACTION']

Alias for field number 5

on_delete: Literal['SET NULL', 'SET DEFAULT', 'CASCADE', 'RESTRICT', 'NO ACTION']

Alias for field number 6

property table_name: str
static __new__(_cls, name, table, fields, foreign_table, foreign_fields, on_update, on_delete)

Create new instance of ForeignConstraint(name, table, fields, foreign_table, foreign_fields, on_update, on_delete)

Parameters:
  • name (str)

  • table (type)

  • fields (list[str])

  • foreign_table (type)

  • foreign_fields (list[str])

  • on_update (FOREIGN_POLICY)

  • on_delete (FOREIGN_POLICY)

property foreign_table_name: str
neuralib.sqlp.table.table_foreign_fields(table)[source]

get a list of the foreign constraint in the table.

Parameters:

table (type[T])

Return type:

list[ForeignConstraint]

neuralib.sqlp.table.table_foreign_field(table: Callable) ForeignConstraint | None[source]
neuralib.sqlp.table.table_foreign_field(table: type[T], target: type[F]) ForeignConstraint | None

get the foreign constraint in the table that refer to the target table.

Parameters:
  • table – table or a foreign constraint function/property (the function decorated by @foreign)

  • target – refer table

Returns:

foreign constraint.

neuralib.sqlp.table.make_foreign_constrain(table, prop, fields, update, delete)[source]
Parameters:
  • table (Table)

  • prop (callable)

  • fields (list)

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

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

Return type:

ForeignConstraint

class neuralib.sqlp.table.CheckConstraint[source]

SQL check constraint

static __new__(_cls, name, table, field, expression)

Create new instance of CheckConstraint(name, table, field, expression)

Parameters:
  • name (str)

  • table (type)

  • field (str | None)

  • expression (SqlExpr)

name: str

constraint name

table: type

associated table

field: str | None

associated field’s name.

expression: SqlExpr

checking expression

neuralib.sqlp.table.table_check_fields(table)[source]

get a dict of the field constraint in the table.

Parameters:

table (type[T])

Return type:

dict[str | None, CheckConstraint]

neuralib.sqlp.table.table_check_field(table, field)[source]

get the check constrain of a field in the table.

Parameters:
  • table (type[T])

  • field (str | None)

Return type:

CheckConstraint | None

neuralib.sqlp.table.make_check_constraint(table, prop, field)[source]
Parameters:
  • table (Table)

  • prop (callable)

  • field (str | None)

Return type:

CheckConstraint