diff --git a/src/55functions.js b/src/55functions.js index 23294e3e6f..5678f3758a 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -445,7 +445,7 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - return (target || '').split(pattern).join(replacement); + return (target.toString() || '').split(pattern).join(replacement); }; // This array is required for fast GUID generation diff --git a/test/test1455.js b/test/test1455.js new file mode 100644 index 0000000000..2a2cb1f6b6 --- /dev/null +++ b/test/test1455.js @@ -0,0 +1,51 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 000 - multiple statements', function () { + const test = '000'; // insert test file number + + before(function () { + alasql('create database test' + test); + alasql('use test' + test); + }); + + after(function () { + alasql('drop database test' + test); + }); + + it('A) From single lines', function () { + var res = []; + res.push(alasql('create table one (a int)')); + res.push(alasql('insert into one values (1),(2),(3),(4),(5)')); + res.push(alasql('select * from one')); + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + }); + + it('B) Multiple statements in one string', function () { + // + var sql = 'create table two (a int);'; + sql += 'insert into two values (1),(2),(3),(4),(5);'; + sql += 'select * from two;'; + var res = alasql(sql); + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + }); + + it('C) Multiple statements in one string with callback', function (done) { + // Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code + var sql = 'create table three (a int);'; + sql += 'insert into three values (1),(2),(3),(4),(5);'; + sql += 'select * from three;'; + alasql(sql, function (res) { + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + done(); + }); + }); +}); +stdfn.REPLACE = function (target, pattern, replacement) { + if (typeof target !== 'string') { + throw new TypeError('Target must be a string'); + } + return target.split(pattern).join(replacement); +}; \ No newline at end of file