Call fetch methods from a DBAPI cursor. Alternate versions of this class may instead buffer the rows from cursors or not use cursors at all.
| 1235 | |
| 1236 | |
| 1237 | class CursorFetchStrategy(ResultFetchStrategy): |
| 1238 | """Call fetch methods from a DBAPI cursor. |
| 1239 | |
| 1240 | Alternate versions of this class may instead buffer the rows from |
| 1241 | cursors or not use cursors at all. |
| 1242 | |
| 1243 | """ |
| 1244 | |
| 1245 | __slots__ = () |
| 1246 | |
| 1247 | def soft_close( |
| 1248 | self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor] |
| 1249 | ) -> None: |
| 1250 | result.cursor_strategy = _NO_CURSOR_DQL |
| 1251 | |
| 1252 | def hard_close( |
| 1253 | self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor] |
| 1254 | ) -> None: |
| 1255 | result.cursor_strategy = _NO_CURSOR_DQL |
| 1256 | |
| 1257 | def handle_exception( |
| 1258 | self, |
| 1259 | result: CursorResult[Any], |
| 1260 | dbapi_cursor: Optional[DBAPICursor], |
| 1261 | err: BaseException, |
| 1262 | ) -> NoReturn: |
| 1263 | result.connection._handle_dbapi_exception( |
| 1264 | err, None, None, dbapi_cursor, result.context |
| 1265 | ) |
| 1266 | |
| 1267 | def yield_per( |
| 1268 | self, result: CursorResult[Any], dbapi_cursor: DBAPICursor, num: int |
| 1269 | ) -> None: |
| 1270 | result.cursor_strategy = BufferedRowCursorFetchStrategy( |
| 1271 | dbapi_cursor, |
| 1272 | {"max_row_buffer": num}, |
| 1273 | initial_buffer=collections.deque(), |
| 1274 | growth_factor=0, |
| 1275 | ) |
| 1276 | |
| 1277 | def fetchone( |
| 1278 | self, |
| 1279 | result: CursorResult[Any], |
| 1280 | dbapi_cursor: DBAPICursor, |
| 1281 | hard_close: bool = False, |
| 1282 | ) -> Any: |
| 1283 | try: |
| 1284 | row = dbapi_cursor.fetchone() |
| 1285 | if row is None: |
| 1286 | result._soft_close(hard=hard_close) |
| 1287 | return row |
| 1288 | except BaseException as e: |
| 1289 | self.handle_exception(result, dbapi_cursor, e) |
| 1290 | |
| 1291 | def fetchmany( |
| 1292 | self, |
| 1293 | result: CursorResult[Any], |
| 1294 | dbapi_cursor: DBAPICursor, |