Skip to content

Commit

Permalink
SEARCH SET()
Browse files Browse the repository at this point in the history
  • Loading branch information
agershun committed May 5, 2015
1 parent 3cee863 commit c476c8c
Show file tree
Hide file tree
Showing 10 changed files with 1,536 additions and 1,386 deletions.
941 changes: 486 additions & 455 deletions alasql.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions console/alasql.min.js

Large diffs are not rendered by default.

941 changes: 486 additions & 455 deletions dist/alasql.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alasql.js.map

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions dist/alasql.min.js

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/35search.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ yy.Search.prototype.execute = function (databaseid, params, cb) {


} else if(sel.srchid) {
var r = alasql.srch[sel.srchid.toUpperCase()](value,sel.args,stope);
var r = alasql.srch[sel.srchid.toUpperCase()](value,sel.args,stope,params);
} else {
throw new Error('Selector not found');
}
Expand Down Expand Up @@ -413,7 +413,35 @@ alasql.srch.AS = function(val,args) {
return {status: 1, values: [val]};
};

// Transform expression
alasql.srch.CLONEDEEP = function(val,args) {
// TODO something wrong
var z = cloneDeep(val);
return {status: 1, values: [z]};
};

// // Transform expression
// alasql.srch.DELETE = function(val,args) {
// // TODO something wrong
// delete val;
// return {status: 1, values: []};
// };


alasql.srch.TO = function(val,args) {
alasql.vars[args[0]].push(val);
return {status: 1, values: [val]};
};

// Transform expression
alasql.srch.SET = function(val,args,stope,params) {
// console.log(arguments);
var s = args.map(function(st){
return 'x[\''+st.column.columnid+'\']='+st.expression.toJavaScript('x','');
}).join(';');
var setfn = new Function('x,params,alasql',s);

setfn(val,params,alasql);

return {status: 1, values: [val]};
};
6 changes: 5 additions & 1 deletion src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,9 @@ SearchSelector
{ $$ = {srchid:"IN"}; }
| DOLLAR
{ $$ = {srchid:"CONTENT"}; }
| DOT DOT
/* | DELETE LPAR RPAR
{ $$ = {srchid:"DELETE"}; }
*/ | DOT DOT
{ $$ = {srchid:"PARENT"}; }
| Json
{ $$ = {srchid:"EX",args:[new yy.Json({value:$1})]}; }
Expand All @@ -552,6 +554,8 @@ SearchSelector
{ $$ = {srchid:"AS", args:[$3]}; }
| TO AT Literal
{ $$ = {srchid:"TO", args:[$3]}; }
| SET LPAR SetColumnsList RPAR
{ $$ = {srchid:"SET", args:$3}; }

| LPAR SearchSelector* RPAR PlusStar
{ $$ = {selid:$4,args:$2 }; }
Expand Down
911 changes: 457 additions & 454 deletions src/alasqlparser.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion test/test306.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe('Test 306 XML reader', function() {

it('7. SEARCH INTO ',function(done){
alasql('SEARCH XML /graph/edges/% INTO CSV({headers:true}) FROM XML("test306a.xml")',[],function(res){
console.log(res);
// console.log('>>',res,'<<');
assert(res == "id,source,target\n0,0,1\n");
// assert.deepEqual(res, [ { id: '0', source: '0', target: '1' } ]);
done();
});
Expand Down
52 changes: 52 additions & 0 deletions test/test307.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
if(typeof exports === 'object') {
var assert = require("assert");
var alasql = require('..');
} else {
__dirname = '.';
};

describe('Test 307 special selectors', function() {

it('0. Create database ',function(done){
alasql('CREATE DATABASE test307;USE test307');
done();
});


it('1. SET selector',function(done){
var data = [{a:1,b:10},{a:2,b:20}];
var res = alasql('SEARCH / set(b=a*3) FROM ?',[data]);
// console.log(res);
// console.log(data);
assert.deepEqual(res,[ { a: 1, b: 3 }, { a: 2, b: 6 } ]);
assert.deepEqual(data,[ { a: 1, b: 3 }, { a: 2, b: 6 } ]);
done();
});

it('2. SET selector',function(done){
var data = [{a:1,b:10},{a:2,b:20}];
var res = alasql('SEARCH / clonedeep() set(b=a*3) FROM ?',[data]);
// console.log(res);
// console.log(data);
assert.deepEqual(res,[ { a: 1, b: 3 }, { a: 2, b: 6 } ]);
assert.deepEqual(data,[ { a: 1, b: 10 }, { a: 2, b: 20 } ]);
done();
});

// it('3. DELETE selector',function(done){
// var data = [{a:1,b:10},{a:2,b:20}];
// var res = alasql('SEARCH / ok(a=1) FROM ?',[data]);
// console.log(res);
// console.log(data);
// // assert.deepEqual(res,[ { a: 1, b: 3 }, { a: 2, b: 6 } ]);
// // assert.deepEqual(data,[ { a: 1, b: 10 }, { a: 2, b: 20 } ]);
// done();
// });

it('99. Drop database ',function(done){
alasql('DROP DATABASE test307');
done();
});

});

0 comments on commit c476c8c

Please sign in to comment.