Last active
December 5, 2017 20:17
-
-
Save snelis/ff64582516655ff6677f7f354eb78423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from http://stackoverflow.com/questions/11476664/easy-way-to-run-explain-on-query-sets-in-django | |
from django.db import connections | |
from django.db.models.query import QuerySet | |
def explain(qs): | |
cursor = connections[qs.db].cursor() | |
query, params = qs.query.sql_with_params() | |
cursor.execute('explain %s' % query, params) | |
print(qs.query) | |
print('\n'.join(r[0] for r in cursor.fetchall())) | |
class ObjectAtPath(Func): | |
function = '#>' | |
template = "%(expressions)s%(function)s'{%(path)s}'" | |
arity = 1 | |
def __init__(self, expression, path, **extra): | |
# if path is a list, convert it to a comma separated string | |
if isinstance(path, (list, tuple)): | |
path = ','.join(path) | |
super().__init__(expression, path=path, **extra) | |
class JsonbArrayElements(Func): | |
function = 'jsonb_array_elements' | |
template = '%(function)s(%(expressions)s)' | |
arity = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment