Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Commit 39526be

Browse files
committed
feat: autocommit statements
1 parent b795dd3 commit 39526be

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

sqlalchemy_trino/dialect.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
88
from sqlalchemy.engine.url import URL
99
from trino.auth import BasicAuthentication
10+
from trino.client import TrinoQuery
11+
from trino.dbapi import Cursor
1012

1113
from . import compiler
1214
from . import datatype
@@ -273,6 +275,19 @@ def _get_default_schema_name(self, connection: Connection) -> Optional[str]:
273275
dbapi_connection: trino_dbapi.Connection = connection.connection
274276
return dbapi_connection.schema
275277

278+
def do_execute(self, cursor: Cursor, statement: str, parameters: Tuple[Any, ...],
279+
context: DefaultExecutionContext = None):
280+
cursor.execute(statement, parameters)
281+
if context and context.should_autocommit:
282+
# SQL statement only submitted to Trino server when cursor.fetch*() is called.
283+
# For DDL (CREATE/ALTER/DROP) and DML (INSERT/UPDATE/DELETE) statement, call cursor.description
284+
# to force submit statement immediately.
285+
d = cursor.description
286+
# old trino client does not support eager-loading cursor.description
287+
if d is None:
288+
query: TrinoQuery = cursor._query # noqa
289+
query._result._rows += query.fetch() # noqa
290+
276291
def do_rollback(self, dbapi_connection):
277292
if dbapi_connection.transaction is not None:
278293
dbapi_connection.rollback()

0 commit comments

Comments
 (0)