Skip to content

Commit

Permalink
Wrap strings in text type
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackYps committed Jun 4, 2024
1 parent 5624be3 commit 28b4ccf
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions service/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import AsyncEngine
from sqlalchemy import create_engine, text
from sqlalchemy.ext.asyncio import AsyncConnection as _AsyncConnection
from sqlalchemy.ext.asyncio import AsyncEngine as _AsyncEngine
from sqlalchemy.util import EMPTY_DICT


class FAFDatabase:
Expand All @@ -24,4 +26,41 @@ def acquire(self):
return self.engine.begin()

async def close(self):
await self.engine.dispose()
await self.engine.dispose()


class AsyncEngine(_AsyncEngine):
"""
For overriding the connection class used to execute statements.
This could also be done by changing engine._connection_cls, however this
is undocumented and probably more fragile so we subclass instead.
"""

def connect(self):
return AsyncConnection(self)


class AsyncConnection(_AsyncConnection):
async def execute(
self,
statement,
parameters=None,
execution_options=EMPTY_DICT,
**kwargs
):
"""
Wrap strings in the text type automatically and allows bindparams to be
passed via kwargs.
"""
if isinstance(statement, str):
statement = text(statement)

if kwargs and parameters is None:
parameters = kwargs

return await super().execute(
statement,
parameters=parameters,
execution_options=execution_options
)

0 comments on commit 28b4ccf

Please sign in to comment.