Skip to content

Commit

Permalink
Expose bind argument for query method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ce11an committed Aug 26, 2024
1 parent c8a4c84 commit ba11e54
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions surrealdb/async_execution_mixins/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import annotations

import json
from typing import TYPE_CHECKING, List, Union
from typing import TYPE_CHECKING, Any, List, Optional, Union

from surrealdb.errors import SurrealDbError
from surrealdb.rust_surrealdb import (
Expand All @@ -22,16 +22,19 @@
class AsyncQueryMixin:
"""This class is responsible for the interface between python and the Rust SurrealDB library for creating a document."""

async def query(self: SurrealDB, query: str) -> List[dict]:
async def query(
self: SurrealDB, query: str, bind: Optional[dict[str, Any]] = None
) -> List[dict]:
"""
queries the database.
:param query: the query to run on the database
:param bind: Paramters to bind the query with.
:return: None
"""
try:
return json.loads(await rust_query_future(self._connection, query))[0]
return json.loads(await rust_query_future(self._connection, query, bind))[0]
except Exception as e:
raise SurrealDbError(e) from None

Expand Down
11 changes: 7 additions & 4 deletions surrealdb/execution_mixins/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import contextlib
import json
from typing import TYPE_CHECKING, List, Union
from typing import TYPE_CHECKING, Any, Dict, List, Union, Optional

from surrealdb.asyncio_runtime import AsyncioRuntime
from surrealdb.errors import SurrealDbError
Expand Down Expand Up @@ -43,17 +43,20 @@ def convert_nested_json_strings(data):
item[key] = json.loads(value)
return data

def query(self: SurrealDB, query: str) -> List[dict]:
def query(
self: SurrealDB, query: str, bind: Optional[Dict[str, Any]] = None
) -> List[dict]:
"""
queries the database.
:param query: the query to run on the database
:param bind: Paramters to bind the query with.
:return: None
"""

async def _query(connection, query):
return await rust_query_future(connection, query)
async def _query(connection, query, bind):
return await rust_query_future(connection, query, bind)

try:
loop_manager = AsyncioRuntime()
Expand Down

0 comments on commit ba11e54

Please sign in to comment.