Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.2 kB
2
Indexable
Never
# ... (previous code)

if pk_value not in target_df[pk].values:
    # ... (previous code)
else:
    # Fetch the corresponding row from the target DataFrame based on the primary key
    target_row = target_df[target_df[pk] == pk_value].iloc[0]
    if not source_row.equals(target_row):
        columns_to_update = []
        for column_name, source_val in source_row.items():
            target_val = target_row[column_name]
            if source_val != target_val:
                if is_datetime(source_val):
                    # Format datetime values using the appropriate function for the database type
                    if db_type_ext == 'ORACLE':
                        update_value = f"TO_DATE('{source_val}', 'YYYY-MM-DD HH24:MI:SS')"
                    elif db_type_ext in ('MYSQL', 'MARIA'):
                        # For MariaDB and MySQL, use STR_TO_DATE
                        update_value = f"STR_TO_DATE('{source_val}', '%Y-%m-%d %H:%i:%s')"
                    else:
                        # Enclose other values in single quotes
                        update_value = f"'{source_val}'"
                    
                    # Add the column name and formatted value to the update statement
                    columns_to_update.append(f"{column_name} = {update_value}")
                else:
                    # Handle non-datetime columns (e.g., strings, numbers) here
                    columns_to_update.append(f"{column_name} = '{source_val}'")

        # Generate an update query dynamically
        if columns_to_update:
            update_query = f"UPDATE {schema_ext}.{table_name} SET "
            update_query += ", ".join(columns_to_update)
            update_query += f" WHERE {eprm_table_col_pk} = '{pk_value}'"

            try:
                print(update_query)
                cursor_ext.execute(update_query)
                update_sucess_count += 1
                # connection_ext.commit()
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
                update_failed_count += 1

# ... (rest of the code)