Skip to content
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

Build queries before acquiring the lock #199

Merged
merged 2 commits into from
Apr 28, 2020
Merged
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
21 changes: 12 additions & 9 deletions databases/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,30 +232,34 @@ async def __aexit__(
async def fetch_all(
self, query: typing.Union[ClauseElement, str], values: dict = None
) -> typing.List[typing.Mapping]:
built_query = self._build_query(query, values)
async with self._query_lock:
return await self._connection.fetch_all(self._build_query(query, values))
return await self._connection.fetch_all(built_query)

async def fetch_one(
self, query: typing.Union[ClauseElement, str], values: dict = None
) -> typing.Optional[typing.Mapping]:
built_query = self._build_query(query, values)
async with self._query_lock:
return await self._connection.fetch_one(self._build_query(query, values))
return await self._connection.fetch_one(built_query)

async def fetch_val(
self,
query: typing.Union[ClauseElement, str],
values: dict = None,
column: typing.Any = 0,
) -> typing.Any:
built_query = self._build_query(query, values)
async with self._query_lock:
row = await self._connection.fetch_one(self._build_query(query, values))
row = await self._connection.fetch_one(built_query)
return None if row is None else row[column]

async def execute(
self, query: typing.Union[ClauseElement, str], values: dict = None
) -> typing.Any:
built_query = self._build_query(query, values)
async with self._query_lock:
return await self._connection.execute(self._build_query(query, values))
return await self._connection.execute(built_query)

async def execute_many(
self, query: typing.Union[ClauseElement, str], values: list
Expand All @@ -267,11 +271,10 @@ async def execute_many(
async def iterate(
self, query: typing.Union[ClauseElement, str], values: dict = None
) -> typing.AsyncGenerator[typing.Any, None]:
built_query = self._build_query(query, values)
async with self.transaction():
async with self._query_lock:
async for record in self._connection.iterate(
self._build_query(query, values)
):
async for record in self._connection.iterate(built_query):
yield record

def transaction(self, *, force_rollback: bool = False) -> "Transaction":
Expand Down Expand Up @@ -466,8 +469,8 @@ def replace(self, **kwargs: typing.Any) -> "DatabaseURL":
kwargs["scheme"] = f"{dialect}+{driver}" if driver else dialect

if not kwargs.get("netloc", self.netloc):
# Using an empty string that evaluates as True means we end
# up with URLs like `sqlite:///database` instead of `sqlite:/database`
# Using an empty string that evaluates as True means we end up
# with URLs like `sqlite:///database` instead of `sqlite:/database`
kwargs["netloc"] = _EmptyNetloc()

components = self.components._replace(**kwargs)
Expand Down