From 8887d41c2e6dd884c15766510e4ca9fd3dddfc3a Mon Sep 17 00:00:00 2001 From: Robin Michael Date: Tue, 30 Jan 2024 20:03:50 -0600 Subject: [PATCH] Correct linkage joins and uniqueness --- data_messages/Promotions.py | 18 +++++++++--------- data_messages/RateModifications.py | 20 ++++++++++---------- price_calculator/ExtraGuestCharges.py | 4 ++-- price_calculator/Promotions.py | 12 ++++++------ price_calculator/RateModifiers.py | 10 +++++----- price_calculator/Rates.py | 4 ++-- price_calculator/TaxesOrFees.py | 12 ++++++------ 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/data_messages/Promotions.py b/data_messages/Promotions.py index 85b62cf..5cdcd58 100644 --- a/data_messages/Promotions.py +++ b/data_messages/Promotions.py @@ -62,7 +62,7 @@ def create_tables(dsn: str): parent_id int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES Promotion(id) ON DELETE CASCADE, - UNIQUE(external_id, promotion_id, file_id, start, end, days_of_week)) + UNIQUE(external_id, file_id, parent_id, start, end, days_of_week)) """) commands.execute(f""" create table if not exists Promotion_CheckinDates ( @@ -75,7 +75,7 @@ def create_tables(dsn: str): parent_id int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES Promotion(id) ON DELETE CASCADE, - UNIQUE(external_id, promotion_id, file_id, start, end, days_of_week)) + UNIQUE(external_id, file_id, parent_id, start, end, days_of_week)) """) commands.execute(f""" create table if not exists Promotion_CheckoutDates ( @@ -88,7 +88,7 @@ def create_tables(dsn: str): parent_id int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES Promotion(id) ON DELETE CASCADE, - UNIQUE(external_id, promotion_id, file_id, start, end, days_of_week)) + UNIQUE(external_id, file_id, parent_id, start, end, days_of_week)) """) commands.execute(f""" create table if not exists Promotion_LengthOfStay ( @@ -100,7 +100,7 @@ def create_tables(dsn: str): parent_id int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES Promotion(id) ON DELETE CASCADE, - UNIQUE(external_id, promotion_id, file_id)) + UNIQUE(external_id, file_id, parent_id, min, max)) """) commands.execute(f""" create table if not exists Promotion_BookingWindow @@ -112,7 +112,7 @@ def create_tables(dsn: str): max int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES Promotion(id) ON DELETE CASCADE, - PRIMARY KEY(external_id, file_id, min, max) + PRIMARY KEY(external_id, file_id, parent_id, min, max) )""") @@ -198,7 +198,7 @@ def load_promotions(promotions: list[Promotion], file_info: FileInfo.FileInfo, ?end?, ?days_of_week? ) - ON CONFLICT (external_id, promotion_id, file_id, start, end, days_of_week) + ON CONFLICT (external_id, promotion_id, file_id, parent_id, start, end, days_of_week) DO NOTHING """, param=[{ @@ -231,7 +231,7 @@ def load_promotions(promotions: list[Promotion], file_info: FileInfo.FileInfo, ?end?, ?days_of_week? ) - ON CONFLICT (external_id, promotion_id, file_id, start, end, days_of_week) + ON CONFLICT (external_id, promotion_id, file_id, parent_id, start, end, days_of_week) DO NOTHING """, param=[{ @@ -264,7 +264,7 @@ def load_promotions(promotions: list[Promotion], file_info: FileInfo.FileInfo, ?end?, ?days_of_week? ) - ON CONFLICT (external_id, promotion_id, file_id, start, end, days_of_week) + ON CONFLICT (external_id, promotion_id, file_id, parent_id, start, end, days_of_week) DO NOTHING """, param=[{ @@ -294,7 +294,7 @@ def load_promotions(promotions: list[Promotion], file_info: FileInfo.FileInfo, ?parent_id?, ?min?, ?max? - ) ON CONFLICT (external_id, promotion_id, file_id) + ) ON CONFLICT (external_id, promotion_id, file_id, parent_id, min, max) DO NOTHING """, param={ diff --git a/data_messages/RateModifications.py b/data_messages/RateModifications.py index ea44a79..a69bbb5 100644 --- a/data_messages/RateModifications.py +++ b/data_messages/RateModifications.py @@ -58,7 +58,7 @@ def create_tables(dsn: str): days_of_week TEXT, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES RateModifications(id) ON DELETE CASCADE, - UNIQUE(external_id, file_id, start, end, days_of_week) + UNIQUE(external_id, file_id, parent_id, start, end, days_of_week) )""") commands.execute(f""" create table if not exists RateModifications_CheckinDates @@ -71,7 +71,7 @@ def create_tables(dsn: str): days_of_week TEXT, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES RateModifications(id) ON DELETE CASCADE, - PRIMARY KEY(external_id, file_id, start, end, days_of_week) + PRIMARY KEY(external_id, file_id, parent_id, start, end, days_of_week) )""") commands.execute(f""" create table if not exists RateModifications_CheckoutDates @@ -84,7 +84,7 @@ def create_tables(dsn: str): days_of_week TEXT, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES RateModifications(id) ON DELETE CASCADE, - PRIMARY KEY(external_id, file_id, start, end, days_of_week) + PRIMARY KEY(external_id, file_id, parent_id, start, end, days_of_week) )""") commands.execute(f""" create table if not exists RateModifications_LengthOfStay @@ -96,7 +96,7 @@ def create_tables(dsn: str): max int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES RateModifications(id) ON DELETE CASCADE, - PRIMARY KEY(external_id, file_id, min, max) + PRIMARY KEY(external_id, file_id, parent_id, min, max) )""") commands.execute(f""" create table if not exists RateModifications_BookingWindow @@ -108,7 +108,7 @@ def create_tables(dsn: str): max int, FOREIGN KEY (file_id) REFERENCES FileInfo(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES RateModifications(id) ON DELETE CASCADE, - PRIMARY KEY(external_id, file_id, min, max) + PRIMARY KEY(external_id, file_id, parent_id, min, max) )""") @@ -180,7 +180,7 @@ def load_rate_modifications(rate_modifiers: list[RateModifications], ?start?, ?end?, ?days_of_week? - ) ON CONFLICT (external_id, file_id, start, end, days_of_week) DO NOTHING + ) ON CONFLICT (external_id, file_id, parent_id, start, end, days_of_week) DO NOTHING """, param=[{ "external_id": rate_modifier.external_id, @@ -209,7 +209,7 @@ def load_rate_modifications(rate_modifiers: list[RateModifications], ?start?, ?end?, ?days_of_week? - ) ON CONFLICT (external_id, file_id, start, end, days_of_week) + ) ON CONFLICT (external_id, file_id, parent_id, start, end, days_of_week) DO NOTHING """, param=[{ @@ -239,7 +239,7 @@ def load_rate_modifications(rate_modifiers: list[RateModifications], ?start?, ?end?, ?days_of_week? - ) ON CONFLICT (external_id, file_id, start, end, days_of_week) + ) ON CONFLICT (external_id, file_id, parent_id, start, end, days_of_week) DO NOTHING""", param=[{ "external_id": rate_modifier.external_id, @@ -266,7 +266,7 @@ def load_rate_modifications(rate_modifiers: list[RateModifications], ?parent_id?, ?min?, ?max? - ) ON CONFLICT (external_id, file_id, min, max) DO NOTHING""", + ) ON CONFLICT (external_id, file_id, parent_id, min, max) DO NOTHING""", param={ "external_id": rate_modifier.external_id, "file_id": new_id, @@ -291,7 +291,7 @@ def load_rate_modifications(rate_modifiers: list[RateModifications], ?parent_id?, ?min?, ?max? - ) ON CONFLICT (external_id, file_id, min, max) DO NOTHING""", + ) ON CONFLICT (external_id, file_id, parent_id, min, max) DO NOTHING""", param={ "external_id": rate_modifier.external_id, "file_id": new_id, diff --git a/price_calculator/ExtraGuestCharges.py b/price_calculator/ExtraGuestCharges.py index edaf617..09433c5 100644 --- a/price_calculator/ExtraGuestCharges.py +++ b/price_calculator/ExtraGuestCharges.py @@ -22,9 +22,9 @@ def get_extra_guest_charges(external_id: str, from ExtraGuestCharges e left join ExtraGuestCharges_StayDates esd on e.id = esd.parent_id + where e.external_id = ?external_id? and ?start_date? between COALESCE(esd.start, DATE(?start_date?, '-1 day')) and COALESCE(esd.end, DATE(?start_date?, '+1 day')) - and ?end_date? between COALESCE(esd.end, DATE(?end_date?, '-1 day')) and COALESCE(esd.end, DATE(?end_date?, '+1 day')) - where e.external_id = ?external_id? + and ?end_date? between COALESCE(esd.end, DATE(?end_date?, '-1 day')) and COALESCE(esd.end, DATE(?end_date?, '+1 day')) """, param={ "external_id": external_id, diff --git a/price_calculator/Promotions.py b/price_calculator/Promotions.py index 35d7ab2..e69f26a 100644 --- a/price_calculator/Promotions.py +++ b/price_calculator/Promotions.py @@ -29,17 +29,17 @@ def get_promotions(external_id: str, left join Promotion_BookingWindow pbw on p.id = pbw.parent_id left join Promotion_BookingDates pbd - on p.id = pbd.parent_id - and ?book_date? between COALESCE(pbd.start, DATE(?book_date?, '-1 day')) and COALESCE(pbd.end, DATE(?book_date?, '+1 day')) + on p.id = pbd.parent_id left join Promotion_CheckinDates pcid - on p.id = pcid.parent_id - and ?start_date? between COALESCE(pcid.start, DATE(?start_date?, '-1 day')) and COALESCE(pcid.end, DATE(?start_date?, '+1 day')) + on p.id = pcid.parent_id left join Promotion_CheckoutDates pcod - on p.id = pcod.parent_id - and ?end_date? between COALESCE(pcod.start, DATE(?end_date?, '-1 day')) and COALESCE(pcod.end, DATE(?end_date?, '+1 day')) + on p.id = pcod.parent_id left join Promotion_LengthOfStay plos on p.id = plos.parent_id WHERE p.external_id = ?external_id? + and ?book_date? between COALESCE(pbd.start, DATE(?book_date?, '-1 day')) and COALESCE(pbd.end, DATE(?book_date?, '+1 day')) + and ?start_date? between COALESCE(pcid.start, DATE(?start_date?, '-1 day')) and COALESCE(pcid.end, DATE(?start_date?, '+1 day')) + and ?end_date? between COALESCE(pcod.start, DATE(?end_date?, '-1 day')) and COALESCE(pcod.end, DATE(?end_date?, '+1 day')) and (?nights? between COALESCE(plos.min, ?nights?) and COALESCE(plos.max, ?nights?)) and ((JULIANDAY(?start_date?) - JULIANDAY(?book_date?)) between COALESCE(pbw.min, 0) and COALESCE(pbw.max, 1000)) and EXISTS diff --git a/price_calculator/RateModifiers.py b/price_calculator/RateModifiers.py index 79bca95..5e79242 100644 --- a/price_calculator/RateModifiers.py +++ b/price_calculator/RateModifiers.py @@ -27,18 +27,18 @@ def get_rate_modifiers(external_id: str, on r.id = rbw.parent_id left join RateModifications_BookingDates rbd on r.id = rbd.parent_id - and ?book_date? between COALESCE(rbd.start, DATE(?book_date?, '-1 day')) and COALESCE(rbd.end, DATE(?book_date?, '+1 day')) left join RateModifications_CheckinDates rcid - on r.id = rcid.parent_id - and ?start_date? between COALESCE(rcid.start, DATE(?start_date?, '-1 day')) and COALESCE(rcid.end, DATE(?start_date?, '+1 day')) + on r.id = rcid.parent_id left join RateModifications_CheckoutDates rcod - on r.id = rcod.parent_id - and ?end_date? between COALESCE(rcod.start, DATE(?end_date?, '-1 day')) and COALESCE(rcod.end, DATE(?end_date?, '+1 day')) + on r.id = rcod.parent_id left join RateModifications_LengthOfStay rlos on r.id = rlos.parent_id WHERE r.external_id = ?external_id? and (?nights? between COALESCE(rlos.min, ?nights?) and COALESCE(rlos.max, ?nights?)) and ((JULIANDAY(?start_date?) - JULIANDAY(?book_date?)) between COALESCE(rbw.min, 0) and COALESCE(rbw.max, 1000)) + and ?book_date? between COALESCE(rbd.start, DATE(?book_date?, '-1 day')) and COALESCE(rbd.end, DATE(?book_date?, '+1 day')) + and ?start_date? between COALESCE(rcid.start, DATE(?start_date?, '-1 day')) and COALESCE(rcid.end, DATE(?start_date?, '+1 day')) + and ?end_date? between COALESCE(rcod.start, DATE(?end_date?, '-1 day')) and COALESCE(rcod.end, DATE(?end_date?, '+1 day')) and EXISTS ( select day_id diff --git a/price_calculator/Rates.py b/price_calculator/Rates.py index ada5d59..db1dbcd 100644 --- a/price_calculator/Rates.py +++ b/price_calculator/Rates.py @@ -43,9 +43,9 @@ def get_rates(external_id: str, start: datetime, end: datetime, dsn: str) -> lis ) aa on aa.td between o.start and o.end left join OTAHotelInvCountNotifRQ oi - on o.external_id = oi.external_id - and aa.td between oi.start and oi.end + on o.external_id = oi.external_id where o.external_id = ?external_id? + and aa.td between oi.start and oi.end """, param={ "external_id": external_id, diff --git a/price_calculator/TaxesOrFees.py b/price_calculator/TaxesOrFees.py index 4dd96b7..1047b3a 100644 --- a/price_calculator/TaxesOrFees.py +++ b/price_calculator/TaxesOrFees.py @@ -19,17 +19,17 @@ def tax_or_fee_base_query(table_prefix: str): select tf.external_id, tf.calc_type, tf.period, tf.amount, tf.xml_contents from {table_prefix} tf left join {table_prefix}_BookingDates tfbd - on tf.id = tfbd.parent_id - and ?book_date? between COALESCE(tfbd.start, DATE(?book_date?, '-1 day')) and COALESCE(tfbd.end, DATE(?book_date?, '+1 day')) + on tf.id = tfbd.parent_id left join {table_prefix}_CheckinDates tfcid - on tf.id = tfcid.parent_id - and ?start_date? between COALESCE(tfcid.start, DATE(?start_date?, '-1 day')) and COALESCE(tfcid.end, DATE(?start_date?, '+1 day')) + on tf.id = tfcid.parent_id left join {table_prefix}_CheckoutDates tfcod - on tf.id = tfcod.parent_id - and ?end_date? between COALESCE(tfcod.start, DATE(?end_date?, '-1 day')) and COALESCE(tfcod.end, DATE(?end_date?, '+1 day')) + on tf.id = tfcod.parent_id left join {table_prefix}_LengthOfStay tflos on tf.id = tflos.parent_id WHERE tf.external_id = ?external_id? + and ?book_date? between COALESCE(tfbd.start, DATE(?book_date?, '-1 day')) and COALESCE(tfbd.end, DATE(?book_date?, '+1 day')) + and ?start_date? between COALESCE(tfcid.start, DATE(?start_date?, '-1 day')) and COALESCE(tfcid.end, DATE(?start_date?, '+1 day')) + and ?end_date? between COALESCE(tfcod.start, DATE(?end_date?, '-1 day')) and COALESCE(tfcod.end, DATE(?end_date?, '+1 day')) AND COALESCE(tflos.min, ?nights?) <= ?nights? and COALESCE(tflos.max, ?nights?) >= ?nights? and EXISTS (