MCPcopy
hub / github.com/django/django / execute_sql

Method execute_sql

django/db/models/sql/compiler.py:1594–1666  ·  view source on GitHub ↗

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
    )

Source from the content-addressed store, hash-verified

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()

Callers 15

results_iterMethod · 0.95
has_resultsMethod · 0.95
explain_queryMethod · 0.95
checkMethod · 0.45
__iter__Method · 0.45
_raw_deleteMethod · 0.45
updateMethod · 0.45
_updateMethod · 0.45
_insertMethod · 0.45
do_queryMethod · 0.45
update_batchMethod · 0.45

Calls 6

as_sqlMethod · 0.95
cursor_iterFunction · 0.85
cursorMethod · 0.80
chunked_cursorMethod · 0.45
executeMethod · 0.45
closeMethod · 0.45