neuralib.sqlp.util

neuralib.sqlp.util.str_to_datetime(t)[source]
Parameters:

t (str)

Return type:

datetime

neuralib.sqlp.util.datetime_to_str(t)[source]
Parameters:

t (datetime)

Return type:

str

neuralib.sqlp.util.take(index: int, coll: Cursor | Iterable[tuple[T, ...]]) list[T][source]
neuralib.sqlp.util.take(index: type[V], coll: Cursor | Iterable[tuple[T, ...]]) list[V]
neuralib.sqlp.util.take(index: tuple[int, ...], coll: Cursor | Iterable[tuple[T, ...]]) list[tuple[T, ...]]
neuralib.sqlp.util.take(index: tuple[V], coll: Cursor | Iterable[T]) list[tuple[V]]
neuralib.sqlp.util.take(index: V, coll: Cursor | Iterable[T]) list[V]

A help function that compose itemgetter and mapping functions.

>>> @named_tuple_table_class
... class A:
...     a: int
...     b: str
>>> take(0, [(0, 'a'), (1, 'b')])
[0, 1]
>>> take(A.a, [A(0, 'a'), A(1, 'b')])
[0, 1]
Parameters:
  • index

  • coll

Returns:

neuralib.sqlp.util.infer_eq(x, v, *, prepend='', append='')[source]

A help function to make a SQL = expression.

>>> infer_eq(A.a, 1) 
A.a = 1
>>> infer_eq(A.a, '!1') 
A.a != 1
>>> infer_eq(A.a, '1%') 
A.a LIKE '1%'
Parameters:
  • x (T)

  • v (T | str)

  • prepend (str)

  • append (str)

Returns:

Return type:

SqlExpr | None

neuralib.sqlp.util.infer_cmp(x, v)[source]

A help function to make a SQL comparison expression.

>>> infer_cmp(A.a, range(0, 10))  
A.a BETWEEN 0 AND 9
>>> infer_cmp(A.a, slice(0, 10))  
A.a BETWEEN 0 AND 10
>>> infer_cmp(A.a, '<10')  
A.a < 10
>>> infer_cmp(A.a, 10)  
A.a = 10
Parameters:
  • x (T)

  • v (T | str | range | slice)

Returns:

Return type:

SqlExpr | None

neuralib.sqlp.util.infer_in(x, v)[source]

A help function to make a SQL containing expression.

>>> infer_in(A.a, '1')  
A.a == '1'
>>> infer_in(A.a, range(0, 10))  
A.a BETWEEN 0 AND 9
>>> infer_in(A.a, ['a', 'b'])  
A.a IN ('a', 'b')
Parameters:
  • x (T)

  • v (T | str | list[str] | slice | range)

Returns:

Return type:

SqlExpr | None

neuralib.sqlp.util.resolve_field_type(f_type)[source]

SQL primary types:

  • bool: BOOLEAN

  • int: INT

  • float: FLOAT

  • str: TEXT

  • bytes: BLOB

  • datetime.date: DATETIME

  • datetime.datetime: DATETIME

Python type mapping

  • T|None: resolve_field_type(T) null-able

  • T|V : supported not

  • Literal: str

  • Path: str

Parameters:

f_type (type)

Returns:

(raw_type, sql_type, not_null)

Return type:

tuple[type, type, bool]

neuralib.sqlp.util.cast_to_sql(raw_type, sql_type, value)[source]
Parameters:
  • raw_type (type[T])

  • sql_type (type[V])

  • value (T)

Return type:

V

neuralib.sqlp.util.cast_from_sql(raw_type, sql_type, value)[source]
Parameters:
  • raw_type (type[T])

  • sql_type (type[V])

  • value (V)

Return type:

T

neuralib.sqlp.util.get_fields_from_schema(schema)[source]
Parameters:

schema (str)

Return type:

list[str]

neuralib.sqlp.util.map_foreign(value, foreign)[source]

Let a table T with a foreign constraint refer to table V, map a T data to the V data.

Parameters:
  • value (T)

  • foreign (type[V]) – a foreign constraint

Returns:

Return type:

Cursor[V]

neuralib.sqlp.util.pull_foreign(target, foreign)[source]

Let a table T with a foreign constraint refer to table V, pull T data from a V data.

Parameters:
  • target (type[T]) – target table T

  • foreign (V) – a foreign data V referred to.

Returns:

Return type:

Cursor[T]

neuralib.sqlp.util.rich_sql_table(table, value)[source]
Parameters:
  • table (type[T])

  • value (list[T])