-
Notifications
You must be signed in to change notification settings - Fork 18
fix: make database function compatible with postgresql and mysql both #260
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
Changes from 4 commits
c594632
bc7a493
1f13467
48194a0
6042f4e
fe0da59
71698c3
4731b8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,16 @@ | ||||||||||||||||
| """ | ||||||||||||||||
| Utilities for tagging and taxonomy models | ||||||||||||||||
| """ | ||||||||||||||||
| from django.db import connection as db_connection | ||||||||||||||||
| from django.db.models import Aggregate, CharField | ||||||||||||||||
| from django.db.models.expressions import Func | ||||||||||||||||
| from django.db.models.expressions import Combinable, Func | ||||||||||||||||
|
|
||||||||||||||||
| RESERVED_TAG_CHARS = [ | ||||||||||||||||
| '\t', # Used in the database to separate tag levels in the "lineage" field | ||||||||||||||||
| # e.g. lineage="Earth\tNorth America\tMexico\tMexico City" | ||||||||||||||||
| ' > ', # Used in the search index and Instantsearch frontend to separate tag levels | ||||||||||||||||
| # e.g. tags_level3="Earth > North America > Mexico > Mexico City" | ||||||||||||||||
| ';', # Used in CSV exports to separate multiple tags from the same taxonomy | ||||||||||||||||
| ';', # Used in CSV exports to separate multiple tags from the same taxonomy… | ||||||||||||||||
| # e.g. languages-v1: en;es;fr | ||||||||||||||||
| ] | ||||||||||||||||
| TAGS_CSV_SEPARATOR = RESERVED_TAG_CHARS[2] | ||||||||||||||||
|
|
@@ -34,21 +35,56 @@ def as_sqlite(self, compiler, connection, **extra_context): | |||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| class StringAgg(Aggregate): # pylint: disable=abstract-method | ||||||||||||||||
| class StringAgg(Aggregate, Combinable): | ||||||||||||||||
| """ | ||||||||||||||||
| Aggregate function that collects the values of some column across all rows, | ||||||||||||||||
| and creates a string by concatenating those values, with "," as a separator. | ||||||||||||||||
| and creates a string by concatenating those values, with a specified separator. | ||||||||||||||||
|
|
||||||||||||||||
| This is the same as Django's django.contrib.postgres.aggregates.StringAgg, | ||||||||||||||||
| but this version works with MySQL and SQLite. | ||||||||||||||||
| This version supports PostgreSQL (STRING_AGG), MySQL (GROUP_CONCAT), and SQLite. | ||||||||||||||||
| """ | ||||||||||||||||
| # Default function is for MySQL (GROUP_CONCAT) | ||||||||||||||||
| function = 'GROUP_CONCAT' | ||||||||||||||||
| template = '%(function)s(%(distinct)s%(expressions)s)' | ||||||||||||||||
|
|
||||||||||||||||
| def __init__(self, expression, distinct=False, **extra): | ||||||||||||||||
| def __init__(self, expression, distinct=False, delimiter=',', **extra): | ||||||||||||||||
| self.delimiter = delimiter | ||||||||||||||||
bradenmacdonald marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| # Handle the distinct option and output type | ||||||||||||||||
| distinct_str = 'DISTINCT ' if distinct else '' | ||||||||||||||||
|
|
||||||||||||||||
| extra.update({ | ||||||||||||||||
| 'distinct': distinct_str, | ||||||||||||||||
| 'output_field': CharField(), | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| # Check the database backend (PostgreSQL, MySQL, or SQLite) | ||||||||||||||||
| if 'postgresql' in db_connection.vendor.lower(): | ||||||||||||||||
pomegranited marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| self.function = 'STRING_AGG' | ||||||||||||||||
| self.template = '%(function)s(%(distinct)s%(expressions)s, %(delimiter)s)' | ||||||||||||||||
| extra.update({"delimiter": delimiter}) | ||||||||||||||||
|
||||||||||||||||
| extra.update({"delimiter": delimiter}) | |
| extra.update({"delimiter": ","}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And is it possible to avoid overriding as_sql() below by providing the same output_field that postgres StringAgg does?
| extra.update({"delimiter": delimiter}) | |
| extra.update({ | |
| "delimiter": ",", | |
| "output_field": TextField(), | |
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a great idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pomegranited I just updated the PR as per your recommendations, thank you for the great suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this ellipsis character was added here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated