Run the query against the database and return the result(s). The return value depends on the value of result_type. When result_type is: - MULTI: Retrieves all rows using fetchmany(). Wraps in an iterator for chunked reads when supported. - SINGLE:
(
self, result_type=MULTI, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE
)
| 1592 | return bool(self.execute_sql(SINGLE)) |
| 1593 | |
| 1594 | def execute_sql( |
| 1595 | self, result_type=MULTI, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE |
| 1596 | ): |
| 1597 | """ |
| 1598 | Run the query against the database and return the result(s). The |
| 1599 | return value depends on the value of result_type. |
| 1600 | |
| 1601 | When result_type is: |
| 1602 | - MULTI: Retrieves all rows using fetchmany(). Wraps in an iterator for |
| 1603 | chunked reads when supported. |
| 1604 | - SINGLE: Retrieves a single row using fetchone(). |
| 1605 | - ROW_COUNT: Retrieves the number of rows in the result. |
| 1606 | - CURSOR: Runs the query, and returns the cursor object. It is the |
| 1607 | caller's responsibility to close the cursor. |
| 1608 | """ |
| 1609 | result_type = result_type or NO_RESULTS |
| 1610 | try: |
| 1611 | sql, params = self.as_sql() |
| 1612 | if not sql: |
| 1613 | raise EmptyResultSet |
| 1614 | except EmptyResultSet: |
| 1615 | if result_type == MULTI: |
| 1616 | return iter([]) |
| 1617 | else: |
| 1618 | return |
| 1619 | if chunked_fetch: |
| 1620 | cursor = self.connection.chunked_cursor() |
| 1621 | else: |
| 1622 | cursor = self.connection.cursor() |
| 1623 | try: |
| 1624 | cursor.execute(sql, params) |
| 1625 | except Exception as e: |
| 1626 | # Might fail for server-side cursors (e.g. connection closed) |
| 1627 | try: |
| 1628 | cursor.close() |
| 1629 | except DatabaseError: |
| 1630 | raise e from None |
| 1631 | raise |
| 1632 | |
| 1633 | if result_type == ROW_COUNT: |
| 1634 | try: |
| 1635 | return cursor.rowcount |
| 1636 | finally: |
| 1637 | cursor.close() |
| 1638 | if result_type == CURSOR: |
| 1639 | # Give the caller the cursor to process and close. |
| 1640 | return cursor |
| 1641 | if result_type == SINGLE: |
| 1642 | try: |
| 1643 | val = cursor.fetchone() |
| 1644 | if val: |
| 1645 | return val[0 : self.col_count] |
| 1646 | return val |
| 1647 | finally: |
| 1648 | # done with the cursor |
| 1649 | cursor.close() |
| 1650 | if result_type == NO_RESULTS: |
| 1651 | cursor.close() |