Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit c4cbf2a

Browse files
committed
Get rid of experimental solution and minor fixes.
1 parent 6b77bae commit c4cbf2a

File tree

6 files changed

+6
-111
lines changed

6 files changed

+6
-111
lines changed

databases/backends/mysql.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sqlalchemy.sql import ClauseElement
1111
from sqlalchemy.types import TypeEngine
1212

13-
from databases.core import DatabaseURL, NoBackendMethod
13+
from databases.core import DatabaseURL
1414
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
1515

1616
logger = logging.getLogger("databases")
@@ -135,25 +135,6 @@ async def execute_many(self, query: ClauseElement, values: list) -> None:
135135
finally:
136136
await cursor.close()
137137

138-
async def raw_api_call(self, method: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
139-
"""
140-
NOTE: highly experimental, seems to be a dead-end for generalized solution
141-
"""
142-
assert self._connection is not None, "Connection is not acquired"
143-
cursor = await self._connection.cursor()
144-
try:
145-
if 'execute' not in method:
146-
await cursor.execute(*args, **kwargs)
147-
api_method = getattr(cursor, method)
148-
return await api_method()
149-
else:
150-
api_method = getattr(cursor, method)
151-
return await api_method(*args, **kwargs)
152-
except AttributeError:
153-
raise NoBackendMethod(f'{self.database._backend._dialect.driver} has no "{method}" implemented.')
154-
finally:
155-
await cursor.close()
156-
157138
async def expose_backend_connection(self) -> aiomysql.connection.Connection:
158139
assert self._connection is not None, "Connection is not acquired"
159140
return self._connection

databases/backends/postgres.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlalchemy.engine.interfaces import Dialect
88
from sqlalchemy.sql import ClauseElement
99

10-
from databases.core import DatabaseURL, NoBackendMethod
10+
from databases.core import DatabaseURL
1111
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
1212

1313
logger = logging.getLogger("databases")
@@ -144,17 +144,6 @@ async def execute_many(self, query: ClauseElement, values: list) -> None:
144144
single_query, args, result_columns = self._compile(single_query)
145145
await self._connection.execute(single_query, *args)
146146

147-
async def raw_api_call(self, method: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
148-
"""
149-
NOTE: highly experimental, seems to be a dead-end for generalized solution
150-
"""
151-
assert self._connection is not None, "Connection is not acquired"
152-
try:
153-
api_method = getattr(self._connection, method)
154-
return await api_method(*args, **kwargs)
155-
except AttributeError:
156-
raise NoBackendMethod(f'{self.database._backend._dialect.driver} has no "{method}" implemented.')
157-
158147
async def expose_backend_connection(self) -> asyncpg.connection.Connection:
159148
assert self._connection is not None, "Connection is not acquired"
160149
return self._connection

databases/backends/sqlite.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sqlalchemy.sql import ClauseElement
1010
from sqlalchemy.types import TypeEngine
1111

12-
from databases.core import DatabaseURL, NoBackendMethod
12+
from databases.core import DatabaseURL
1313
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
1414

1515
logger = logging.getLogger("databases")
@@ -116,17 +116,6 @@ async def execute_many(self, query: ClauseElement, values: list) -> None:
116116
for value in values:
117117
await self.execute(query, value)
118118

119-
async def raw_api_call(self, method: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
120-
"""
121-
NOTE: highly experimental, seems to be a dead-end for generalized solution
122-
"""
123-
assert self._connection is not None, "Connection is not acquired"
124-
try:
125-
api_method = getattr(self._connection, method)
126-
return await api_method(*args, **kwargs)
127-
except AttributeError:
128-
raise NoBackendMethod(f'{self.database._backend._dialect.driver} has no "{method}" implemented.')
129-
130119
async def expose_backend_connection(self) -> aiosqlite.core.Connection:
131120
assert self._connection is not None, "Connection is not acquired"
132121
return self._connection

databases/core.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ async def execute_many(self, query: ClauseElement, values: list) -> None:
104104
async with self.connection() as connection:
105105
return await connection.execute_many(query=query, values=values)
106106

107-
async def raw_api_call(self, method: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
108-
async with self.connection() as connection:
109-
return await connection.raw_api_call(method, *args, **kwargs)
110-
111107
async def expose_backend_connection(self) -> typing.Any:
112108
async with self.connection() as connection:
113109
return await connection.expose_backend_connection()
@@ -176,9 +172,6 @@ async def execute(self, query: ClauseElement, values: dict = None) -> typing.Any
176172
async def execute_many(self, query: ClauseElement, values: list) -> None:
177173
await self._connection.execute_many(query, values)
178174

179-
async def raw_api_call(self, method: str, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
180-
return await self._connection.raw_api_call(method, *args, **kwargs)
181-
182175
async def expose_backend_connection(self) -> typing.Any:
183176
return await self._connection.expose_backend_connection()
184177

@@ -362,7 +355,3 @@ def __repr__(self) -> str:
362355
if self.password:
363356
url = str(self.replace(password="********"))
364357
return f"{self.__class__.__name__}({repr(url)})"
365-
366-
367-
class NoBackendMethod(Exception):
368-
pass

databases/interfaces.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ async def execute(self, query: ClauseElement, values: dict = None) -> typing.Any
3333
async def execute_many(self, query: ClauseElement, values: list) -> None:
3434
raise NotImplementedError() # pragma: no cover
3535

36+
async def expose_backend_connection(self) -> typing.Any:
37+
raise NotImplementedError() # pragma: no cover
38+
3639
async def iterate(
3740
self, query: ClauseElement
3841
) -> typing.AsyncGenerator[typing.Mapping, None]:

tests/test_databases.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -516,62 +516,6 @@ async def get_connection_2():
516516
await task_2
517517

518518

519-
@pytest.mark.parametrize("database_url", DATABASE_URLS)
520-
@async_adapter
521-
async def test_queries_with_raw_api_call(database_url):
522-
"""
523-
Test that the basic `execute()`, `execute_many()`, `fetch_all()``, and
524-
`fetch_one()` interfaces are working as expected being called as raw driver calls.
525-
"""
526-
async with Database(database_url) as database:
527-
async with database.transaction(force_rollback=True):
528-
# Insert query
529-
if str(database_url).startswith('mysql'):
530-
insert_query = "INSERT INTO notes (text, completed) VALUES (%s, %s)"
531-
else:
532-
insert_query = "INSERT INTO notes (text, completed) VALUES ($1, $2)"
533-
534-
# execute()
535-
values = ("example1", True)
536-
537-
if str(database_url).startswith('postgresql'):
538-
await database.raw_api_call('execute', insert_query, *values)
539-
else:
540-
await database.raw_api_call('execute', insert_query, values)
541-
542-
# execute_many()
543-
values = [("example2", False), ("example3", True)]
544-
await database.raw_api_call('executemany', insert_query, values)
545-
546-
# Select query
547-
select_query = "SELECT notes.id, notes.text, notes.completed FROM notes"
548-
549-
# fetch_all()
550-
if str(database_url).startswith('postgresql'):
551-
results = await database.raw_api_call('fetch', select_query)
552-
elif str(database_url).startswith('mysql'):
553-
results = await database.raw_api_call('fetchall', select_query)
554-
elif str(database_url).startswith('sqlite'):
555-
results = await database.raw_api_call('execute_fetchall', select_query)
556-
557-
assert len(results) == 3
558-
# Raw output for the raw request
559-
assert results[0][1] == "example1"
560-
assert results[0][2] == True
561-
assert results[1][1] == "example2"
562-
assert results[1][2] == False
563-
assert results[2][1] == "example3"
564-
assert results[2][2] == True
565-
566-
# # fetch_one()
567-
# if str(database_url).startswith('postgresql'):
568-
# result = await database.raw_api_call('fetchrow', select_query)
569-
# else:
570-
# result = await database.raw_api_call('fetchone', select_query)
571-
# assert result[1] == "example1"
572-
# assert result[2] == True
573-
574-
575519
@pytest.mark.parametrize("database_url", DATABASE_URLS)
576520
@async_adapter
577521
async def test_queries_with_expose_backend_connection(database_url):

0 commit comments

Comments
 (0)