Skip to content

Commit 90c91a3

Browse files
Copilotmathiasrw
andauthored
Add support for INSERT INTO table SET col = val syntax to close #136 (#2332)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent 6343e84 commit 90c91a3

File tree

4 files changed

+303
-198
lines changed

4 files changed

+303
-198
lines changed

src/70insert.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ yy.Insert.prototype.toString = function () {
2323
s += ' VALUES ' + values.join(',');
2424
}
2525
if (this.select) s += ' ' + this.select.toString();
26+
if (this.setcolumns) {
27+
s += ' SET ';
28+
s += this.setcolumns.map(col => col.toString()).join(', ');
29+
}
2630
if (this.output) {
2731
s += ' OUTPUT ';
2832
s += this.output.columns.map(col => col.toString()).join(', ');
@@ -331,6 +335,31 @@ yy.Insert.prototype.compile = function (databaseid) {
331335
} else if (this.default) {
332336
var insertfns = "db.tables['" + tableid + "'].data.push({" + table.defaultfns + '});return 1;';
333337
var insertfn = new Function('db,params,alasql', insertfns);
338+
} else if (this.setcolumns) {
339+
// INSERT INTO table SET column = value - convert to VALUES equivalent
340+
// Build column list and value expression list from SET columns
341+
var columns = [];
342+
var valueExprs = [];
343+
this.setcolumns.forEach(function (setcol) {
344+
columns.push(setcol.column);
345+
valueExprs.push(setcol.expression);
346+
});
347+
348+
// Temporarily transform to use VALUES path
349+
var originalColumns = this.columns;
350+
var originalValues = this.values;
351+
this.columns = columns;
352+
this.values = [valueExprs];
353+
354+
try {
355+
// Reuse VALUES compilation logic by recursively calling compile
356+
var compiledFn = yy.Insert.prototype.compile.call(this, databaseid);
357+
return compiledFn;
358+
} finally {
359+
// Always restore original state
360+
this.columns = originalColumns;
361+
this.values = originalValues;
362+
}
334363
} else {
335364
throw new Error('Wrong INSERT parameters');
336365
}

src/alasqlparser.jison

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,8 @@ Insert
18641864
{ $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); yy.extend($$,$7); }
18651865
| INSERT Into Table LPAR ColumnsList RPAR Select OutputClause
18661866
{ $$ = new yy.Insert({into:$3, columns: $5, select: $7}); yy.extend($$,$9); }
1867+
| INSERT Into Table SET SetColumnsList OutputClause
1868+
{ $$ = new yy.Insert({into:$3, setcolumns: $5}); yy.extend($$,$6); }
18671869
;
18681870

18691871
Values

0 commit comments

Comments
 (0)