Finish
This commit is contained in:
parent
85b49ddf0f
commit
39394caa8e
3 changed files with 249 additions and 15 deletions
|
@ -80,37 +80,50 @@ class DatabaseManager:
|
|||
try:
|
||||
cur = self.connection.cursor()
|
||||
|
||||
# Build the UPDATE query dynamically
|
||||
# Build the INSERT and UPDATE query dynamically
|
||||
insert_placeholders = []
|
||||
insert_values = [key_value] # Start with key_value
|
||||
|
||||
set_clauses = []
|
||||
values = []
|
||||
update_values = []
|
||||
|
||||
for field, value in fields.items():
|
||||
if value == "NOW()":
|
||||
# Special handling for NOW()
|
||||
insert_placeholders.append("NOW()")
|
||||
set_clauses.append(f"{field} = NOW()")
|
||||
else:
|
||||
insert_placeholders.append("%s")
|
||||
insert_values.append(value)
|
||||
set_clauses.append(f"{field} = %s")
|
||||
values.append(value)
|
||||
update_values.append(value)
|
||||
|
||||
# Add schema prefix if table doesn't already have it
|
||||
full_table_name = table if '.' in table else f"gas_station_1.{table}"
|
||||
|
||||
# Build the complete query
|
||||
query = f"""
|
||||
INSERT INTO {full_table_name} ({key_field}, {', '.join(fields.keys())})
|
||||
VALUES (%s, {', '.join(['%s'] * len(fields))})
|
||||
VALUES (%s, {', '.join(insert_placeholders)})
|
||||
ON CONFLICT ({key_field})
|
||||
DO UPDATE SET {', '.join(set_clauses)}
|
||||
"""
|
||||
|
||||
# Add key_value to the beginning of values list
|
||||
all_values = [key_value] + list(fields.values()) + values
|
||||
# Combine values for the query: insert_values + update_values
|
||||
all_values = insert_values + update_values
|
||||
|
||||
logger.debug(f"SQL Query: {query}")
|
||||
logger.debug(f"Values: {all_values}")
|
||||
|
||||
cur.execute(query, all_values)
|
||||
self.connection.commit()
|
||||
cur.close()
|
||||
logger.info(f"Updated {table} for {key_field}={key_value}")
|
||||
logger.info(f"✅ Updated {table} for {key_field}={key_value} with fields: {fields}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to execute update on {table}: {e}")
|
||||
logger.error(f"❌ Failed to execute update on {table}: {e}")
|
||||
logger.debug(f"Query: {query if 'query' in locals() else 'Query not built'}")
|
||||
logger.debug(f"Values: {all_values if 'all_values' in locals() else 'Values not prepared'}")
|
||||
if self.connection:
|
||||
self.connection.rollback()
|
||||
return False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue