Further cleanups.

This commit is contained in:
Timothy Allen 2018-08-14 11:04:42 +02:00
parent d200af238c
commit ddd3f64fa3

View File

@ -86,6 +86,7 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(),
if show == -1: if show == -1:
limit = '' limit = ''
''' Build standard query (list of results) '''
if event: if event:
if isinstance(event, str): if isinstance(event, str):
event = db.escape_string(event).decode() event = db.escape_string(event).decode()
@ -98,19 +99,8 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(),
if isinstance(licence, str): if isinstance(licence, str):
licence = db.escape_string(licence).decode() licence = db.escape_string(licence).decode()
where += ' AND licence LIKE "{}"'.format(licence.lower()) where += ' AND licence LIKE "{}"'.format(licence.lower())
if year:
if not isinstance(year, (int, float)):
year = int(db.escape_string(year).decode())
firstdate = dt.datetime.min
lastdate = dt.datetime.max
firstdate = firstdate.replace(year=int(year))
lastdate = lastdate.replace(year=int(year))
where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate)
''' This statement is expensive but doesn't increase the count, so don't change the count statement '''
if finishers:
select = 'total.finishers, query.* FROM( SELECT *'
close = ') AS query INNER JOIN (SELECT event, date, distance, COUNT(event) as finishers FROM `results` GROUP BY event, distance, date) AS total ON total.event=query.event AND total.date=query.date AND total.distance=query.distance'
''' Build search query '''
for column in search.keys(): for column in search.keys():
if isinstance(column, str): if isinstance(column, str):
column = db.escape_string(column).decode() column = db.escape_string(column).decode()
@ -143,31 +133,43 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(),
else: else:
pass pass
''' Build list query (list of races, rankings, licences, or runners by distance) '''
if listing: if listing:
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no%return%"'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no%card%"'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank%card%"'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"'
if listing == 'races': if listing == 'races':
select = 'event, date' select = 'TRIM(event), date'
group = 'GROUP BY event, date' group = 'GROUP BY event, date'
order = 'date DESC, event' order = 'date DESC, TRIM(event)'
elif listing == 'runners': elif listing == 'runners':
select = 'CONCAT_WS(" ", name, surname) person, FORMAT(SUM(distance),0) total' select = 'TRIM(CONCAT_WS(" ", name, surname)) AS person, FORMAT(SUM(distance),0) AS total'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no return%"' group = 'GROUP BY TRIM(CONCAT_WS(" ", name, surname))'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no card%"' order = 'SUM(distance) DESC, TRIM(CONCAT_WS(" ", name, surname))'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank card%"'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"'
group = 'GROUP BY CONCAT_WS(" ", name, surname)'
order = 'SUM(distance) DESC, CONCAT_WS(" ", name, surname)'
elif listing == 'rankings': elif listing == 'rankings':
select = 'CONCAT_WS(" ", name, surname) person, SUM(position) positions, COUNT(event) races, SUM(position)/COUNT(event) podiums, FORMAT(SUM(position)/COUNT(event),1) score' # SELECT query.person, query.positions, query.races, query.podiums, query.score, sex.positions AS sexpositions, sex.races AS sexraces, cat.positions AS catpositions, cat.races catraces FROM (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(position) positions, COUNT(event) races, SUM(position)/COUNT(event) podiums, FORMAT(SUM(position)/COUNT(event),1) score FROM `results` WHERE club LIKE "AAC" GROUP BY CONCAT_WS(" ", name, surname) ) AS query INNER JOIN (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(sexposition) as positions, COUNT(event) races FROM `results` WHERE club LIKE "AAC" AND sexposition > 0 GROUP BY CONCAT_WS(" ", name, surname) ) sex ON query.person=sex.person INNER JOIN (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(catposition) as positions, COUNT(event) races FROM `results` WHERE club LIKE "AAC" AND catposition > 0 GROUP BY CONCAT_WS(" ", name, surname) ) cat ON query.person=cat.person WHERE query.person NOT LIKE "%no return%" AND query.person NOT LIKE "%no card%" AND query.person NOT LIKE "%blank card%" AND query.person NOT LIKE "%disqualified%" GROUP BY query.person ORDER BY podiums, races DESC;
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no return%"' select = 'TRIM(CONCAT_WS(" ", name, surname)) AS person, SUM(position) AS positions, COUNT(event) AS races, SUM(position)/COUNT(event) AS podiums, FORMAT(SUM(position)/COUNT(event), 1) AS score'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no card%"' group = 'GROUP BY TRIM(CONCAT_WS(" ", name, surname))'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank card%"'
where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"'
group = 'GROUP BY CONCAT_WS(" ", name, surname)'
order = 'podiums, races DESC' order = 'podiums, races DESC'
elif listing == 'licence': elif listing == 'licence':
select = 'licence, date, CONCAT_WS(" ", name, surname) person' select = 'licence, date, TRIM(CONCAT_WS(" ", name, surname)) AS person'
group = 'GROUP BY licence, CONCAT_WS(" ", name, surname)' group = 'GROUP BY licence, name, surname'
order = 'CONCAT_WS(" ", name, surname), date DESC' order = 'TRIM(CONCAT_WS(" ", name, surname)) ASC'
''' Add elements common to multiple types of queries '''
if year:
if not isinstance(year, (int, float)):
year = int(db.escape_string(year).decode())
firstdate = dt.datetime.min
lastdate = dt.datetime.max
firstdate = firstdate.replace(year=int(year))
lastdate = lastdate.replace(year=int(year))
where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate)
''' This statement is expensive but doesn't increase the count, so don't change the count statement '''
if finishers:
select = 'total.finishers, query.* FROM( SELECT *'
close = ') AS query INNER JOIN (SELECT event, date, distance, COUNT(event) as finishers FROM `results` GROUP BY event, distance, date) AS total ON total.event=query.event AND total.date=query.date AND total.distance=query.distance'
sql = 'SELECT {} FROM `results` {} {} ORDER BY {} {} {};'.format(select, where, group, order, limit, close) sql = 'SELECT {} FROM `results` {} {} ORDER BY {} {} {};'.format(select, where, group, order, limit, close)