@@ -23,6 +23,18 @@ yy.Insert.prototype.toString = function () {
2323 s += ' VALUES ' + values . join ( ',' ) ;
2424 }
2525 if ( this . select ) s += ' ' + this . select . toString ( ) ;
26+ if ( this . output ) {
27+ s += ' OUTPUT ' ;
28+ s += this . output . columns . map ( col => col . toString ( ) ) . join ( ', ' ) ;
29+ if ( this . output . intovar ) {
30+ s += ' INTO ' + this . output . method + this . output . intovar ;
31+ } else if ( this . output . intotable ) {
32+ s += ' INTO ' + this . output . intotable . toString ( ) ;
33+ if ( this . output . intocolumns ) {
34+ s += '(' + this . output . intocolumns . map ( col => col . toString ( ) ) . join ( ', ' ) + ')' ;
35+ }
36+ }
37+ }
2638 return s ;
2739} ;
2840
@@ -56,6 +68,7 @@ yy.Insert.prototype.compile = function (databaseid) {
5668 var s = '' ;
5769 var sw = '' ;
5870 var s = "db.tables['" + tableid + "'].dirty=true;" ;
71+ // aa = array to accumulate inserted rows (used for OUTPUT clause and concat to table.data)
5972 var s3 = 'var a,aa=[],x;' ;
6073
6174 var s33 ;
@@ -191,6 +204,10 @@ yy.Insert.prototype.compile = function (databaseid) {
191204 if ( db . tables [ tableid ] . insert ) {
192205 s += "var db=alasql.databases['" + databaseid + "'];" ;
193206 s += "db.tables['" + tableid + "'].insert(a," + ( self . orreplace ? 'true' : 'false' ) + ');' ;
207+ // Also push to aa for OUTPUT clause
208+ if ( self . output ) {
209+ s += 'aa.push(a);' ;
210+ }
194211 } else {
195212 s += 'aa.push(a);' ;
196213 }
@@ -214,7 +231,27 @@ yy.Insert.prototype.compile = function (databaseid) {
214231 "'].data.concat(aa);" ;
215232 }
216233
217- if ( db . tables [ tableid ] . insert ) {
234+ // Handle OUTPUT clause
235+ if ( self . output ) {
236+ s += 'var output = [];' ;
237+ s += 'for(var i=0;i<aa.length;i++){' ;
238+ s += 'var r = aa[i];' ;
239+ s += 'var outputRow = {};' ;
240+ // Process each output column
241+ self . output . columns . forEach ( function ( col ) {
242+ if ( col . columnid === '*' ) {
243+ // For *, expand all properties
244+ s += 'for(var key in r){ outputRow[key] = r[key]; }' ;
245+ } else {
246+ var colname = col . as || col . columnid ;
247+ // Direct property access for simple columns
248+ s += "outputRow['" + colname + "']=r['" + col . columnid + "'];" ;
249+ }
250+ } ) ;
251+ s += 'output.push(outputRow);' ;
252+ s += '}' ;
253+ s += 'return output;' ;
254+ } else if ( db . tables [ tableid ] . insert ) {
218255 if ( db . tables [ tableid ] . isclass ) {
219256 s += 'return a.$id;' ;
220257 } else {
@@ -250,16 +287,43 @@ yy.Insert.prototype.compile = function (databaseid) {
250287 var defaultfn = new Function ( 'r,db,params,alasql' , defaultfns ) ;
251288 var insertfn = function ( db , params , alasql ) {
252289 var res = selectfn ( params ) . data ;
290+ var insertedRows = [ ] ;
253291 if ( db . tables [ tableid ] . insert ) {
254292 // If insert() function exists (issue #92)
255293 for ( var i = 0 , ilen = res . length ; i < ilen ; i ++ ) {
256294 var r = cloneDeep ( res [ i ] ) ;
257295 defaultfn ( r , db , params , alasql ) ;
258296 db . tables [ tableid ] . insert ( r , self . orreplace ) ;
297+ insertedRows . push ( r ) ;
259298 }
260299 } else {
300+ insertedRows = res ;
261301 db . tables [ tableid ] . data = db . tables [ tableid ] . data . concat ( res ) ;
262302 }
303+
304+ // Handle OUTPUT clause
305+ if ( self . output ) {
306+ var output = [ ] ;
307+ for ( var i = 0 ; i < insertedRows . length ; i ++ ) {
308+ var r = insertedRows [ i ] ;
309+ var outputRow = { } ;
310+ self . output . columns . forEach ( function ( col ) {
311+ if ( col . columnid === '*' ) {
312+ // For *, expand all properties
313+ for ( var key in r ) {
314+ outputRow [ key ] = r [ key ] ;
315+ }
316+ } else {
317+ var colname = col . as || col . columnid ;
318+ // Direct property access for simple columns
319+ outputRow [ colname ] = r [ col . columnid ] ;
320+ }
321+ } ) ;
322+ output . push ( outputRow ) ;
323+ }
324+ return output ;
325+ }
326+
263327 if ( alasql . options . nocount ) return ;
264328 else return res . length ;
265329 } ;
0 commit comments