alasql
Version:
AlaSQL.js - JavaScript SQL database library for relational and graph data manipulation with support of localStorage, IndexedDB, and Excel
76 lines (70 loc) • 2.27 kB
JavaScript
/*
//
// SET for Alasql.js
// Date: 01.12.2014
// (c) 2014, Andrey Gershun
//
*/
yy.SetVariable = function (params) { return yy.extend(this, params); }
yy.SetVariable.prototype.toString = function() {
var s = K('SET')+' ';
if(typeof this.value != 'undefined') s += K(this.variable.toUpperCase())+' '+(this.value?'ON':'OFF');
if(this.expression) s += '@' + L(this.variable)+' = '+this.expression.toString();
return s;
}
yy.SetVariable.prototype.execute = function (databaseid,params,cb) {
// console.log(this);
if(typeof this.value != 'undefined') {
var val = this.value;
if(val == 'ON') val = true;
else if(val == 'OFF') val = false;
alasql.options[this.variable] = val;
} else if(this.expression) {
if(this.exists) {
this.existsfn = this.exists.map(function(ex) {
var nq = ex.compile(databaseid);
if(nq.query && !nq.query.modifier) nq.query.modifier='ARRAY';
return nq;
// return ex.compile(databaseid);
// TODO Include modifier
});
}
if(this.queries) {
this.queriesfn = this.queries.map(function(q) {
var nq = q.compile(databaseid);
if(nq.query && !nq.query.modifier) nq.query.modifier='ARRAY';
return nq;
// TODO Include modifier
});
}
// console.log(this.expression.toJavaScript('','', null));
var res = new Function("params,alasql","return "
+this.expression.toJavaScript('({})','', null)).bind(this)(params,alasql);
if(alasql.declares[this.variable]) {
res = alasql.stdfn.CONVERT(res,alasql.declares[this.variable]);
}
if(this.props && this.props.length > 0) {
var fs = 'alasql.vars[\''+this.variable+'\']';
fs += this.props.map(function(prop){
if(typeof prop == 'string') {
return '[\''+prop+'\']';
} else if(typeof prop == 'number') {
return '['+prop+']';
} else {
// console.log('prop:',prop, prop.toJavaScript());
return '['+prop.toJavaScript()+']';
// } else {
// console.log(prop, typeof );
// throw new Error('Wrong SET property');
}
}).join();
// console.log(fs);
new Function("value,alasql",fs +'=value')(res,alasql);
} else {
alasql.vars[this.variable] = res;
}
}
var res = 1;
if(cb) res=cb(res);
return res;
};