Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
wip - very small formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Aug 23, 2018
1 parent eae578b commit 8bc4e84
Showing 1 changed file with 39 additions and 46 deletions.
85 changes: 39 additions & 46 deletions backdrop/core/storage/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,48 +165,48 @@ def delete_record(self, data_set_id, record_id):
self.connection.commit()

def execute_query(self, data_set_id, query):
if query.is_grouped:
with self.connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as psql_cursor:
collect_lookup = self._get_collect_lookup(query)
groups_lookup = self._get_groups_lookup(query)

pg_query = self._get_grouped_postgres_query(
data_set_id,
query,
collect_lookup,
groups_lookup
)

if not query.is_grouped:
with self.connection.cursor() as psql_cursor:
pg_query = self._get_basic_postgres_query(data_set_id, query)
psql_cursor.execute(pg_query)
records = psql_cursor.fetchall()
return [parse_datetime_fields(record) for (record,) in records]

for record in records:
keys_to_remove = []
pairs_to_add = []
with self.connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as psql_cursor:
collect_lookup = self._get_collect_lookup(query)
groups_lookup = self._get_groups_lookup(query)

aggregated_pairs = {}
aggregated_pairs.update(collect_lookup)
aggregated_pairs.update(groups_lookup)
pg_query = self._get_grouped_postgres_query(
data_set_id,
query,
collect_lookup,
groups_lookup
)

for key, value in record.iteritems():
if isinstance(value, datetime):
record[key] = value.replace(tzinfo=pytz.UTC)
if key in aggregated_pairs:
pairs_to_add.append(tuple([aggregated_pairs[key], value]))
keys_to_remove.append(key)
psql_cursor.execute(pg_query)
records = psql_cursor.fetchall()

for key in keys_to_remove:
del record[key]
for key, value in pairs_to_add:
record[key] = value
for record in records:
keys_to_remove = []
pairs_to_add = []

return records
else:
with self.connection.cursor() as psql_cursor:
pg_query = self._get_basic_postgres_query(data_set_id, query)
psql_cursor.execute(pg_query)
records = psql_cursor.fetchall()
return [parse_datetime_fields(record) for (record,) in records]
aggregated_pairs = {}
aggregated_pairs.update(collect_lookup)
aggregated_pairs.update(groups_lookup)

for key, value in record.iteritems():
if isinstance(value, datetime):
record[key] = value.replace(tzinfo=pytz.UTC)
if key in aggregated_pairs:
pairs_to_add.append(tuple([aggregated_pairs[key], value]))
keys_to_remove.append(key)

for key in keys_to_remove:
del record[key]
for key, value in pairs_to_add:
record[key] = value

return records

def _get_grouped_postgres_query(self, data_set_id, query, collect_lookup, groups_lookup):
with self.connection.cursor() as psql_cursor:
Expand Down Expand Up @@ -273,13 +273,10 @@ def _get_groups_lookup(self, query):
>>> _get_groups_lookup(Query.create(group_by=['foo','bar']))
{'group_0': 'foo', 'group_1': 'bar'}
"""
if query.group_by:
return {
'group_{index}'.format(index=i): group_by
for i, group_by in enumerate(query.group_by)
}
else:
return {}
return {
'group_{index}'.format(index=i): group_by
for i, group_by in enumerate(query.group_by or [])
}

def _get_groups(self, psql_cursor, groups_lookup):
return {
Expand Down Expand Up @@ -356,10 +353,6 @@ def _get_time_limit_conditions(self, query):
Converts a query into a list of conditions to be concatenated into a where query
These conditions are only based on start_at and end_at
"""

if not query:
return []

clauses = []

if query.start_at:
Expand Down

0 comments on commit 8bc4e84

Please sign in to comment.