Subject Need for BEFORE COMMIT trigger ?
Author sugi
Dear firebird-support,

I recently came across a case where i thought a 'BEFORE COMMIT' trigger
would be nice, and I'm wondering what is the firebird-ish way (without
locking ? ) to achieve the same effect. Here's the hypotethical
(simplified) data structure:

Table CUSTOMER : CUSTOMER_ID, CUSTOMER_NAME, ADDRESS, CREDIT_LIMIT,
CURRENT_CREDIT_LEVEL
Table PRODUCT : PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE
Table INVOICE : INVOICE_ID, CUSTOMER_ID, INVOICE_DATE
Table INVOICE_DETAIL : INVOICE_ID, INVOICE_DETAIL_ID, PRODUCT_ID, QTY,
UNIT_PRICE

Imagine a simple master-detail data entry screen for
INVOICE/INVOICE_DETAIL. At the end of the data entry session (when the
user press an 'OK' button, for example), we will need to check whether
CUSTOMER.CURRENT_CREDIT_LEVEL + INVOICE.TOTAL <= CUSTOMER.CREDIT_LIMIT.

The checking is preferably done in the server, but i'm not sure where is
the best place to put the check:
- if I put the check on INVOICE_DETAIL BEFORE_INSERT trigger, then i
think there is a possibility of 'collision' when two or more users are
creating invoices for the same customer. (Rare occasion, but not
impossible).
- maybe put the logic in INVOICE BEFORE_UPDATE trigger? (Not
BEFORE_INSERT trigger, since in normal master-detail situation, the
first time a master record is inserted, there's no detail yet, hence
nothing to check).

Any suggestions are very much appreciated.
Thank you very much in advance.
sugi.