-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy path74update.js
More file actions
executable file
·343 lines (310 loc) · 10.1 KB
/
74update.js
File metadata and controls
executable file
·343 lines (310 loc) · 10.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
//
// UPDATE for Alasql.js
// Date: 03.11.2014
// (c) 2014, Andrey Gershun
//
*/
/* global yy alasql */
yy.Update = function (params) {
return Object.assign(this, params);
};
yy.Update.prototype.toString = function () {
var s = 'UPDATE ' + this.table.toString();
if (this.columns) s += ' SET ' + this.columns.toString();
if (this.where) s += ' WHERE ' + this.where.toString();
if (this.output) {
s += ' OUTPUT ';
s += this.output.columns.map(col => col.toString()).join(', ');
if (this.output.intovar) {
s += ' INTO ' + this.output.method + this.output.intovar;
} else if (this.output.intotable) {
s += ' INTO ' + this.output.intotable.toString();
if (this.output.intocolumns) {
s += '(' + this.output.intocolumns.map(col => col.toString()).join(', ') + ')';
}
}
}
return s;
};
yy.SetColumn = function (params) {
return Object.assign(this, params);
};
yy.SetColumn.prototype.toString = function () {
return this.column.toString() + '=' + this.expression.toString();
};
yy.Update.prototype.compile = function (databaseid) {
var self = this;
// console.log(this);
databaseid = this.table.databaseid || databaseid;
var tableid = this.table.tableid;
if (this.where) {
if (this.exists) {
this.existsfn = this.exists.map(function (ex) {
var nq = ex.compile(databaseid);
nq.query.modifier = 'RECORDSET';
return nq;
});
}
if (this.queries) {
this.queriesfn = this.queries.map(function (q) {
var nq = q.compile(databaseid);
nq.query.modifier = 'RECORDSET';
return nq;
});
}
// console.log(73625, this.where.toJS('r', ''));
var wherefn = new Function('r,params,alasql', 'var y;return ' + this.where.toJS('r', '')).bind(
this
);
}
// Construct update function
var s = alasql.databases[databaseid].tables[tableid].onupdatefns || '';
s += ';';
this.columns.forEach(function (col) {
s += "r['" + col.column.columnid + "']=" + col.expression.toJS('r', '') + ';';
});
// console.log(423623, s);
var assignfn = new Function('r,params,alasql', 'var y;' + s);
var statement = function (params, cb) {
var db = alasql.databases[databaseid];
// console.log(db.engineid);
// console.log(db.engineid && alasql.engines[db.engineid].updateTable);
if (db.engineid && alasql.engines[db.engineid].updateTable) {
// console.log('updateTable');
return alasql.engines[db.engineid].updateTable(
databaseid,
tableid,
assignfn,
wherefn,
params,
cb
);
}
if (alasql.options.autocommit && db.engineid) {
alasql.engines[db.engineid].loadTableData(databaseid, tableid);
}
var table = db.tables[tableid];
if (!table) {
throw new Error("Table '" + tableid + "' not exists");
}
// table.dirty = true;
var numrows = 0;
var updatedRows = [];
var pkUpdates = []; // Track primary key updates for CASCADE processing
// Determine which columns are being updated
var updatedColumns = [];
self.columns.forEach(function(col) {
updatedColumns.push(col.column.columnid);
});
// Check if any primary key columns are being updated
var pkColumnsUpdated = false;
if (table.pk && table.pk.columns) {
for (var i = 0; i < table.pk.columns.length; i++) {
if (updatedColumns.indexOf(table.pk.columns[i]) !== -1) {
pkColumnsUpdated = true;
break;
}
}
}
// If PK is being updated, collect old values first and check for RESTRICT
if (pkColumnsUpdated) {
for (var i = 0, ilen = table.data.length; i < ilen; i++) {
if (!wherefn || wherefn(table.data[i], params, alasql)) {
var oldPkValues = {};
table.pk.columns.forEach(function(col) {
oldPkValues[col] = table.data[i][col];
});
// Check for RESTRICT constraints BEFORE updating
for (var childTableId in db.tables) {
var childTable = db.tables[childTableId];
if (!childTable.foreignKeys) continue;
childTable.foreignKeys.forEach(function(fk) {
if (fk.fktable === tableid && fk.fkdatabase === databaseid) {
if (fk.onupdate === 'RESTRICT' || fk.onupdate === 'NO ACTION') {
// Check if any child rows reference this parent row
for (var j = 0; j < childTable.data.length; j++) {
var childRow = childTable.data[j];
var matches = true;
for (var k = 0; k < fk.columns.length; k++) {
var fkCol = fk.columns[k];
var parentCol = fk.fkcolumns[k];
if (childRow[fkCol] !== oldPkValues[parentCol]) {
matches = false;
break;
}
}
if (matches) {
throw new Error('Cannot update primary key in table "' + tableid + '" because it has dependent rows in table "' + childTableId + '"');
}
}
}
}
});
}
}
}
}
for (var i = 0, ilen = table.data.length; i < ilen; i++) {
if (!wherefn || wherefn(table.data[i], params, alasql)) {
// Track row state for OUTPUT clause (DELETED.*)
var oldRow = self.output ? cloneDeep(table.data[i]) : null;
// Store old primary key values if PK is being updated
var oldPkValues = null;
if (pkColumnsUpdated && table.pk) {
oldPkValues = {};
table.pk.columns.forEach(function(col) {
oldPkValues[col] = table.data[i][col];
});
}
if (table.update) {
table.update(assignfn, i, params);
} else {
assignfn(table.data[i], params, alasql);
}
// Track PK update for CASCADE processing
if (pkColumnsUpdated && table.pk) {
var newPkValues = {};
table.pk.columns.forEach(function(col) {
newPkValues[col] = table.data[i][col];
});
// Check if PK actually changed
var pkChanged = false;
for (var col in oldPkValues) {
if (oldPkValues[col] !== newPkValues[col]) {
pkChanged = true;
break;
}
}
if (pkChanged) {
pkUpdates.push({
oldValues: oldPkValues,
newValues: newPkValues
});
}
}
// Track updated row for OUTPUT clause (INSERTED.*)
if (self.output) {
updatedRows.push({
deleted: oldRow,
inserted: cloneDeep(table.data[i]),
});
}
numrows++;
}
}
// Process CASCADE operations for primary key updates
if (pkUpdates.length > 0) {
pkUpdates.forEach(function(update) {
// Check all child tables that reference this table
for (var childTableId in db.tables) {
var childTable = db.tables[childTableId];
if (!childTable.foreignKeys) continue;
childTable.foreignKeys.forEach(function(fk) {
// Check if this foreign key references the table we're updating
if (fk.fktable === tableid && fk.fkdatabase === databaseid) {
// Skip RESTRICT and NO ACTION - already checked above
if (fk.onupdate === 'RESTRICT' || fk.onupdate === 'NO ACTION') {
return;
}
// Find matching child rows based on old PK values
var childRowsToProcess = [];
for (var j = 0; j < childTable.data.length; j++) {
var childRow = childTable.data[j];
var matches = true;
for (var k = 0; k < fk.columns.length; k++) {
var fkCol = fk.columns[k];
var parentCol = fk.fkcolumns[k];
if (childRow[fkCol] !== update.oldValues[parentCol]) {
matches = false;
break;
}
}
if (matches) {
childRowsToProcess.push(j);
}
}
// Apply the appropriate action based on onupdate
if (childRowsToProcess.length > 0) {
if (fk.onupdate === 'CASCADE') {
// Update child foreign key columns to new values
childRowsToProcess.forEach(function(idx) {
for (var k = 0; k < fk.columns.length; k++) {
var fkCol = fk.columns[k];
var parentCol = fk.fkcolumns[k];
childTable.data[idx][fkCol] = update.newValues[parentCol];
}
});
} else if (fk.onupdate === 'SET NULL') {
// Set foreign key columns to NULL
childRowsToProcess.forEach(function(idx) {
fk.columns.forEach(function(col) {
var colDef = childTable.xcolumns[col];
if (colDef && colDef.notnull) {
throw new Error('Cannot SET NULL on NOT NULL column "' + col + '" in table "' + childTableId + '"');
}
childTable.data[idx][col] = null;
});
});
} else if (fk.onupdate === 'SET DEFAULT') {
// Set foreign key columns to their default values
childRowsToProcess.forEach(function(idx) {
fk.columns.forEach(function(col) {
// Find the column definition to get default value
var colDef = childTable.xcolumns[col];
if (colDef && colDef.default !== undefined) {
childTable.data[idx][col] = colDef.default;
} else if (colDef && colDef.notnull) {
throw new Error('Cannot SET DEFAULT to NULL on NOT NULL column "' + col + '" in table "' + childTableId + '" without a DEFAULT value');
} else {
childTable.data[idx][col] = null;
}
});
});
}
}
}
});
}
});
}
if (alasql.options.autocommit && db.engineid) {
alasql.engines[db.engineid].saveTableData(databaseid, tableid);
}
var res = numrows;
// Handle OUTPUT clause
if (self.output) {
var output = [];
for (var i = 0; i < updatedRows.length; i++) {
var deleted = updatedRows[i].deleted;
var inserted = updatedRows[i].inserted;
var outputRow = {};
self.output.columns.forEach(function (col) {
if (col.columnid === '*') {
// For *, use INSERTED values
for (var key in inserted) {
outputRow[key] = inserted[key];
}
} else {
var colname = col.as || col.columnid;
// Check tableid to determine which version to use
if (col.tableid === 'DELETED') {
outputRow[colname] = deleted[col.columnid];
} else {
// Default to INSERTED
outputRow[colname] = inserted[col.columnid];
}
}
});
output.push(outputRow);
}
res = output;
}
if (cb) cb(res);
return res;
};
return statement;
};
yy.Update.prototype.execute = function (databaseid, params, cb) {
return this.compile(databaseid)(params, cb);
};