Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
41 changes: 41 additions & 0 deletions src/70insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ yy.Insert.prototype.toJS = function (context, tableid, defcols) {

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

// Handle ParamValue (anonymous data table) - wrap execution
if (self.into instanceof yy.ParamValue) {
var paramIndex = self.into.param;
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
var err = new Error('INSERT requires an array for parameter ' + paramIndex);
if (cb) return cb(null, err);
throw err;
}

// Create temp table, execute, sync, cleanup
var tmpid = '__p' + paramIndex + '_' + Date.now();
var db = alasql.databases[databaseid || 'alasql'];
db.tables[tmpid] = new alasql.Table({tableid: tmpid});
db.tables[tmpid].data = data;

try {
var origInto = self.into;
self.into = new yy.Table({tableid: tmpid, databaseid: db.databaseid});
var stmt = self.compile(databaseid);
self.into = origInto;

var res = stmt(params, cb);

// Sync back changes
var newData = db.tables[tmpid].data;
data.length = 0;
Array.prototype.push.apply(data, newData);

return res;
} catch (err) {
if (cb) return cb(null, err);
throw err;
} finally {
delete db.tables[tmpid];
}
};
}

databaseid = self.into.databaseid || databaseid;
var db = alasql.databases[databaseid];
// console.log(self);
Expand Down
41 changes: 41 additions & 0 deletions src/72delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,47 @@ yy.Delete.prototype.toString = function () {

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

// Handle ParamValue (anonymous data table) - wrap execution
if (this.table instanceof yy.ParamValue) {
var paramIndex = this.table.param;
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
var err = new Error('DELETE requires an array for parameter ' + paramIndex);
if (cb) return cb(null, err);
throw err;
}

// Create temp table, execute, sync, cleanup
var tmpid = '__p' + paramIndex + '_' + Date.now();
var db = alasql.databases[databaseid || 'alasql'];
db.tables[tmpid] = new alasql.Table({tableid: tmpid});
db.tables[tmpid].data = data;

try {
var origTable = self.table;
self.table = new yy.Table({tableid: tmpid, databaseid: db.databaseid});
var stmt = self.compile(databaseid);
self.table = origTable;

var res = stmt(params, cb);

// Sync back changes
var newData = db.tables[tmpid].data;
data.length = 0;
Array.prototype.push.apply(data, newData);

return res;
} catch (err) {
if (cb) return cb(null, err);
throw err;
} finally {
delete db.tables[tmpid];
}
};
}

databaseid = this.table.databaseid || databaseid;
var tableid = this.table.tableid;
var statement;
Expand Down
34 changes: 34 additions & 0 deletions src/74update.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ yy.SetColumn.prototype.toString = function () {
yy.Update.prototype.compile = function (databaseid) {
var self = this;
// console.log(this);

// Handle ParamValue (anonymous data table) - wrap execution
if (this.table instanceof yy.ParamValue) {
var paramIndex = this.table.param;
return function (params, cb) {
var data = params[paramIndex];
if (!Array.isArray(data)) {
var err = new Error('UPDATE requires an array for parameter ' + paramIndex);
if (cb) return cb(null, err);
throw err;
}

// Create temp table, execute, cleanup (no sync needed - UPDATE modifies in place)
var tmpid = '__p' + paramIndex + '_' + Date.now();
var db = alasql.databases[databaseid || 'alasql'];
db.tables[tmpid] = new alasql.Table({tableid: tmpid});
db.tables[tmpid].data = data;

try {
var origTable = self.table;
self.table = new yy.Table({tableid: tmpid, databaseid: db.databaseid});
var stmt = self.compile(databaseid);
self.table = origTable;

return stmt(params, cb);
} catch (err) {
if (cb) return cb(null, err);
throw err;
} finally {
delete db.tables[tmpid];
}
};
}

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

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