Path functions
Path functions used in py-linq-sql.
get_path(magic_dp, sqle=None, force=False)
Get path from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/functions/path_functions.py
def get_path(
magic_dp: BaseMagicDotPath | Tuple[BaseMagicDotPath] | Dict[str, BaseMagicDotPath],
sqle: SQLEnumerableData | None = None,
force: bool = False,
) -> List[str]:
"""
Get path from a MagicDotPath.
Args:
magic_dp: A MagicDotPath objects contains a bases with element of the future
path.
sqle: SQLEnumerableData with connection, flags, list of commands and a table.
force: True if we want to force the json path in text, False otherwise.
By default: False.
Returns:
List a path transform by jsonb_path.
Raises:
TypeError: If magic_dp is not a subclass of BaseMagicDotPath.
psycopg.Error: Indirect raise by `_get_path_base_mdp`.
TableError: Indirect raise by `_get_path_base_mdp`.
"""
paths: List[str] = []
match magic_dp:
case BaseMagicDotPath():
_get_path_base_mdp(magic_dp, sqle, force, paths)
case tuple():
for element in magic_dp:
_get_path_base_mdp(element, sqle, force, paths)
case dict():
for element in magic_dp.values():
_get_path_base_mdp(element, sqle, force, paths)
case _:
raise TypeError(
"`get_path()` take only BaseMagicDotPath or tuple of BaseMagicDotPath.",
)
return paths
get_paths(fquery, sqle, as_str=False)
Get jsonb paths to build commands.
The format of paths give by the function: person->'address'->'zip_code'
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/functions/path_functions.py
def get_paths(
fquery: LambdaMagicDotPath,
sqle: SQLEnumerableData,
as_str: bool = False,
) -> List[str]:
"""
Get jsonb paths to build commands.
The format of paths give by the function: person->'address'->'zip_code'
Args:
fquery: Lambda function to get the path(s)
sqle: SQLEnumerableData with connection, flags, list of commands and a table.
as_str: False if we want basic paths, True if we want force the paths on string.
Returns:
List of paths.
Raises:
psycopg.Error: Indirect raise by `get_path`.
TableError: Indirect raise by `get_path`.
TypeError: Indirect raise by `get_path`.
TypeOperatorError: Indirect raise by `BaseMagicDotPath._get_number_operator`
or `BaseMagicDotPath._get_generic_operator`.
"""
return get_path(fquery(MagicDotPath(sqle.connection)), sqle, as_str)
get_update_path(path)
Get the correct format of path for UPDATE.
We need a path like: {0, 1, 2, 3}
.
Parameters: |
|
---|
Returns: |
|
---|
Source code in py_linq_sql/utils/functions/path_functions.py
def get_update_path(path: str) -> str:
"""
Get the correct format of path for UPDATE.
We need a path like: `{0, 1, 2, 3}`.
Args:
path: The path to get in the correct format for UPDATE.
Returns:
The path in the correct format for UPDATE.
"""
tmp = re.split(r">|-|'", path)
# Delete the first, is the column
tmp = tmp[1:]
# Join all not None str in tmp with the function filter()
# (https://docs.python.org/3/library/functions.html#filter)
result = ",".join(filter(None, tmp))
return "{" + result + "}"