refactoring SQL get-query for shipcall into a separate utility-section, so it becomes reusable
This commit is contained in:
parent
c1f91a3ef8
commit
20f139ebd9
46
src/server/BreCal/database/sql_queries.py
Normal file
46
src/server/BreCal/database/sql_queries.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
|
||||||
|
def create_sql_query_shipcall_get(options:dict)->str:
|
||||||
|
"""
|
||||||
|
creates an SQL query, which selects all shipcalls from the mysql database.
|
||||||
|
the agency eta times are used to order the entries.
|
||||||
|
|
||||||
|
args:
|
||||||
|
options : dict. A dictionary, which must contains the 'past_days' key (int). Determines the range
|
||||||
|
by which shipcalls are filtered.
|
||||||
|
"""
|
||||||
|
query = ("SELECT s.id as id, ship_id, type, eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, " +
|
||||||
|
"flags, s.pier_side, bunkering, replenishing_terminal, replenishing_lock, draft, tidal_window_from, " +
|
||||||
|
"tidal_window_to, rain_sensitive_cargo, recommended_tugs, anchored, moored_lock, canceled, evaluation, " +
|
||||||
|
"evaluation_message, evaluation_time, evaluation_notifications_sent, s.created as created, s.modified as modified, time_ref_point " +
|
||||||
|
"FROM shipcall s " +
|
||||||
|
"LEFT JOIN times t ON t.shipcall_id = s.id AND t.participant_type = 8 " +
|
||||||
|
"WHERE " +
|
||||||
|
"(type = 1 AND " +
|
||||||
|
"((t.id IS NOT NULL AND t.eta_berth >= DATE(NOW() - INTERVAL %d DAY)) OR " +
|
||||||
|
"(eta >= DATE(NOW() - INTERVAL %d DAY)))) OR " +
|
||||||
|
"((type = 2 OR type = 3) AND " +
|
||||||
|
"((t.id IS NOT NULL AND t.etd_berth >= DATE(NOW() - INTERVAL %d DAY)) OR " +
|
||||||
|
"(etd >= DATE(NOW() - INTERVAL %d DAY)))) " +
|
||||||
|
"ORDER BY eta") % (options["past_days"], options["past_days"], options["past_days"], options["past_days"])
|
||||||
|
|
||||||
|
"""
|
||||||
|
alternatively, f-strings could be used.
|
||||||
|
query_two = ("SELECT s.id as id, ship_id, type, eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, " +
|
||||||
|
"flags, s.pier_side, bunkering, replenishing_terminal, replenishing_lock, draft, tidal_window_from, " +
|
||||||
|
"tidal_window_to, rain_sensitive_cargo, recommended_tugs, anchored, moored_lock, canceled, evaluation, " +
|
||||||
|
"evaluation_message, evaluation_time, evaluation_notifications_sent, s.created as created, s.modified as modified, time_ref_point " +
|
||||||
|
"FROM shipcall s " +
|
||||||
|
"LEFT JOIN times t ON t.shipcall_id = s.id AND t.participant_type = 8 " +
|
||||||
|
"WHERE " +
|
||||||
|
"(type = 1 AND " +
|
||||||
|
f"((t.id IS NOT NULL AND t.eta_berth >= DATE(NOW() - INTERVAL {options['past_days']} DAY)) OR " +
|
||||||
|
f"(eta >= DATE(NOW() - INTERVAL {options['past_days']} DAY)))) OR " +
|
||||||
|
"((type = 2 OR type = 3) AND " +
|
||||||
|
f"((t.id IS NOT NULL AND t.etd_berth >= DATE(NOW() - INTERVAL {options['past_days']} DAY)) OR " +
|
||||||
|
f"(etd >= DATE(NOW() - INTERVAL {options['past_days']} DAY)))) " +
|
||||||
|
"ORDER BY eta")
|
||||||
|
|
||||||
|
assert query==query_two
|
||||||
|
"""
|
||||||
|
return query
|
||||||
Reference in New Issue
Block a user