Skip to content

Commit b6a68cf

Browse files
Copilotmathiasrw
andauthored
Add support for SEARCH INTO $variable to close #2363 (#2375)
Fixed SELECT INTO $variable (src/40select.js) - Modified ParamValue INTO handling to initialize array and set whole data array - Changed from per-row push to full array assignment - Now properly handles `SELECT * INTO $var FROM ...` syntax Added SEARCH INTO $variable support (src/35search.js) - Extended SEARCH statement to support ParamValue and VarValue in INTO clause - Previously only supported FuncValue (INTO TXT(), INTO JSON(), etc.) - Now supports: - `SEARCH ... INTO $variable FROM ...` (ParamValue with string param) - `SEARCH ... INTO ? FROM ...` (ParamValue with numeric param) - `SEARCH ... INTO @variable FROM ...` (VarValue) - Maintains backward compatibility with function-based INTO --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent 9d63861 commit b6a68cf

File tree

6 files changed

+69
-37
lines changed

6 files changed

+69
-37
lines changed

src/35search.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -690,20 +690,38 @@ yy.Search = class Search {
690690
}
691691

692692
if (this.into) {
693-
var a1, a2;
694-
if (typeof this.into.args[0] !== 'undefined') {
695-
a1 = new Function('params,alasql', 'var y;return ' + this.into.args[0].toJS())(
696-
params,
697-
alasql
698-
);
699-
}
700-
if (typeof this.into.args[1] !== 'undefined') {
701-
a2 = new Function('params,alasql', 'var y;return ' + this.into.args[1].toJS())(
702-
params,
703-
alasql
704-
);
693+
// Handle different INTO types
694+
if (this.into instanceof yy.ParamValue) {
695+
// INTO $variable or INTO ?
696+
if (typeof this.into.param === 'string') {
697+
// $variable syntax - replace the variable
698+
params[this.into.param] = res;
699+
} else {
700+
// ? syntax - assign to parameter
701+
params[this.into.param] = res;
702+
}
703+
if (cb) res = cb(res);
704+
} else if (this.into instanceof yy.VarValue) {
705+
// INTO @variable
706+
alasql.vars[this.into.variable] = res;
707+
if (cb) res = cb(res);
708+
} else {
709+
// INTO function (TXT(), JSON(), etc.)
710+
var a1, a2;
711+
if (typeof this.into.args[0] !== 'undefined') {
712+
a1 = new Function('params,alasql', 'var y;return ' + this.into.args[0].toJS())(
713+
params,
714+
alasql
715+
);
716+
}
717+
if (typeof this.into.args[1] !== 'undefined') {
718+
a2 = new Function('params,alasql', 'var y;return ' + this.into.args[1].toJS())(
719+
params,
720+
alasql
721+
);
722+
}
723+
res = alasql.into[this.into.funcid.toUpperCase()](a1, a2, res, [], cb);
705724
}
706-
res = alasql.into[this.into.funcid.toUpperCase()](a1, a2, res, [], cb);
707725
} else {
708726
if (stope.value && res.length > 0) {
709727
res = res[0];

src/40select.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,22 @@ yy.Select = class Select {
344344
//
345345
// Save data into parameters array
346346
// like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
347+
// or SELECT * INTO $variable FROM ?
347348
//
348-
query.intofns = `params[${JSON.stringify(this.into.param)}].push(r)`;
349+
// Distinguish between ? (numeric param - push to array) and $variable (string param - replace array)
350+
if (typeof this.into.param === 'string') {
351+
// $variable syntax - replace the array
352+
query.intoallfns = `
353+
if(!params[${JSON.stringify(this.into.param)}]) params[${JSON.stringify(this.into.param)}]=[];
354+
params[${JSON.stringify(this.into.param)}]=this.data;
355+
res=this.data.length;
356+
if(cb) res = cb(res);
357+
return res;
358+
`;
359+
} else {
360+
// ? syntax - push to existing array
361+
query.intofns = `params[${JSON.stringify(this.into.param)}].push(r)`;
362+
}
349363
}
350364

351365
if (query.intofns) {

test/test2169.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function runTest(testName, sql, params = [testData]) {
3838
}
3939
}
4040

41-
describe.skip('Test 2169: Comprehensive verification of compileToJS functionality', function () {
41+
describe('Test 2169: Comprehensive verification of compileToJS functionality', function () {
4242
it('1. Basic SELECT', function () {
4343
runTest('Basic SELECT', 'SELECT name, age FROM ?');
4444
});

test/test336.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ if (typeof exports === 'object') {
1010
//http://stackoverflow.com/questions/18811265/sql-creating-temporary-variables
1111
//
1212
describe('Test 336 SLT test #4', function () {
13-
it.skip('1. CREATE DATABASE', function (done) {
13+
it('1. CREATE DATABASE', function (done) {
1414
alasql('CREATE DATABASE test336;USE test336');
1515

1616
done();
1717
});
1818

19-
it.skip('2. Create table', function (done) {
19+
it('2. Create table', function (done) {
2020
var res = alasql(function () {
2121
/*
2222
CREATE TABLE t1(
@@ -33,13 +33,13 @@ describe('Test 336 SLT test #4', function () {
3333
done();
3434
});
3535

36-
it.skip('3. INSERT some data', function (done) {
36+
it('3. INSERT some data', function (done) {
3737
var res = alasql(function () {
3838
/*
3939
INSERT INTO t1 VALUES(382,414,67,992,483,'table tn1 row 1');
40-
-- INSERT INTO t1 VALUES(231,468,97,414,795,'table tn1 row 2');
41-
-- INSERT INTO t1 VALUES(810,355,805,274,858,'table tn1 row 3');
42-
-- INSERT INTO t1 VALUES(536,956,417,418,381,'table tn1 row 4');
40+
INSERT INTO t1 VALUES(231,468,97,414,795,'table tn1 row 2');
41+
INSERT INTO t1 VALUES(810,355,805,274,858,'table tn1 row 3');
42+
INSERT INTO t1 VALUES(536,956,417,418,381,'table tn1 row 4');
4343
*/
4444
});
4545
// console.log(res);
@@ -48,7 +48,7 @@ describe('Test 336 SLT test #4', function () {
4848
done();
4949
});
5050

51-
it.skip('3. CREATE INDEX', function (done) {
51+
it('3. CREATE INDEX', function (done) {
5252
var res = alasql(function () {
5353
/*
5454
CREATE INDEX t1i0 ON t1(a1,b1,c1,d1,e1,x1);
@@ -64,7 +64,7 @@ describe('Test 336 SLT test #4', function () {
6464
done();
6565
});
6666

67-
it.skip('99. DROP DATABASE', function (done) {
67+
it('99. DROP DATABASE', function (done) {
6868
alasql('DROP DATABASE test336');
6969
done();
7070
});

test/test343.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,54 @@ if (typeof exports === 'object') {
66
}
77

88
describe('Test 343 Use params for $variables', function () {
9-
it.skip('1. CREATE DATABASE', function (done) {
9+
it('1. CREATE DATABASE', function (done) {
1010
alasql('CREATE DATABASE test343;USE test343');
1111
done();
1212
});
1313

14-
it.skip('2. Simple get undefined', function (done) {
14+
it('2. Simple get undefined', function (done) {
1515
var res = alasql('=$a');
1616
assert.deepEqual(res, undefined);
1717
done();
1818
});
1919

20-
it.skip('3. Simple get from empty param {}', function (done) {
20+
it('3. Simple get from empty param {}', function (done) {
2121
var res = alasql('=$a', {});
2222
assert.deepEqual(res, undefined);
2323
done();
2424
});
2525

26-
it.skip('4. Simple get from empty param {}', function (done) {
26+
it('4. Simple get from empty param {}', function (done) {
2727
var params = {a: 123};
2828
var res = alasql('=$a', params);
2929
assert.deepEqual(res, 123);
3030
done();
3131
});
3232

33-
it.skip('5. Simple set to param', function (done) {
33+
it('5. Simple set to param', function (done) {
3434
var params = {a: 123};
3535
var res = alasql('SET $a = $a + 100', params);
3636
assert.deepEqual(params.a, 223);
3737
done();
3838
});
3939

40-
it.skip('6. SELECT INTO $var', function (done) {
40+
it('6. SELECT INTO $var', function (done) {
4141
var params = {};
4242
params.data = [{v: 1}, {v: 2}, {v: 3}];
4343
var res = alasql('SELECT * INTO $arr FROM $data', params);
4444
assert.deepEqual(params.arr, [{v: 1}, {v: 2}, {v: 3}]);
4545
done();
4646
});
4747

48-
it.skip('6. SEARCH AS $var', function (done) {
48+
it('6. SEARCH INTO $var', function (done) {
4949
var params = {};
5050
params.data = [{v: 1}, {v: 2}, {v: 3}];
51-
var res = alasql('SEARCH /v AS $vres FROM $data', params);
52-
assert.deepEqual(params.vres, 3);
51+
var res = alasql('SEARCH /v INTO $vres FROM $data', params);
52+
assert.deepEqual(params.vres, [1, 2, 3]);
5353
done();
5454
});
5555

56-
it.skip('99. DROP DATABASE', function (done) {
56+
it('99. DROP DATABASE', function (done) {
5757
alasql.options.modifier = undefined;
5858
alasql('DROP DATABASE test343');
5959
done();

test/test360.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (typeof exports === 'object') {
66
}
77

88
describe('Test 360 AGGR function', function () {
9-
it.skip('1. CREATE DATABASE', function (done) {
9+
it('1. CREATE DATABASE', function (done) {
1010
alasql('CREATE DATABASE test360;USE test360');
1111
done();
1212
});
@@ -564,7 +564,7 @@ describe('Test 360 AGGR function', function () {
564564
},
565565
];
566566

567-
it.skip('2. Prepare Data', function (done) {
567+
it('2. Prepare Data without GROUP BY', function (done) {
568568
var res = alasql(
569569
function () {
570570
/*
@@ -591,7 +591,7 @@ FROM ?
591591
done();
592592
});
593593

594-
it.skip('2. Prepare Data', function (done) {
594+
it('3. Prepare Data with GROUP BY', function (done) {
595595
var res = alasql(
596596
function () {
597597
/*
@@ -613,7 +613,7 @@ FROM ?
613613
done();
614614
});
615615

616-
it.skip('99. DROP DATABASE', function (done) {
616+
it('99. DROP DATABASE', function (done) {
617617
alasql.options.modifier = undefined;
618618
alasql('DROP DATABASE test360');
619619
done();

0 commit comments

Comments
 (0)