-
Notifications
You must be signed in to change notification settings - Fork 695
Expand file tree
/
Copy path71trigger.js
More file actions
executable file
·134 lines (117 loc) · 3.45 KB
/
Copy path71trigger.js
File metadata and controls
executable file
·134 lines (117 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
//
// TRIGGER for Alasql.js
// Date: 29.12.2015
//
*/
yy.CreateTrigger = function (params) {
return Object.assign(this, params);
};
yy.CreateTrigger.prototype.toString = function () {
var s = 'CREATE TRIGGER ' + this.trigger + ' ';
if (this.when) s += this.when + ' ';
s += this.action + ' ON ';
if (this.table.databaseid) s += this.table.databaseid + '.';
s += this.table.tableid + ' ';
s += this.statement.toString();
return s;
};
const validTriggers = [
'beforeinsert',
'afterinsert',
'insteadofinsert',
'beforedelete',
'afterdelete',
'insteadofdelete',
'beforeupdate',
'afterupdate',
'insteadofupdate',
];
yy.CreateTrigger.prototype.execute = function (databaseid, params, cb) {
let res = 1; // No tables removed
const triggerid = this.trigger;
databaseid = this.table.databaseid || databaseid;
const db = alasql.databases[databaseid];
const {tableid} = this.table;
const trigger = {
action: this.action,
when: this.when,
statement: this.statement,
funcid: this.funcid,
tableid,
};
db.triggers[triggerid] = trigger;
const actionKey = `${this.when}${this.action}`.toLowerCase();
if (validTriggers.includes(actionKey)) {
// Ensure the existence of db.tables[tableid] and db.tables[tableid][actionKey]
db.tables[tableid] = db.tables[tableid] || {};
db.tables[tableid][actionKey] = db.tables[tableid][actionKey] || {};
db.tables[tableid][actionKey][triggerid] = trigger;
}
if (cb) res = cb(res);
return res;
};
yy.DropTrigger = function (params) {
return Object.assign(this, params);
};
yy.DropTrigger.prototype.toString = function () {
var s = 'DROP TRIGGER ' + this.trigger;
return s;
};
/**
Drop trigger
@param {string} databaseid Database id
@param {object} params Parameters
@param {callback} cb Callback function
@return Number of dropped triggers
@example
DROP TRIGGER one;
*/
yy.DropTrigger.prototype.execute = function (databaseid, params, cb) {
let res = 0; // No tables removed
const db = alasql.databases[databaseid];
const triggerid = this.trigger;
// Get the trigger
const trigger = db.triggers[triggerid];
// If the trigger exists
if (trigger) {
const {tableid} = trigger;
if (tableid) {
res = 1;
// Delete the trigger from all trigger points
validTriggers.forEach(point => {
delete db.tables[tableid][point][triggerid];
});
// Finally, delete the trigger itself
delete db.triggers[triggerid];
} else {
throw new Error('Trigger Table not found');
}
} else {
throw new Error('Trigger not found');
}
if (cb) res = cb(res);
return res;
};
/**
* Helper function to execute a trigger with proper parameter passing
* @param {object} trigger - The trigger object
* @param {string} databaseid - Database ID for statement execution
* @param {...any} args - Arguments to pass to the trigger function
* @return {*} Result from trigger execution (typically boolean or undefined)
*/
alasql.executeTrigger = function (trigger, databaseid, ...args) {
if (!trigger) return;
if (trigger.funcid) {
// Direct function ID (older syntax: CREATE TRIGGER ... tablename funcname)
return alasql.fn[trigger.funcid](...args);
} else if (trigger.statement) {
// CALL syntax: CREATE TRIGGER ... CALL funcname()
if (trigger.statement.expression && trigger.statement.expression.funcid) {
return alasql.fn[trigger.statement.expression.funcid](...args);
} else {
// Fallback: execute statement without parameters (for non-CALL statements)
return trigger.statement.execute(databaseid);
}
}
};