Skip to content

Add move_rows() to query.py #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ What's New in the LabKey 3.0.0 package
- WAF encoding of parameters is initially supported with LabKey Server v23.09
- WAF encoding can be opted out of on execute_sql calls by specifying waf_encode_sql=False
- Query API - add optional parameters to insert_rows, update_rows, and delete_rows
- Query API - add move_rows()
- earliest compatible LabKey Server version: 24.1.0

What's New in the LabKey 2.6.1 package
==============================
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Query API - [sample code](samples/query_examples.py)
- **insert_rows()** - Insert rows into a table.
- **select_rows()** - Query and get results sets.
- **update_rows()** - Update rows in a table.
- **move_rows()()** - Move rows in a table.
- **truncate_table()** - Delete all rows from a table.

Domain API - [sample code](samples/domain_example.py)
Expand Down
72 changes: 72 additions & 0 deletions labkey/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,52 @@ def update_rows(
)


def move_rows(
server_context: ServerContext,
target_container_path: str,
schema_name: str,
query_name: str,
rows: any,
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
"""
Move a set of rows from the schema.query
:param server_context: A LabKey server context. See utils.create_server_context.
:param target_container_path: target labkey container path for the move
:param schema_name: schema of table
:param query_name: table name to move from
:param rows: Set of rows to move
:param container_path: source labkey container path if not already set in context
:param transacted: whether all of the updates should be done in a single transaction
:param audit_behavior: used to override the audit behavior for the update. See class query.AuditBehavior
:param audit_user_comment: used to provide a comment that will be attached to certain detailed audit log records
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""
url = server_context.build_url("query", "moveRows.api", container_path=container_path)

payload = {"targetContainerPath": target_container_path, "schemaName": schema_name, "queryName": query_name, "rows": rows}

if transacted is False:
payload["transacted"] = transacted

if audit_behavior is not None:
payload["auditBehavior"] = audit_behavior

if audit_user_comment is not None:
payload["auditUserComment"] = audit_user_comment

return server_context.make_request(
url,
json=payload,
timeout=timeout,
)


class QueryWrapper:
"""
Wrapper for all of the API methods exposed in the query module. Used by the APIWrapper class.
Expand Down Expand Up @@ -672,3 +718,29 @@ def update_rows(
audit_user_comment,
timeout
)

@functools.wraps(move_rows)
def move_rows(
self,
target_container_path: str,
schema_name: str,
query_name: str,
rows: any,
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
return move_rows(
self.server_context,
target_container_path,
schema_name,
query_name,
rows,
container_path,
transacted,
audit_behavior,
audit_user_comment,
timeout
)