Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 15, 2025

Fixes SQL-99 trigger functionality to properly pass row data when using the CALL funcname() syntax. The trigger framework (parser, registration, execution timing) was already implemented, but the CALL syntax wasn't extracting function IDs correctly or passing row parameters.

Problem

Triggers created with CREATE TRIGGER ... CALL funcname() were being invoked but received undefined instead of row data. This affected all trigger types across INSERT, UPDATE, and DELETE operations.

Changes Made

1. Fixed Parameter Passing for All Trigger Types

  • INSERT triggers: Pass row data to BEFORE, AFTER, and INSTEAD OF triggers
  • UPDATE triggers: Pass old and new row data to all trigger types
  • DELETE triggers: Pass deleted row data to all trigger types
  • AFTER DELETE: Fixed to call once per deleted row with proper data

2. Refactored for Maintainability

  • Created alasql.executeTrigger() helper function in src/71trigger.js
  • Eliminated 8 instances of duplicated trigger execution logic
  • Helper handles both direct funcid syntax and CALL syntax with proper parameter spreading

3. Files Modified

  • src/71trigger.js: Added alasql.executeTrigger() helper function
  • src/60createtable.js: Updated INSERT, UPDATE, DELETE trigger execution in table.insert(), table.update(), and table.delete()
  • src/72delete.js: Fixed AFTER DELETE trigger execution with row-level data
  • test/test1119.js: Enabled trigger tests (removed .skip)

Test Results

5/5 trigger tests passing in test1119.js:

  • BEFORE INSERT trigger receives the inserted row
  • AFTER INSERT trigger receives the inserted row
  • BEFORE UPDATE trigger receives old and new row data
  • BEFORE DELETE trigger receives the row being deleted
  • INSTEAD OF INSERT trigger receives the row

37 tests passing including all basic INSERT/UPDATE/DELETE tests with no regressions

Code Quality

  • ✅ Code review completed - refactored to address duplication concerns
  • ✅ Security scan completed - 0 vulnerabilities found
  • ✅ Added documentation for row-level trigger behavior

Implementation Notes

  • Supports ES6 rest parameters (...args) consistent with existing codebase
  • Row-level triggers fire once per affected row (N rows × M triggers = N×M calls)
  • Maintains backward compatibility with direct funcid syntax
  • INSTEAD OF triggers prevent default operation execution as expected
Original prompt

This section details on the original issue you should resolve

<issue_title>Triggers - Database Event Automation Support</issue_title>
<issue_description>Priority: 3-4 (Medium)
Impact: SQL-99 Compliance
Test Files: test/test1119.js
Test Count: 1 test

Problem Description

The test file contains skipped tests for SQL trigger functionality including BEFORE, AFTER, and INSTEAD OF triggers for INSERT, UPDATE, and DELETE operations. Triggers are essential SQL-99 features for database automation and business logic enforcement.

Specific Test Cases

test1119.js - Trigger Callback Parameters (1 test suite)

  • Lines 6-156: Comprehensive trigger testing framework
  • Tests all trigger types: BEFORE/AFTER/INSTEAD OF
  • Tests all DML operations: INSERT/UPDATE/DELETE
  • Tests callback parameter passing and data reception

Expected Behavior

AlaSQL should support SQL-99 standard trigger functionality:

  1. BEFORE Triggers: Execute before DML operations
  2. AFTER Triggers: Execute after DML operations
  3. INSTEAD OF Triggers: Replace DML operations
  4. Trigger Events: INSERT, UPDATE, DELETE operations
  5. Callback Parameters: Pass row data to trigger functions
  6. Business Logic: Data validation and automation

Current Status

  • Test Status: Entire test suite skipped (describe.skip)
  • Error: Unknown (tests not executed)
  • Root Cause: Trigger system not implemented in database engine

Implementation Requirements

1. Parser Support

Add trigger syntax to src/alasqlparser.jison:

-- Basic trigger creation CREATE TRIGGER trigger_name BEFORE INSERT ON table_name CALL trigger_function() -- AFTER trigger CREATE TRIGGER trigger_name AFTER UPDATE ON table_name CALL trigger_function() -- INSTEAD OF trigger CREATE TRIGGER trigger_name INSTEAD OF DELETE ON table_name CALL trigger_function() -- Drop trigger DROP TRIGGER trigger_name

2. Trigger Engine

  • Trigger Registration: Store trigger definitions in table metadata
  • Event Detection: Intercept DML operations (INSERT/UPDATE/DELETE)
  • Execution Control: BEFORE/AFTER/INSTEAD OF timing
  • Parameter Passing: Pass row data to trigger functions
  • Error Handling: Handle trigger failures appropriately

3. Trigger Types Support

  • BEFORE INSERT: Validate/modify data before insertion
  • AFTER INSERT: Post-insertion processing
  • BEFORE UPDATE: Validate/modify data before update
  • AFTER UPDATE: Post-update processing
  • BEFORE DELETE: Validate before deletion
  • AFTER DELETE: Post-deletion cleanup
  • INSTEAD OF: Custom DML operation handling

4. Data Parameter Support

  • INSERT Triggers: Pass inserted row data
  • UPDATE Triggers: Pass old and new row data
  • DELETE Triggers: Pass deleted row data
  • Function Integration: Integrate with AlaSQL function system

5. Advanced Features

  • Multiple Triggers: Support multiple triggers per table/event
  • Trigger Ordering: Control execution order for multiple triggers
  • Nested Triggers: Handle triggers that fire other triggers
  • Performance: Efficient trigger execution without major overhead

SQL-99 Features Involved

  • CREATE TRIGGER statement
  • BEFORE/AFTER/INSTEAD OF trigger timing
  • INSERT/UPDATE/DELETE trigger events
  • Trigger function calling mechanism
  • Row-level trigger execution
  • Database automation and business logic

Dependencies

  • AlaSQL parser (src/alasqlparser.jison)
  • Database engine DML operations
  • Function system (alasql.fn)
  • Table metadata management
  • Transaction system (for rollback support)

Acceptance Criteria

Basic Trigger Functionality (test1119.js)

  • All 5 trigger tests enabled and passing
  • BEFORE INSERT trigger with row data
  • AFTER INSERT trigger with row data
  • BEFORE UPDATE trigger with old/new row data
  • BEFORE DELETE trigger with row data
  • INSTEAD OF INSERT trigger with row data
  • Proper callback parameter passing
  • Trigger function integration

Implementation Strategy

Phase 1: Basic Trigger Framework

  1. Parser Support for CREATE/DROP TRIGGER
  2. Trigger Registration in table metadata
  3. Basic Event Detection for DML operations
  4. Simple Trigger Execution with function calling

Phase 2: Trigger Types and Parameters

  1. BEFORE/AFTER Timing implementation
  2. INSTEAD OF Trigger support
  3. Parameter Passing for different DML operations
  4. Error Handling and rollback support

Phase 3: Advanced Features

  1. Multiple Triggers per table/event
  2. Trigger Ordering and execution control
  3. Performance Optimization for trigger overhead
  4. Edge Cases and error scenarios

Test Implementation Examples

-- BEFORE INSERT trigger CREATE TRIGGER validate_email BEFORE INSERT ON users CALL validateEmailFunction() -- AFTER UPDATE trigger CREATE TRIGGER audit_changes AFTER UPDATE ON products CALL auditUpdateFunction() -- INSTEAD OF... </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes AlaSQL/alasql#2365 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
@mathiasrw
Copy link
Member

@copilot continue

Copy link
Contributor Author

Copilot AI commented Dec 17, 2025

@copilot continue

The trigger implementation is complete and ready for review. All 5 acceptance criteria tests are passing:

✅ BEFORE INSERT trigger with row data
✅ AFTER INSERT trigger with row data
✅ BEFORE UPDATE trigger with old/new row data
✅ BEFORE DELETE trigger with row data
✅ INSTEAD OF INSERT trigger with row data

The implementation:

  • Fixed parameter passing for all trigger types (BEFORE/AFTER/INSTEAD OF)
  • Refactored duplicate code into alasql.executeTrigger() helper
  • Verified with 37 passing tests (no regressions)
  • Passed code review and security scan (0 vulnerabilities)

Ready to merge when you are.

Copilot AI changed the title [WIP] Add support for SQL-99 database event automation triggers Fix SQL-99 trigger CALL syntax to pass row data parameters Dec 17, 2025
Copilot AI requested a review from mathiasrw December 17, 2025 12:40
@mathiasrw mathiasrw marked this pull request as ready for review December 17, 2025 12:44
Copilot AI and others added 5 commits December 17, 2025 23:47
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
@mathiasrw mathiasrw force-pushed the copilot/add-database-event-automation branch from b61aece to 4093c48 Compare December 17, 2025 12:48
@mathiasrw mathiasrw merged commit 4816906 into develop Dec 17, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants