def order_detail_flush_dirty(a_row: models.OrderDetail, a_session: session): old_row = get_old_row(a_row) # type: models.OrderDetail if a_row.OrderId == old_row.OrderId: if a_row.ProductId != old_row.ProductId: product = a_session.query(models.Product). \ filter(models.Product.Id == a_row.ProductId).one() a_row.UnitPrice = product.UnitPrice a_row.Amount = a_row.UnitPrice * a_row.Quantity if a_row.Amount != old_row.Amount: order = a_row.OrderHeader order.AmountTotal += a_row.Amount - old_row.Amount old_order = ObjectView(row2dict(order)) order_update(order, old_order, a_session) row_prt(order, "order_detail_flush_dirty adjusted to: " + str(order.AmountTotal)) else: # moved item to different order order = a_row.OrderHeader # reduce the old one order.AmountTotal -= old_row.Amount old_order = ObjectView(row2dict(order)) order_update(order, old_order, a_session) row_prt(order, "order_detail_flush_dirty adjusted to: " + str(order.AmountTotal)) if a_row.ProductId != old_row.ProductId: product = a_session.query(models.Product). \ filter(models.Product.Id == old_row.ProductId).one() a_row.UnitPrice = product.UnitPrice a_row.Amount = a_row.UnitPrice * a_row.Quantity order = a_session.query(models.Order). \ filter(models.Order.Id == a_row.OrderId).one() old_order = ObjectView(row2dict(order)) order.AmountTotal += a_row.Amount order_update(order, old_order, a_session) row_prt(order, "order_detail_flush_dirty adjusted to: " + str(order.AmountTotal))
def order_flush_dirty(a_row, a_session: session): """ Called from module init on before_flush E.g., altering an Order ShippedDate (we must adjust Customer balance) """ old_row = get_old_row(a_row) order_update(a_row, old_row, a_session)
def customer_flush_dirty(a_row, a_session: session): """ Called from listeners.py on before_flush """ old_row = get_old_row(a_row) row_prt(a_row, "\ncustomer_flush_dirty") customer_update(a_row, old_row, a_session)
def before_flush(a_session: session, a_flush_context, an_instances): """ Logic Execution processes LogicRows: row and old_row Note old_row is critical for: * user logic (did the value change? by how much?) * performance / pruning (skip rules iff no dependent values change) * performance / optimization (1 row adjustments, not expensive select sum/count) """ logic_engine.logic_logger.debug( "\nLogic Phase (sqlalchemy before_flush)\t\t\t") # print("\n***************** sqlalchemy calls logic_engine\n") row_cache = RowCache() what_is = a_session.dirty for each_instance in a_session.dirty: table_name = each_instance.__tablename__ old_row = get_old_row(each_instance) logic_row = LogicRow(row=each_instance, old_row=old_row, ins_upd_dlt="upd", nest_level=0, a_session=a_session, row_cache=row_cache) logic_row.update(reason="client") for each_instance in a_session.new: table_name = each_instance.__tablename__ logic_row = LogicRow(row=each_instance, old_row=None, ins_upd_dlt="ins", nest_level=0, a_session=a_session, row_cache=row_cache) logic_row.insert(reason="client") logic_engine.logic_logger.debug("Commit Logic Phase \t\t\t") for each_logic_row_key in row_cache.row_list: each_logic_row = row_cache.row_list[each_logic_row_key] logic_engine.engine_logger.debug("visit: " + each_logic_row.__str__()) commit_row_events = rule_bank_withdraw.rules_of_class( each_logic_row, CommitRowEvent) for each_row_event in commit_row_events: each_logic_row.log("Commit Event") each_row_event.execute(each_logic_row) logic_engine.logic_logger.debug("Flush Phase \t\t\t")
def order_detail_commit_dirty(a_row, a_session: session): old_row = get_old_row(a_row) row_prt(a_row, "\norder_detail_commit_dirty")
def customer_commit_dirty(a_row, a_session: session): old_row = get_old_row(a_row) row_prt(a_row, "order_commit_dirty")