django_evolution.utils.sql
Utilities for working with SQL statements.
Classes
|
Base class for a grouped list of SQL statements. |
|
A list of SQL statements to execute in its own transaction. |
|
A list of SQL statements to execute outside of a transaction. |
|
Management for the execution of SQL. |
- class django_evolution.utils.sql.BaseGroupedSQL(sql)
Bases:
object
Base class for a grouped list of SQL statements.
This is a simple wrapper around a list of SQL statements, used to group statements under some category defined by a subclass.
- class django_evolution.utils.sql.NewTransactionSQL(sql)
Bases:
BaseGroupedSQL
A list of SQL statements to execute in its own transaction.
- class django_evolution.utils.sql.NoTransactionSQL(sql)
Bases:
BaseGroupedSQL
A list of SQL statements to execute outside of a transaction.
- class django_evolution.utils.sql.SQLExecutor(database, check_constraints=True)
Bases:
object
Management for the execution of SQL.
This allows callers to perform raw SQL queries against the database, and to do so with a fine degree of transaction management. Callers can continually add new SQL to execute and, in-between, enter into a new transaction, ensure a previous transaction is already open, or close out any existing transaction.
Through this, it can effectively script a set of transactions and queries in a more loose form than normally allowed by Django.
New in version 2.1.
- __init__(database, check_constraints=True)
Initialize the executor.
- __enter__()
Enter the context manager.
This will prepare internal state for execution, and optionally disable constraint checking (if requested during construction).
The context manager must be entered before operations will work.
- Context:
SQLExecutor
– This instance.
- __exit__(*args, **kwargs)
Exit the context manager.
This will commit any transaction that may be in progress, close the database cursor, and re-enable constraint checking if it were previously disabled.
- new_transaction()
Start a new transaction.
This will commit any prior transaction, if one exists, and then start a new one.
- ensure_transaction()
Ensure a transaction has started.
If no existing transaction has started, this will start a new one.
- finish_transaction()
Finish and commit a transaction.
- run_sql(sql, capture=False, execute=False)
Run (execute and/or capture) a list of SQL statements.
- Parameters:
sql (
list
) – A list of SQL statements. Each entry might be a string, a tuple consisting of a format string and formatting arguments, or a subclass ofBaseGroupedSQL
, or a callable that returns a list of the above.capture (
bool
, optional) – Whether to capture any processed SQL statements.execute (
bool
, optional) – Whether to execute any executed SQL statements and return them.
- Returns:
The list of SQL statements executed, if passing
capture=True
. Otherwise, this will just be an empty list.- Return type:
- Raises:
django.db.transaction.TransactionManagementError – Could not execute a batch of SQL statements inside of an existing transaction.