From 20f139ebd9c1ff4aff4aff42f45e104a909db3eb Mon Sep 17 00:00:00 2001 From: Max Metz Date: Tue, 14 May 2024 12:19:25 +0200 Subject: [PATCH] refactoring SQL get-query for shipcall into a separate utility-section, so it becomes reusable --- src/server/BreCal/database/sql_queries.py | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/server/BreCal/database/sql_queries.py diff --git a/src/server/BreCal/database/sql_queries.py b/src/server/BreCal/database/sql_queries.py new file mode 100644 index 0000000..551aa91 --- /dev/null +++ b/src/server/BreCal/database/sql_queries.py @@ -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