Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/70insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,47 @@
*/

/* global yy alasql*/

// Helper to wrap a statement for ParamValue execution
function wrapParamValueStatement(
originalStatement,
paramIndex,
tableid,
databaseid,
needsSync,
restoreRef,
refKey,
self
) {
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
throw new Error('Operation requires an array for parameter ' + paramIndex);
}

var db = alasql.databases[databaseid];
db.tables[tableid].data = data;

try {
var res = originalStatement(params, cb);

// Sync changes back for operations that replace the array
if (needsSync) {
var newData = db.tables[tableid].data;
data.length = 0;
for (var i = 0; i < newData.length; i++) {
data.push(newData[i]);
}
}

return res;
} finally {
delete db.tables[tableid];
if (restoreRef) self[refKey] = restoreRef;
}
};
}

yy.Insert = function (params) {
return Object.assign(this, params);
};
Expand Down Expand Up @@ -59,10 +100,30 @@ yy.Insert.prototype.toJS = function (context, tableid, defcols) {

yy.Insert.prototype.compile = function (databaseid) {
var self = this;

// Handle ParamValue (anonymous data table) with temporary table approach
var isParamValue = self.into instanceof yy.ParamValue;
var paramIndex = isParamValue ? self.into.param : null;
var originalInto = self.into;

// If ParamValue, temporarily replace with a temp table reference
if (isParamValue) {
var tempTableName =
'__alasql_tmp_' + Date.now() + '_' + Math.random().toString(36).substring(2, 11);
self.into = new yy.Table({tableid: tempTableName});
}

databaseid = self.into.databaseid || databaseid;
var db = alasql.databases[databaseid];
// console.log(self);
var tableid = self.into.tableid;

// For ParamValue, we need to create the temp table before compilation
if (isParamValue) {
db.tables[tableid] = new alasql.Table({tableid: tableid});
db.tables[tableid].data = [];
}

var table = db.tables[tableid];

if (!table) {
Expand Down Expand Up @@ -424,6 +485,20 @@ yy.Insert.prototype.compile = function (databaseid) {
};
}

// If this was a ParamValue, wrap the statement to handle temp table setup/cleanup
if (isParamValue) {
statement = wrapParamValueStatement(
statement,
paramIndex,
tableid,
databaseid,
true, // needsSync - INSERT adds rows
originalInto,
'into',
self
);
}

return statement;
};

Expand Down
73 changes: 73 additions & 0 deletions src/72delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,46 @@
//
*/

// Helper to wrap a statement for ParamValue execution
function wrapParamValueStatement(
originalStatement,
paramIndex,
tableid,
databaseid,
needsSync,
restoreRef,
refKey,
self
) {
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
throw new Error('Operation requires an array for parameter ' + paramIndex);
}

var db = alasql.databases[databaseid];
db.tables[tableid].data = data;

try {
var res = originalStatement(params, cb);

// Sync changes back for operations that replace the array
if (needsSync) {
var newData = db.tables[tableid].data;
data.length = 0;
for (var i = 0; i < newData.length; i++) {
data.push(newData[i]);
}
}

return res;
} finally {
delete db.tables[tableid];
if (restoreRef) self[refKey] = restoreRef;
}
};
}

yy.Delete = function (params) {
return Object.assign(this, params);
};
Expand All @@ -29,11 +69,30 @@ yy.Delete.prototype.toString = function () {

yy.Delete.prototype.compile = function (databaseid) {
var self = this;

// Handle ParamValue (anonymous data table) with temporary table approach
var isParamValue = this.table instanceof yy.ParamValue;
var paramIndex = isParamValue ? this.table.param : null;
var originalTable = this.table;

// If ParamValue, temporarily replace with a temp table reference
if (isParamValue) {
var tempTableName =
'__alasql_tmp_' + Date.now() + '_' + Math.random().toString(36).substring(2, 11);
this.table = new yy.Table({tableid: tempTableName});
}

databaseid = this.table.databaseid || databaseid;
var tableid = this.table.tableid;
var statement;
var db = alasql.databases[databaseid];

// For ParamValue, we need to create the temp table before compilation
if (isParamValue) {
db.tables[tableid] = new alasql.Table({tableid: tableid});
db.tables[tableid].data = [];
}

if (this.where) {
if (this.exists) {
this.existsfn = this.exists.map(function (ex) {
Expand Down Expand Up @@ -211,6 +270,20 @@ yy.Delete.prototype.compile = function (databaseid) {
};
}

// If this was a ParamValue, wrap the statement to handle temp table setup/cleanup
if (isParamValue) {
statement = wrapParamValueStatement(
statement,
paramIndex,
tableid,
databaseid,
true, // needsSync - DELETE removes rows
originalTable,
'table',
self
);
}

return statement;
};

Expand Down
75 changes: 75 additions & 0 deletions src/74update.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@

/* global yy alasql */

// Helper to wrap a statement for ParamValue execution
function wrapParamValueStatement(
originalStatement,
paramIndex,
tableid,
databaseid,
needsSync,
restoreRef,
refKey,
self
) {
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
throw new Error('Operation requires an array for parameter ' + paramIndex);
}

var db = alasql.databases[databaseid];
db.tables[tableid].data = data;

try {
var res = originalStatement(params, cb);

// Sync changes back for operations that replace the array
if (needsSync) {
var newData = db.tables[tableid].data;
data.length = 0;
for (var i = 0; i < newData.length; i++) {
data.push(newData[i]);
}
}

return res;
} finally {
delete db.tables[tableid];
if (restoreRef) self[refKey] = restoreRef;
}
};
}

yy.Update = function (params) {
return Object.assign(this, params);
};
Expand Down Expand Up @@ -40,9 +80,29 @@ yy.SetColumn.prototype.toString = function () {
yy.Update.prototype.compile = function (databaseid) {
var self = this;
// console.log(this);

// Handle ParamValue (anonymous data table) with temporary table approach
var isParamValue = this.table instanceof yy.ParamValue;
var paramIndex = isParamValue ? this.table.param : null;
var originalTable = this.table;

// If ParamValue, temporarily replace with a temp table reference
if (isParamValue) {
var tempTableName =
'__alasql_tmp_' + Date.now() + '_' + Math.random().toString(36).substring(2, 11);
this.table = new yy.Table({tableid: tempTableName});
}

databaseid = this.table.databaseid || databaseid;
var tableid = this.table.tableid;

// For ParamValue, we need to create the temp table before compilation
if (isParamValue) {
var db = alasql.databases[databaseid];
db.tables[tableid] = new alasql.Table({tableid: tableid});
db.tables[tableid].data = [];
}

if (this.where) {
if (this.exists) {
this.existsfn = this.exists.map(function (ex) {
Expand Down Expand Up @@ -163,6 +223,21 @@ yy.Update.prototype.compile = function (databaseid) {
if (cb) cb(res);
return res;
};

// If this was a ParamValue, wrap the statement to handle temp table setup/cleanup
if (isParamValue) {
statement = wrapParamValueStatement(
statement,
paramIndex,
tableid,
databaseid,
false, // needsSync - UPDATE modifies in-place
originalTable,
'table',
self
);
}

return statement;
};

Expand Down
53 changes: 30 additions & 23 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,13 @@ Table
{ $$ = new yy.Table({tableid: $1});}
;

TargetTable
: Table
{ $$ = $1; }
| ParamValue
{ $$ = $1; }
;

JoinTablesList
: JoinTablesList JoinTable
{ $$ = $1; $1.push($2); }
Expand Down Expand Up @@ -1836,9 +1843,9 @@ AllSome
/* UPDATE */

Update
: UPDATE Table SET SetColumnsList WHERE Expression OutputClause
: UPDATE TargetTable SET SetColumnsList WHERE Expression OutputClause
{ $$ = new yy.Update({table:$2, columns:$4, where:$6}); yy.extend($$,$7); }
| UPDATE Table SET SetColumnsList OutputClause
| UPDATE TargetTable SET SetColumnsList OutputClause
{ $$ = new yy.Update({table:$2, columns:$4}); yy.extend($$,$5); }
;

Expand All @@ -1860,52 +1867,52 @@ SetColumn
/* DELETE */

Delete
: DELETE FROM Table WHERE Expression OutputClause
: DELETE FROM TargetTable WHERE Expression OutputClause
{ $$ = new yy.Delete({table:$3, where:$5}); yy.extend($$,$6);}
| DELETE FROM Table OutputClause
| DELETE FROM TargetTable OutputClause
{ $$ = new yy.Delete({table:$3}); yy.extend($$,$4);}
;

/* INSERT */

Insert
: INSERT Into Table Values ValuesListsList OutputClause
: INSERT Into TargetTable Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, values: $5}); yy.extend($$,$6); }
| INSERT Into Table ValuesListsList OutputClause
| INSERT Into TargetTable ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, values: $4}); yy.extend($$,$5); }
| INSERT IGNORE Into Table Values ValuesListsList OutputClause
| INSERT IGNORE Into TargetTable Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$4, values: $6, ignore:true}); yy.extend($$,$7); }
| INSERT IGNORE Into Table ValuesListsList OutputClause
| INSERT IGNORE Into TargetTable ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$4, values: $5, ignore:true}); yy.extend($$,$6); }
| INSERT IGNORE Into Table LPAR ColumnsList RPAR Values ValuesListsList OutputClause
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$4, columns: $6, values: $9, ignore:true}); yy.extend($$,$10); }
| INSERT IGNORE Into Table LPAR ColumnsList RPAR ValuesListsList OutputClause
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$4, columns: $6, values: $8, ignore:true}); yy.extend($$,$9); }
| INSERT IGNORE Into Table Select OutputClause
| INSERT IGNORE Into TargetTable Select OutputClause
{ $$ = new yy.Insert({into:$4, select: $5, ignore:true}); yy.extend($$,$6); }
| INSERT IGNORE Into Table LPAR ColumnsList RPAR Select OutputClause
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Select OutputClause
{ $$ = new yy.Insert({into:$4, columns: $6, select: $8, ignore:true}); yy.extend($$,$9); }
| INSERT OR REPLACE Into Table Values ValuesListsList OutputClause
| INSERT OR REPLACE Into TargetTable Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$5, values: $7, orreplace:true}); yy.extend($$,$8); }
| INSERT OR REPLACE Into Table ValuesListsList OutputClause
| INSERT OR REPLACE Into TargetTable ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$5, values: $6, orreplace:true}); yy.extend($$,$7); }
| REPLACE Into Table Values ValuesListsList OutputClause
| REPLACE Into TargetTable Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, values: $5, orreplace:true}); yy.extend($$,$6); }
| REPLACE Into Table ValuesListsList OutputClause
| REPLACE Into TargetTable ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, values: $4, orreplace:true}); yy.extend($$,$5); }
| INSERT Into Table DEFAULT Values OutputClause
| INSERT Into TargetTable DEFAULT Values OutputClause
{ $$ = new yy.Insert({into:$3, "default": true}); yy.extend($$,$6); }
| INSERT Into Table LPAR ColumnsList RPAR Values ValuesListsList OutputClause
| INSERT Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, columns: $5, values: $8}); yy.extend($$,$9); }
| INSERT Into Table LPAR ColumnsList RPAR ValuesListsList OutputClause
| INSERT Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
{ $$ = new yy.Insert({into:$3, columns: $5, values: $7}); yy.extend($$,$9); }
| INSERT Into Table Select OutputClause
| INSERT Into TargetTable Select OutputClause
{ $$ = new yy.Insert({into:$3, select: $4}); yy.extend($$,$5); }
| INSERT OR REPLACE Into Table Select OutputClause
| INSERT OR REPLACE Into TargetTable Select OutputClause
{ $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); yy.extend($$,$7); }
| INSERT Into Table LPAR ColumnsList RPAR Select OutputClause
| INSERT Into TargetTable LPAR ColumnsList RPAR Select OutputClause
{ $$ = new yy.Insert({into:$3, columns: $5, select: $7}); yy.extend($$,$9); }
| INSERT Into Table SET SetColumnsList OutputClause
| INSERT Into TargetTable SET SetColumnsList OutputClause
{ $$ = new yy.Insert({into:$3, setcolumns: $5}); yy.extend($$,$6); }
;

Expand Down
Loading