WEBVTT

00:00:00.350 --> 00:00:01.331
Welcome back!

00:00:01.511 --> 00:00:02.685
Here's what we'll learn.

00:00:02.865 --> 00:00:05.553
In lesson nine we supplied totals ourselves.

00:00:05.733 --> 00:00:09.231
Now Python hooks will calculate and validate
them when we write.

00:00:09.411 --> 00:00:13.806
We will also see which direct SQL operations
bypass this behavior.

00:00:13.986 --> 00:00:18.743
These are Python hooks in Asqueel, not SQL
triggers installed in SQLite.

00:00:18.923 --> 00:00:20.737
We keep our database in memory.

00:00:22.150 --> 00:00:23.921
Let's define the write behavior.

00:00:24.101 --> 00:00:28.887
Our shared calculation checks quantity and
price, then stores their product rounded to

00:00:28.887 --> 00:00:29.221
cents.

00:00:29.401 --> 00:00:32.857
The inserting and updating hooks run before the
SQL write.

00:00:33.037 --> 00:00:36.557
The updated hook prints the old and new
quantities afterwards.

00:00:36.737 --> 00:00:39.894
Afterwards means after the statement, not after
commit.

00:00:41.224 --> 00:00:43.016
Let's attach it to invoice rows.

00:00:43.196 --> 00:00:47.207
X table class gives this existing table its
Python behavior.

00:00:47.387 --> 00:00:50.032
Reload the model and get a fresh table handle.

00:00:50.212 --> 00:00:54.756
True means no migration is needed: we added
Python hooks, not columns.

00:00:54.936 --> 00:00:57.325
Existing rows are not recalculated.

00:00:58.750 --> 00:01:00.179
Let's insert two teas.

00:01:00.359 --> 00:01:02.898
We supply quantity and price, but no total.

00:01:03.078 --> 00:01:04.785
The inserting hook stores seven.

00:01:04.965 --> 00:01:08.271
The formula column from lesson four also reads
seven.

00:01:08.451 --> 00:01:13.145
The stored total is written by our hook; the
formula is calculated when we query.

00:01:13.325 --> 00:01:15.053
This change is still uncommitted.

00:01:16.383 --> 00:01:18.153
Let's change only the quantity.

00:01:18.333 --> 00:01:23.624
Query update supplies the complete record and
its previous values, as in lesson nine.

00:01:23.804 --> 00:01:27.708
This time our updating hook recalculates ten
fifty automatically.

00:01:27.888 --> 00:01:30.576
The updated hook prints two to three before
commit.

00:01:31.950 --> 00:01:34.254
Let's compare a direct SQL update.

00:01:34.434 --> 00:01:38.295
SQL update changes one selected row and returns
one.

00:01:38.475 --> 00:01:43.531
It skips our Python hooks: no message appears,
and the stored total stays ten fifty.

00:01:43.711 --> 00:01:45.589
The formula now reads fourteen.

00:01:45.769 --> 00:01:47.731
Database constraints still apply.

00:01:47.911 --> 00:01:53.394
The direct SQL insert used for CSV imports also
skips these Python hooks.

00:01:54.724 --> 00:01:56.921
Let's run the normal write cycle again.

00:01:57.101 --> 00:01:59.597
Normal query update runs our hooks again.

00:01:59.777 --> 00:02:05.089
Both totals become seventeen fifty, and the
result contains the updated primary key.

00:02:06.450 --> 00:02:08.477
Let's undo our demonstration writes.

00:02:08.657 --> 00:02:14.134
Rollback removes our new row and its updates
together, returning us to thirty-six original

00:02:14.134 --> 00:02:14.438
rows.

00:02:14.618 --> 00:02:18.159
It does not erase messages already printed by a
Python hook.

00:02:18.339 --> 00:02:23.395
Commit here closes the verification read; it
does not save the rolled-back examples.

00:02:24.783 --> 00:02:26.746
Let's reject a zero quantity.

00:02:26.926 --> 00:02:31.001
Our inserting hook raises a value error before
SQL insertion.

00:02:31.181 --> 00:02:34.637
After a failed write cycle, roll back before
continuing.

00:02:34.817 --> 00:02:39.169
That rollback affects all pending work in the
transaction, not just this row.

00:02:39.349 --> 00:02:42.826
We began from a clean boundary, so no earlier
work is lost.

00:02:43.006 --> 00:02:47.379
The empty result confirms that row nine hundred
and three was never stored.

00:02:48.783 --> 00:02:50.277
Thanks for following along!

00:02:50.457 --> 00:02:54.147
Use normal writes for Python validation and
calculations.

00:02:54.327 --> 00:02:57.975
Use direct SQL deliberately, with complete valid
data.

00:02:58.155 --> 00:03:00.289
Keep transaction boundaries explicit.

00:03:00.469 --> 00:03:04.906
We have added automatic write behavior to the
operations from lesson nine.
