|
1 |
| -# Copyright (c) 2009, 2022, Oracle and/or its affiliates. |
| 1 | +# Copyright (c) 2009, 2023, Oracle and/or its affiliates. |
2 | 2 | #
|
3 | 3 | # This program is free software; you can redistribute it and/or modify
|
4 | 4 | # it under the terms of the GNU General Public License, version 2.0, as
|
@@ -1549,35 +1549,26 @@ def fetchone(self) -> Optional[RowType]:
|
1549 | 1549 | Returns:
|
1550 | 1550 | tuple or None: A row from query result set.
|
1551 | 1551 | """
|
1552 |
| - self._check_executed() |
1553 |
| - row = self._fetch_row() |
1554 |
| - if row: |
1555 |
| - if hasattr(self._connection, "converter"): |
1556 |
| - return self._row_to_python(row, self.description) |
1557 |
| - return row |
1558 |
| - return None |
| 1552 | + row = super().fetchone() |
| 1553 | + if not row: |
| 1554 | + return None |
| 1555 | + return ( |
| 1556 | + self._row_to_python(row, self.description) |
| 1557 | + if hasattr(self._connection, "converter") |
| 1558 | + else row |
| 1559 | + ) |
1559 | 1560 |
|
1560 | 1561 | def fetchall(self) -> List[Optional[RowType]]:
|
1561 | 1562 | """Return all rows of a query result set.
|
1562 | 1563 |
|
1563 | 1564 | Returns:
|
1564 | 1565 | list: A list of tuples with all rows of a query result set.
|
1565 | 1566 | """
|
1566 |
| - self._check_executed() |
1567 |
| - if not self._have_unread_result(): |
1568 |
| - return [] |
1569 |
| - |
1570 |
| - (rows, eof) = self._connection.get_rows() |
1571 |
| - if self._nextrow[0]: |
1572 |
| - rows.insert(0, self._nextrow[0]) |
1573 |
| - res = [self._row_to_python(row, self.description) for row in rows] |
1574 |
| - |
1575 |
| - self._handle_eof(eof) |
1576 |
| - rowcount = len(rows) |
1577 |
| - if rowcount >= 0 and self._rowcount == -1: |
1578 |
| - self._rowcount = 0 |
1579 |
| - self._rowcount += rowcount |
1580 |
| - return res |
| 1567 | + return [ |
| 1568 | + self._row_to_python(row, self.description) |
| 1569 | + for row in super().fetchall() |
| 1570 | + if row |
| 1571 | + ] |
1581 | 1572 |
|
1582 | 1573 |
|
1583 | 1574 | class MySQLCursorBufferedDict(MySQLCursorDict, MySQLCursorBuffered):
|
@@ -1680,3 +1671,88 @@ def fetchmany(
|
1680 | 1671 | for row in super().fetchmany(size=size)
|
1681 | 1672 | if row
|
1682 | 1673 | ]
|
| 1674 | + |
| 1675 | + |
| 1676 | +class MySQLCursorPreparedNamedTuple(MySQLCursorNamedTuple, MySQLCursorPrepared): |
| 1677 | + """ |
| 1678 | + This class is a blend of features from MySQLCursorNamedTuple and MySQLCursorPrepared |
| 1679 | + """ |
| 1680 | + |
| 1681 | + def fetchmany(self, size: Optional[int] = None) -> List[RowType]: |
| 1682 | + """Return the next set of rows of a query result set. |
| 1683 | +
|
| 1684 | + When no more rows are available, it returns an empty list. |
| 1685 | + The number of rows returned can be specified using the size argument, |
| 1686 | + which defaults to one. |
| 1687 | +
|
| 1688 | + Returns: |
| 1689 | + list: The next set of rows of a query result set represented |
| 1690 | + as a list of named tuples where column names are used as names. |
| 1691 | + """ |
| 1692 | + return [ |
| 1693 | + self._row_to_python(row, self.description) |
| 1694 | + for row in super().fetchmany(size=size) |
| 1695 | + if row |
| 1696 | + ] |
| 1697 | + |
| 1698 | + |
| 1699 | +class MySQLCursorPreparedRaw(MySQLCursorPrepared): |
| 1700 | + """ |
| 1701 | + This class is a blend of features from MySQLCursorRaw and MySQLCursorPrepared |
| 1702 | + """ |
| 1703 | + |
| 1704 | + _raw: bool = True |
| 1705 | + |
| 1706 | + def fetchone(self) -> Optional[RowType]: |
| 1707 | + """Return next row of a query result set. |
| 1708 | +
|
| 1709 | + Returns: |
| 1710 | + tuple or None: A row from query result set. |
| 1711 | + """ |
| 1712 | + self._check_executed() |
| 1713 | + if self._cursor_exists: |
| 1714 | + self._connection.cmd_stmt_fetch(self._prepared["statement_id"]) |
| 1715 | + return self._fetch_row(raw=self._raw) or None |
| 1716 | + |
| 1717 | + def fetchmany(self, size: Optional[int] = None) -> List[RowType]: |
| 1718 | + """Return the next set of rows of a query result set. |
| 1719 | +
|
| 1720 | + When no more rows are available, it returns an empty list. |
| 1721 | + The number of rows returned can be specified using the size argument, |
| 1722 | + which defaults to one. |
| 1723 | +
|
| 1724 | + Returns: |
| 1725 | + list: The next set of rows of a query result set. |
| 1726 | + """ |
| 1727 | + self._check_executed() |
| 1728 | + res = [] |
| 1729 | + cnt = size or self.arraysize |
| 1730 | + while cnt > 0 and self._have_unread_result(): |
| 1731 | + cnt -= 1 |
| 1732 | + row = self._fetch_row(raw=self._raw) |
| 1733 | + if row: |
| 1734 | + res.append(row) |
| 1735 | + return res |
| 1736 | + |
| 1737 | + def fetchall(self) -> List[RowType]: |
| 1738 | + """Return all rows of a query result set. |
| 1739 | +
|
| 1740 | + Returns: |
| 1741 | + list: A list of tuples with all rows of a query result set. |
| 1742 | + """ |
| 1743 | + self._check_executed() |
| 1744 | + rows = [] |
| 1745 | + if self._nextrow[0]: |
| 1746 | + rows.append(self._nextrow[0]) |
| 1747 | + while self._have_unread_result(): |
| 1748 | + if self._cursor_exists: |
| 1749 | + self._connection.cmd_stmt_fetch( |
| 1750 | + self._prepared["statement_id"], MAX_RESULTS |
| 1751 | + ) |
| 1752 | + (tmp, eof) = self._connection.get_rows( |
| 1753 | + raw=self._raw, binary=self._binary, columns=self.description |
| 1754 | + ) |
| 1755 | + rows.extend(tmp) |
| 1756 | + self._handle_eof(eof) |
| 1757 | + self._rowcount = len(rows) |
| 1758 | + return rows |
0 commit comments