@@ -122,10 +122,12 @@ def __init__( # noqa: D107
122122 async def _execute (
123123 self ,
124124 query : typing .Union [ClauseElement , str ],
125- params : typing .Optional [typing .Union [
126- typing .Dict [str , typing .Any ],
127- typing .List [typing .Any ],
128- ]] = None ,
125+ params : typing .Optional [
126+ typing .Union [
127+ typing .Dict [str , typing .Any ],
128+ typing .List [typing .Any ],
129+ ]
130+ ] = None ,
129131 ) -> typing .Any : # noqa: ANN401
130132 """Execute the provided query and return a corresponding Cursor object.
131133
@@ -145,34 +147,17 @@ async def _execute(
145147 """
146148 return await self ._conn .execute (query , params )
147149
148- async def _execute_within_transaction (
149- self ,
150- query : typing .Union [ClauseElement , str ],
151- params : typing .Optional [typing .Union [
152- typing .Dict [str , typing .Any ],
153- typing .List [typing .Any ],
154- ]] = None ,
155- ) -> typing .Any : # noqa: ANN401
156- await self .create_transaction ()
157-
158- try :
159- cursor = await self ._conn .execute (query , params )
160- except Exception :
161- await self .cancel_transaction ()
162- raise
163- else :
164- await self .commit_transaction ()
165-
166- return cursor
167-
168150 def _compile_query (
169- self , query : ClauseElement ,
151+ self ,
152+ query : ClauseElement ,
170153 ) -> typing .Tuple [
171154 str ,
172- typing .Optional [typing .Union [
173- typing .Dict [str , typing .Any ],
174- typing .List [typing .Any ],
175- ]],
155+ typing .Optional [
156+ typing .Union [
157+ typing .Dict [str , typing .Any ],
158+ typing .List [typing .Any ],
159+ ]
160+ ],
176161 ]:
177162 compiled_query = query .compile (
178163 dialect = self ._dialect ,
@@ -182,7 +167,9 @@ def _compile_query(
182167 return str (compiled_query ), compiled_query .params
183168
184169 def _cast_row (
185- self , cursor : typing .Any , row : typing .Any , # noqa: ANN401
170+ self ,
171+ cursor : typing .Any , # noqa: ANN401
172+ row : typing .Any , # noqa: ANN401
186173 ) -> typing .Dict [str , typing .Any ]:
187174 """Cast a driver specific Row object to a more general mapping."""
188175 fields = [column [0 ] for column in cursor .description ]
@@ -191,10 +178,12 @@ def _cast_row(
191178 async def execute (
192179 self ,
193180 query : typing .Union [ClauseElement , str ],
194- params : typing .Optional [typing .Union [
195- typing .Dict [str , typing .Any ],
196- typing .List [typing .Any ],
197- ]] = None ,
181+ params : typing .Optional [
182+ typing .Union [
183+ typing .Dict [str , typing .Any ],
184+ typing .List [typing .Any ],
185+ ]
186+ ] = None ,
198187 ) -> None :
199188 """Execute the provided query.
200189
@@ -207,15 +196,17 @@ async def execute(
207196 """
208197 if isinstance (query , ClauseElement ):
209198 query , params = self ._compile_query (query )
210- await self ._execute_within_transaction (query , params )
199+ await self ._execute (query , params )
211200
212201 async def fetch_one (
213202 self ,
214203 query : typing .Union [ClauseElement , str ],
215- params : typing .Optional [typing .Union [
216- typing .Dict [str , typing .Any ],
217- typing .List [typing .Any ],
218- ]] = None ,
204+ params : typing .Optional [
205+ typing .Union [
206+ typing .Dict [str , typing .Any ],
207+ typing .List [typing .Any ],
208+ ]
209+ ] = None ,
219210 ) -> typing .Optional [typing .Dict [str , typing .Any ]]:
220211 """Execute the provided query.
221212
@@ -234,19 +225,23 @@ async def fetch_one(
234225 if isinstance (query , ClauseElement ):
235226 query , params = self ._compile_query (query )
236227
237- cursor = await self ._execute_within_transaction (query , params )
228+ cursor = await self ._execute (query , params )
238229 row = await cursor .fetchone ()
239230 if not row :
240231 return None
241- return self ._cast_row (cursor , row )
232+ row = self ._cast_row (cursor , row )
233+ await cursor .close ()
234+ return row
242235
243236 async def fetch_all (
244237 self ,
245238 query : typing .Union [ClauseElement , str ],
246- params : typing .Optional [typing .Union [
247- typing .Dict [str , typing .Any ],
248- typing .List [typing .Any ],
249- ]] = None ,
239+ params : typing .Optional [
240+ typing .Union [
241+ typing .Dict [str , typing .Any ],
242+ typing .List [typing .Any ],
243+ ]
244+ ] = None ,
250245 ) -> typing .List [typing .Dict [str , typing .Any ]]:
251246 """Execute the provided query.
252247
@@ -264,9 +259,11 @@ async def fetch_all(
264259 if isinstance (query , ClauseElement ):
265260 query , params = self ._compile_query (query )
266261
267- cursor = await self ._execute_within_transaction (query , params )
262+ cursor = await self ._execute (query , params )
268263 rows = await cursor .fetchall ()
269- return [self ._cast_row (cursor , row ) for row in rows ]
264+ rows = [self ._cast_row (cursor , row ) for row in rows ]
265+ await cursor .close ()
266+ return rows
270267
271268 async def create_transaction (self ) -> None :
272269 """Create a transaction and add it to the transaction stack."""
0 commit comments