Repository navigation
Description
What happens today
Each statement writes rows its own way, and the rules are split between the statement and the table closures from src/statements/createtable.js.
- INSERT VALUES (
src/statements/insert.js) generates per-row code with the defaults spliced in (line 246) and callstable.insert(a, orreplace, ignore). INSERT SELECT (line 380) does the same in JavaScript.INSERT ... DEFAULT VALUES(line 425) pushes intotable.dataand skips every rule. - UPDATE (
src/statements/update.js, line 93) appliesonupdatefnsand the SET list throughassignfnand callstable.update(assignfn, i, params). - DELETE (
src/statements/delete.js) callstable.delete(i)and runs the AFTER DELETE triggers itself (line 113). - OUTPUT rows are built five times (
insert.jslines 312 and 398,update.jsline 154,delete.jslines 131 and 208).
What to change
-
Add
writeRows(table, op, rows, ctx)insrc/statements/write.jswithopone of'insert','update','delete'. Per row and in today's order it runs defaults and identity (insert), ON UPDATE and the SET list (update), BEFORE and INSTEAD OF triggers, checks and keys fromsrc/statements/tablerules.js(Move the key and check rules of a table into one module #2716), the write totable.data, the key and other indexes, and AFTER triggers. Defaults, identity and triggers come fromtable.schema(Keep the definition of a table as data in table.schema and build its rules from it #3169). It returns{count, output}with{inserted, deleted}pairs when the statement has OUTPUT.const {count, output} = writeRows(table, 'update', matches, ctx); return self.output ? outputRows(self.output, output) : count;
-
Add
outputRows(spec, pairs)in the same file, the only place that builds OUTPUT rows (*, plain columns,INSERTED.x,DELETED.x, aliases). -
INSERT VALUES and INSERT SELECT build plain value rows (generated code keeps only the column expressions and type conversion) and hand them to
writeRows.INSERT ... DEFAULT VALUESuseswriteRowswith a flag that skips the rules. -
UPDATE and DELETE find rows with
wherefnas today and hand them towriteRows. AFTER DELETE triggers move into the pipeline. -
table.insert,table.updateandtable.deletebecome thin wrappers overwriteRowsfor one row. -
Triggers are called through
alasql.executeTriggeras today.
What stays the same
- Every result for memory tables, including counts with IGNORE and OR REPLACE, the return value of INSERT into a class table, OUTPUT rows, trigger order and arguments, and every error text.
- Storage engines take their current path.
alasql.options.nocountkeeps its effect.- No result changes.
Done when
- A new test file named after this issue runs INSERT VALUES, INSERT SELECT, INSERT SET, INSERT DEFAULT VALUES, UPDATE and DELETE with and without OUTPUT, OR REPLACE and IGNORE, and BEFORE, INSTEAD OF and AFTER triggers of each kind, and pins the results and the trigger order.
- OUTPUT rows are built only in
outputRows. - The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Activity
[-]Review #2302 for code improvements: OUTPUT rows built by hand in each statement[/-][+]Pass the rows of INSERT, UPDATE and DELETE through one write pipeline[/+]