-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smarter sorting that doesn't keep nesting the sorts #135
Comments
I can do this with a regular expression. I'm going to assume that the column name is double-quoted, since that's the generated SQL I'll be using for order by clauses, so the expression can be quite simple: re.compile(' order by "[^"]+"( desc)?$') |
This looks like it works: In [4]: sql = """select * from (
...: select * from (
...: select id, slug, link_url, link_title, via_url, via_title, commentary, created, metadata, search_document, import_ref, card_image from blog_blogmark
...: ) as results order by "link_title"
...: ) as results order by "link_url" desc"""
In [21]: r = re.compile('(^.*) order by "[^"]+"( desc)?$', re.DOTALL)
In [24]: print(r.match(sql).group(1))
select * from (
select * from (
select id, slug, link_url, link_title, via_url, via_title, commentary, created, metadata, search_document, import_ref, card_image from blog_blogmark
) as results order by "link_title"
) as results |
I'm going to attempt match the regex. If it matches, I'll use the captured group and add the order-by on the end. If it doesn't match, I'll wrap the whole thing in a sub-query and stick the order by on that. |
Here are the tests I wrote for this: django-sql-dashboard/test_project/test_utils.py Lines 22 to 52 in b88350f
|
Currently if you click "sort ascending" and then "sort descending" in the cog menu you get SQL something like this:
Each sort wraps the previous sorted query in another nested query.
We can do better than this! Sorting really complex queries with
with (...)
in them and suchlike is hard, but we can at least notice if the query we are re-sorting ends withorder by X
already and replace that clause rather than wrapping the whole thing.The text was updated successfully, but these errors were encountered: