Skip to content

Commit

Permalink
added INSERT instructions for ships inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Nov 15, 2023
1 parent 16f73fe commit 5bab4e6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions straders_sdk/pg_pieces/upsert_ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,27 @@ def _upsert_ship_cooldown(connection, ship: Ship):


def _upsert_ship_cargo(connection, ship: Ship):
return LocalSpaceTradersRespose(
None, None, None, url=f"{__name__}._upsert_ship_cargo"
)
sql = """
INSERT INTO SHIP_CARGO (ship_symbol, trade_symbol, quantity)
VALUES (%s, %s, %s) ON CONFLICT (ship_symbol, trade_symbol) DO UPDATE
SET quantity = EXCLUDED.quantity;
"""

values = [(ship.name, t.symbol, t.units) for t in ship.cargo_inventory]
for value in values:
resp = try_execute_upsert(connection, sql, value)
if not resp:
return resp
if len(values) > 0:
sql = """
delete from ship_cargo where ship_symbol = %s and trade_symbol != ANY(%s);"""
values = (ship.name, [t.symbol for t in ship.cargo_inventory])
resp = try_execute_upsert(connection, sql, values)
else:
sql = "delete from ship_cargo where ship_symbol = %s;"
resp = try_execute_upsert(connection, sql, (ship.name,))
return resp
# not implemented yet
pass

Expand Down

0 comments on commit 5bab4e6

Please sign in to comment.