| 1 |
from django.apps import AppConfig |
| 2 |
from django.db import connections |
| 3 |
|
| 4 |
|
| 5 |
class BaseAppConfig(AppConfig): |
| 6 |
name = "apps.query_performing_app" |
| 7 |
database = "default" |
| 8 |
|
| 9 |
def __init__(self, *args, **kwargs): |
| 10 |
super().__init__(*args, **kwargs) |
| 11 |
self.query_results = [] |
| 12 |
|
| 13 |
def ready(self): |
| 14 |
self.query_results = [] |
| 15 |
self._perform_query() |
| 16 |
|
| 17 |
def _perform_query(self): |
| 18 |
raise NotImplementedError |
| 19 |
|
| 20 |
|
| 21 |
class ModelQueryAppConfig(BaseAppConfig): |
| 22 |
def _perform_query(self): |
| 23 |
from ..models import TotallyNormal |
| 24 |
|
| 25 |
queryset = TotallyNormal.objects.using(self.database) |
| 26 |
queryset.update_or_create(name="new name") |
| 27 |
self.query_results = list(queryset.values_list("name")) |
| 28 |
|
| 29 |
|
| 30 |
class QueryDefaultDatabaseModelAppConfig(ModelQueryAppConfig): |
| 31 |
database = "default" |
| 32 |
|
| 33 |
|
| 34 |
class QueryOtherDatabaseModelAppConfig(ModelQueryAppConfig): |
| 35 |
database = "other" |
| 36 |
|
| 37 |
|
| 38 |
class CursorQueryAppConfig(BaseAppConfig): |
| 39 |
def _perform_query(self): |
| 40 |
connection = connections[self.database] |
| 41 |
with connection.cursor() as cursor: |
| 42 |
cursor.execute("SELECT 42" + connection.features.bare_select_suffix) |
| 43 |
self.query_results = cursor.fetchall() |
| 44 |
|
| 45 |
|
| 46 |
class QueryDefaultDatabaseCursorAppConfig(CursorQueryAppConfig): |
| 47 |
database = "default" |
| 48 |
|
| 49 |
|
| 50 |
class QueryOtherDatabaseCursorAppConfig(CursorQueryAppConfig): |
| 51 |
database = "other" |
| 52 |
|
| 53 |
|
| 54 |
class CursorQueryManyAppConfig(BaseAppConfig): |
| 55 |
def _perform_query(self): |
| 56 |
from ..models import TotallyNormal |
| 57 |
|
| 58 |
connection = connections[self.database] |
| 59 |
table_meta = TotallyNormal._meta |
| 60 |
with connection.cursor() as cursor: |
| 61 |
cursor.executemany( |
| 62 |
"INSERT INTO %s (%s) VALUES(%%s)" |
| 63 |
% ( |
| 64 |
connection.introspection.identifier_converter(table_meta.db_table), |
| 65 |
connection.ops.quote_name(table_meta.get_field("name").column), |
| 66 |
), |
| 67 |
[("test name 1",), ("test name 2",)], |
| 68 |
) |
| 69 |
self.query_results = [] |
| 70 |
|
| 71 |
|
| 72 |
class QueryDefaultDatabaseCursorManyAppConfig(CursorQueryManyAppConfig): |
| 73 |
database = "default" |
| 74 |
|
| 75 |
|
| 76 |
class QueryOtherDatabaseCursorManyAppConfig(CursorQueryManyAppConfig): |
| 77 |
database = "other" |
| 78 |
|
| 79 |
|
| 80 |
class StoredProcedureQueryAppConfig(BaseAppConfig): |
| 81 |
def _perform_query(self): |
| 82 |
with connections[self.database].cursor() as cursor: |
| 83 |
cursor.callproc("test_procedure") |
| 84 |
self.query_results = [] |
| 85 |
|
| 86 |
|
| 87 |
class QueryDefaultDatabaseStoredProcedureAppConfig(StoredProcedureQueryAppConfig): |
| 88 |
database = "default" |
| 89 |
|
| 90 |
|
| 91 |
class QueryOtherDatabaseStoredProcedureAppConfig(StoredProcedureQueryAppConfig): |
| 92 |
database = "other" |