Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
7 сем / 2 / мисприс-2.2_1374_Наволоцкий_Зырянов_Харитонов.docx
Скачиваний:
3
Добавлен:
29.03.2025
Размер:
1.7 Mб
Скачать

2.7.7 Процедура, меняющая порядок операции, позволяя вставлять ее в начало, середину, конец тм

функция: insert_in_tm - изменение приоритета (порядка) операций в ТМ

-- вход:

-- 1. prod_id - id объекта

-- 2. some_operation_id1 - id операции 1

-- 3. some_operation_id2 - id операции 1

-- 4. operation_id - id операции вставки

-- выход: 1 успех / 0 ошибка

-- эффекты:

-- 1. изменение приоритета (порядка) операций в ТМ

-- требования:

-- 1. передаваемые аргументы должны существовать

def insert_in_tm(prod_id, some_operation_id1, some_operation_id2, operation_id):

try:

# Check if the operation to modify exists

cursor.execute(f"SELECT prod_id FROM {PROD_OPERATION_TABLE} WHERE id = ?", (operation_id,))

operation_prod_id = cursor.fetchone()

if operation_prod_id is None:

print(f'Operation with ID {operation_id} does not exist')

return

# Ensure the product ID matches the target product

if operation_prod_id[0] != prod_id:

print(f'Операция с id={operation_id} не принадлежит к объекту с id={prod_id}')

return

# Check if the provided product ID exists

cursor.execute("SELECT COUNT(*) FROM product WHERE id = ?", (prod_id,))

if cursor.fetchone()[0] == 0:

print(f'Product with ID {prod_id} does not exist')

return

# Fetch current operation priorities and validate neighbors

if some_operation_id1 is None and some_operation_id2 is not None:

# Case 1: Insert at the beginning

cursor.execute(f"SELECT operation_priority FROM {PROD_OPERATION_TABLE} WHERE id = ?", (some_operation_id2,))

next_priority = cursor.fetchone()

if next_priority is None:

print(f'Operation with ID {some_operation_id2} does not exist')

return

new_priority = (next_priority[0] + 0.0) / 2

elif some_operation_id1 is not None and some_operation_id2 is not None:

# Case 2: Insert in the middle

cursor.execute(f"SELECT operation_priority FROM {PROD_OPERATION_TABLE} WHERE id = ?", (some_operation_id1,))

priority1 = cursor.fetchone()

cursor.execute(f"SELECT operation_priority FROM {PROD_OPERATION_TABLE} WHERE id = ?", (some_operation_id2,))

priority2 = cursor.fetchone()

if priority1 is None or priority2 is None:

print(f'One or both neighbor operation IDs do not exist')

return

new_priority = (priority1[0] + priority2[0]) / 2

elif some_operation_id1 is not None and some_operation_id2 is None:

# Case 3: Insert at the end

cursor.execute(f"SELECT operation_priority FROM {PROD_OPERATION_TABLE} WHERE id = ?", (some_operation_id1,))

last_priority = cursor.fetchone()

if last_priority is None:

print(f'Operation with ID {some_operation_id1} does not exist')

return

new_priority = last_priority[0] + 1.0

else:

print("Invalid combination of parameters")

return

# Update the operation_priority for the given operation

cursor.execute(f"UPDATE {PROD_OPERATION_TABLE} SET operation_priority = ? WHERE id = ?", (new_priority, operation_id))

conn.commit()

print(f'Operation {operation_id} successfully updated with new priority {new_priority}')

except Exception as e:

conn.rollback()

print(f"Error in insert_in_tm: {e}")

Соседние файлы в папке 2