Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions redash/query_runner/cass.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def generate_ssl_options_dict(protocol, cert_path=None):
return ssl_options


def custom_json_encoder(dec, o):
if isinstance(o, sortedset):
return list(o)
return None


class Cassandra(BaseQueryRunner):
noop_query = "SELECT dateof(now()) FROM system.local"

@classmethod
def enabled(cls):
return enabled

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, sortedset):
return list(o)
return None

@classmethod
def configuration_schema(cls):
return {
Expand Down
20 changes: 10 additions & 10 deletions redash/query_runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@
}


def custom_json_encoder(dec, o):
if isinstance(o, ObjectId):
return str(o)
elif isinstance(o, Timestamp):
return dec.default(o.as_datetime())
elif isinstance(o, Decimal128):
return o.to_decimal()
return None


date_regex = re.compile(r'ISODate\("(.*)"\)', re.IGNORECASE)


Expand Down Expand Up @@ -170,6 +160,16 @@ def __init__(self, configuration):
True if "replicaSetName" in self.configuration and self.configuration["replicaSetName"] else False
)

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, ObjectId):
return str(o)
elif isinstance(o, Timestamp):
return dec.default(o.as_datetime())
elif isinstance(o, Decimal128):
return o.to_decimal()
return None

def _get_db(self):
kwargs = {}
if self.is_replica_set:
Expand Down
24 changes: 12 additions & 12 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@
}


def custom_json_encoder(dec, o):
if isinstance(o, Range):
# From: https://github.com/psycopg/psycopg2/pull/779
if o._bounds is None:
return ""

items = [o._bounds[0], str(o._lower), ", ", str(o._upper), o._bounds[1]]

return "".join(items)
return None


def _wait(conn, timeout=None):
while 1:
try:
Expand Down Expand Up @@ -195,6 +183,18 @@ def configuration_schema(cls):
def type(cls):
return "pg"

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, Range):
# From: https://github.com/psycopg/psycopg2/pull/779
if o._bounds is None:
return ""

items = [o._bounds[0], str(o._lower), ", ", str(o._upper), o._bounds[1]]

return "".join(items)
return None

def _get_definitions(self, schema, query):
results, error = self.run_query(query, None)

Expand Down
4 changes: 3 additions & 1 deletion redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class JSONEncoder(json.JSONEncoder):
"""Adapter for `json.dumps`."""

def __init__(self, **kwargs):
self.encoders = [m.custom_json_encoder for m in sys.modules.values() if hasattr(m, "custom_json_encoder")]
from redash.query_runner import query_runners

self.encoders = [r.custom_json_encoder for r in query_runners.values() if hasattr(r, "custom_json_encoder")]
Comment on lines +77 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, this change is much smaller and more readable than previously: only the essential was done to fix the issue at hand. We can move things away from the __init__.py files in a later PR, but for now we need to fix the CI, so we shouldn't do any refactor changes if they're not necessary.

super().__init__(**kwargs)

def default(self, o):
Expand Down