Skip to content

Commit

Permalink
Correct linkage joins and uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
Siliconrob committed Jan 31, 2024
1 parent 2b1d955 commit 8887d41
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
18 changes: 9 additions & 9 deletions data_messages/Promotions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
Expand All @@ -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 (
Expand All @@ -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
Expand All @@ -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)
)""")


Expand Down Expand Up @@ -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=[{
Expand Down Expand Up @@ -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=[{
Expand Down Expand Up @@ -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=[{
Expand Down Expand Up @@ -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={
Expand Down
20 changes: 10 additions & 10 deletions data_messages/RateModifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
)""")


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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=[{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions price_calculator/ExtraGuestCharges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions price_calculator/Promotions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions price_calculator/RateModifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions price_calculator/Rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions price_calculator/TaxesOrFees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down

0 comments on commit 8887d41

Please sign in to comment.