src/38query.js
function queryfn(query, oldscope, cb, A, B) {
	var aaa = query.sources.length;

	var ms;
	query.sourceslen = query.sources.length;
	var slen = query.sourceslen;
	query.query = query; // TODO Remove to prevent memory leaks
	query.A = A;
	query.B = B;
	query.cb = cb;
	query.oldscope = oldscope;

	// Run all subqueries before main statement
	if (query.queriesfn) {
		query.sourceslen += query.queriesfn.length;
		slen += query.queriesfn.length;

		query.queriesdata = [];

		//		console.log(8);
		query.queriesfn.forEach(function(q, idx) {
			//			if(query.explain) ms = Date.now();
			//console.log(18,idx);
			//			var res = flatArray(q(query.params,null,queryfn2,(-idx-1),query));

			//			var res = flatArray(queryfn(q.query,null,queryfn2,(-idx-1),query));
			//			console.log(A,B);
			// console.log(q);
			q.query.params = query.params;
			//			query.queriesdata[idx] =

			//	if(false) {
			//			queryfn(q.query,query.oldscope,queryfn2,(-idx-1),query);
			//	} else {
			queryfn2([], -idx - 1, query);
			//	}

			//			console.log(27,q);

			//			query.explaination.push({explid: query.explid++, description:'Query '+idx,ms:Date.now()-ms});
			//			query.queriesdata[idx] = res;
			//			return res;
		});
		//		console.log(9,query.queriesdata.length);
		//		console.log(query.queriesdata[0]);
	}

	var scope;
	if (!oldscope) scope = {};
	else scope = cloneDeep(oldscope);
	query.scope = scope;

	// First - refresh data sources

	var result;
	query.sources.forEach(function(source, idx) {
		//		source.data = query.database.tables[source.tableid].data;
		//		console.log(666,idx);
		source.query = query;
		var rs = source.datafn(query, query.params, queryfn2, idx, alasql);
		//		console.log(333,rs);
		if (typeof rs !== 'undefined') {
			// TODO - this is a hack: check if result is array - check all cases and
			// make it more logical
			if ((query.intofn || query.intoallfn) && Array.isArray(rs)) rs = rs.length;
			result = rs;
		}
		//
		// Ugly hack to use in query.wherefn and source.srcwherefns functions
		// constructions like this.queriesdata['test'].
		// I can elimite it with source.srcwherefn.bind(this)()
		// but it may be slow.
		//
		source.queriesdata = query.queriesdata;
	});
	if (query.sources.length == 0 || 0 === slen) result = queryfn3(query);
	//	console.log(82,aaa,slen,query.sourceslen, query.sources.length);
	return result;
}
function queryfn(query, oldscope, cb, A, B) {
	var aaa = query.sources.length;

	var ms;
	query.sourceslen = query.sources.length;
	var slen = query.sourceslen;
	query.query = query; // TODO Remove to prevent memory leaks
	query.A = A;
	query.B = B;
	query.cb = cb;
	query.oldscope = oldscope;

	// Run all subqueries before main statement
	if (query.queriesfn) {
		query.sourceslen += query.queriesfn.length;
		slen += query.queriesfn.length;

		query.queriesdata = [];

		//		console.log(8);
		query.queriesfn.forEach(function(q, idx) {
			//			if(query.explain) ms = Date.now();
			//console.log(18,idx);
			//			var res = flatArray(q(query.params,null,queryfn2,(-idx-1),query));

			//			var res = flatArray(queryfn(q.query,null,queryfn2,(-idx-1),query));
			//			console.log(A,B);
			// console.log(q);
			q.query.params = query.params;
			//			query.queriesdata[idx] =

			//	if(false) {
			//			queryfn(q.query,query.oldscope,queryfn2,(-idx-1),query);
			//	} else {
			queryfn2([], -idx - 1, query);
			//	}

			//			console.log(27,q);

			//			query.explaination.push({explid: query.explid++, description:'Query '+idx,ms:Date.now()-ms});
			//			query.queriesdata[idx] = res;
			//			return res;
		});
		//		console.log(9,query.queriesdata.length);
		//		console.log(query.queriesdata[0]);
	}

	var scope;
	if (!oldscope) scope = {};
	else scope = cloneDeep(oldscope);
	query.scope = scope;

	// First - refresh data sources

	var result;
	query.sources.forEach(function(source, idx) {
		//		source.data = query.database.tables[source.tableid].data;
		//		console.log(666,idx);
		source.query = query;
		var rs = source.datafn(query, query.params, queryfn2, idx, alasql);
		//		console.log(333,rs);
		if (typeof rs !== 'undefined') {
			// TODO - this is a hack: check if result is array - check all cases and
			// make it more logical
			if ((query.intofn || query.intoallfn) && Array.isArray(rs)) rs = rs.length;
			result = rs;
		}
		//
		// Ugly hack to use in query.wherefn and source.srcwherefns functions
		// constructions like this.queriesdata['test'].
		// I can elimite it with source.srcwherefn.bind(this)()
		// but it may be slow.
		//
		source.queriesdata = query.queriesdata;
	});
	if (query.sources.length == 0 || 0 === slen) result = queryfn3(query);
	//	console.log(82,aaa,slen,query.sourceslen, query.sources.length);
	return result;
}
function queryfn ( query , oldscope , cb , A , B ) { var aaa = query . sources . length ; var ms ; query . sourceslen = query . sources . length ; var slen = query . sourceslen ; query . query = query ; // TODO Remove to prevent memory leaks
 query . A = A ; query . B = B ; query . cb = cb ; query . oldscope = oldscope ; // Run all subqueries before main statement
 if ( query . queriesfn ) { query . sourceslen += query . queriesfn . length ; slen += query . queriesfn . length ; query . queriesdata = [ ] ; //		console.log(8);
 query . queriesfn . forEach ( function ( q , idx ) { //			if(query.explain) ms = Date.now();
 //console.log(18,idx);
 //			var res = flatArray(q(query.params,null,queryfn2,(-idx-1),query));
 //			var res = flatArray(queryfn(q.query,null,queryfn2,(-idx-1),query));
 //			console.log(A,B);
 // console.log(q);
 q . query . params = query . params ; //			query.queriesdata[idx] =
 //	if(false) {
 //			queryfn(q.query,query.oldscope,queryfn2,(-idx-1),query);
 //	} else {
 queryfn2 ( [ ] , - idx - 1 , query ) ; //	}
 //			console.log(27,q);
 //			query.explaination.push({explid: query.explid++, description:'Query '+idx,ms:Date.now()-ms});
 //			query.queriesdata[idx] = res;
 //			return res;
 } ) ; //		console.log(9,query.queriesdata.length);
 //		console.log(query.queriesdata[0]);
 } var scope ; if ( ! oldscope ) scope = { } ; else scope = cloneDeep ( oldscope ) ; query . scope = scope ; // First - refresh data sources
 var result ; query . sources . forEach ( function ( source , idx ) { //		source.data = query.database.tables[source.tableid].data;
 //		console.log(666,idx);
 source . query = query ; var rs = source . datafn ( query , query . params , queryfn2 , idx , alasql ) ; //		console.log(333,rs);
 if ( typeof rs !== 'undefined' ) { // TODO - this is a hack: check if result is array - check all cases and
 // make it more logical
 if ( ( query . intofn || query . intoallfn ) && Array . isArray ( rs ) ) rs = rs . length ; result = rs ; } //
 // Ugly hack to use in query.wherefn and source.srcwherefns functions
 // constructions like this.queriesdata['test'].
 // I can elimite it with source.srcwherefn.bind(this)()
 // but it may be slow.
 //
 source . queriesdata = query . queriesdata ; } ) ; if ( query . sources . length == 0 || 0 === slen ) result = queryfn3 ( query ) ; //	console.log(82,aaa,slen,query.sourceslen, query.sources.length);
 return result ; }
Main query procedure
Main query procedure