Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/15utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ let loadFile = (utils.loadFile = function (path, asy, success, error) {
try {
data = fs.readFileSync(path);
} catch (e) {
error(err, null);
error(e, null);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@
return '(' + declareRefs + ', ' + expr + ')';
}

return `(${declareRefs}, y.some(e => e == null) ? void 0 : ${expr})`;
return `(${declareRefs}, y.some(e => e == null || (typeof e === 'number' && isNaN(e))) ? void 0 : ${expr})`;
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/60createtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {
if (col.check) {
table.checks.push({
id: col.check.constrantid,
fn: new Function('r', 'var y;return ' + col.check.expression.toJS('r', '')),
fn: new Function('r,params,alasql', 'var y;return ' + col.check.expression.toJS('r', '')),
});
}

Expand Down Expand Up @@ -168,19 +168,23 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {
}
var fkfn = function (r) {
var rr = {};
if (typeof r[col.columnid] === 'undefined') {
// Allow NULL values in foreign keys (check for undefined, null, and NaN)
var val = r[col.columnid];
if (
typeof val === 'undefined' ||
val === null ||
(typeof val === 'number' && isNaN(val))
) {
return true;
}
rr[fk.columnid] = r[col.columnid];
rr[fk.columnid] = val;
var addr = fktable.pk.onrightfn(rr);
if (!fktable.uniqs[fktable.pk.hh][addr]) {
throw new Error(
'Foreign key "' + r[col.columnid] + '" not found in table "' + fk.tableid + '"'
);
throw new Error('Foreign key "' + val + '" not found in table "' + fk.tableid + '"');
}
return true;
};
table.checks.push({fn: fkfn});
table.checks.push({fn: fkfn, fk: true});
}

if (col.onupdate) {
Expand Down Expand Up @@ -212,7 +216,7 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {
pk.hh = hash(pk.onrightfns);
table.uniqs[pk.hh] = {};
} else if (con.type === 'CHECK') {
checkfn = new Function('r', 'var y;return ' + con.expression.toJS('r', ''));
checkfn = new Function('r,params,alasql', 'var y;return ' + con.expression.toJS('r', ''));
} else if (con.type === 'UNIQUE') {
var uk = {};
table.uk = table.uk || [];
Expand Down Expand Up @@ -371,7 +375,9 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {

if (table.checks && table.checks.length > 0) {
table.checks.forEach(function (check) {
if (!check.fn(r)) {
// In SQL, CHECK constraints treat NULL (undefined) as passing
// Only fail if the check explicitly returns false
if (check.fn(r, {}, alasql) === false) {
// if(orreplace) toreplace=true; else
throw new Error('Violation of CHECK constraint ' + (check.id || ''));
}
Expand Down Expand Up @@ -640,7 +646,9 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {
// PART 2 - POST CHECK
if (table.checks && table.checks.length > 0) {
table.checks.forEach(function (check) {
if (!check.fn(r)) {
// In SQL, CHECK constraints treat NULL (undefined) as passing
// Only fail if the check explicitly returns false
if (check.fn(r, params, alasql) === false) {
throw new Error('Violation of CHECK constraint ' + (check.id || ''));
}
});
Expand Down
50 changes: 29 additions & 21 deletions test/test324.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ if (typeof exports === 'object') {
}

describe('Test 324 Roads samples', function () {
it.skip('1. CREATE DATABASE', function (done) {
it('1. CREATE DATABASE', function (done) {
alasql('CREATE DATABASE test324a; USE test324a');
done();
});

it.skip('2. OBJECT_ID()', function (done) {
it('2. OBJECT_ID()', function (done) {
alasql('CREATE TABLE dbo.Employees(id INT, name STRING)');
alasql('INSERT INTO dbo.Employees VALUES (1,"Tomas"),(2,"Lisa")');
assert.deepEqual(alasql('SELECT * FROM dbo.Employees'), [
Expand All @@ -28,17 +28,17 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('3. DROP DATABASE', function (done) {
it('3. DROP DATABASE', function (done) {
alasql('DROP DATABASE test324a');
done();
});

it.skip('2. CREATE DATABASE', function (done) {
it('2. CREATE DATABASE', function (done) {
alasql('CREATE DATABASE test324b; USE test324b');
done();
});

it.skip('3. CREATE TABLE with constraints', function (done) {
it('3. CREATE TABLE with constraints', function (done) {
var res = alasql(function () {
/*
CREATE TABLE dbo.Employees
Expand All @@ -56,7 +56,7 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('4. INSERT INTO table with constraints', function (done) {
it('4. INSERT INTO table with constraints', function (done) {
var res = alasql(function () {
/*
INSERT INTO dbo.Employees(empid, mgrid, empname, salary) VALUES
Expand All @@ -72,7 +72,7 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('5. INSERT INTO table with same primary key', function (done) {
it('5. INSERT INTO table with same primary key', function (done) {
assert.throws(function () {
var res = alasql(function () {
/*
Expand All @@ -85,7 +85,7 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('6. INSERT INTO wrong NULL in NOT NULL column', function (done) {
it('6. INSERT INTO wrong NULL in NOT NULL column', function (done) {
assert.throws(function () {
var res = alasql(function () {
/*
Expand All @@ -97,27 +97,27 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('7. UPDATE wrong NULL in NOT NULL column', function (done) {
it('7. UPDATE wrong NULL in NOT NULL column', function (done) {
assert.throws(function () {
var res = alasql('UPDATE dbo.Employees SET empid = NULL WHERE empid = 1');
}, Error);
done();
});

it.skip('8. UPDATE wrong NULL in NOT NULL column', function (done) {
it('8. UPDATE wrong NULL in NOT NULL column', function (done) {
var res = alasql('UPDATE dbo.Employees SET mgrid = NULL WHERE empid = 2');
assert(res == 1);
done();
});

it.skip('9. UPDATE wrong NULL in NOT NULL column', function (done) {
it('9. UPDATE wrong NULL in NOT NULL column', function (done) {
assert.throws(function () {
var res = alasql('UPDATE dbo.Employees SET mgrid = 3 WHERE empid = 2');
}, Error);
done();
});

it.skip('10. INSERT INTO table with constraints violation', function (done) {
it('10. INSERT INTO table with constraints violation', function (done) {
// console.log(alasql.databases.dbo.tables.Employees);
assert.throws(function () {
var res = alasql(
Expand All @@ -129,7 +129,7 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('11. INSERT INTO table with constraints violation', function (done) {
it('11. INSERT INTO table with constraints violation', function (done) {
// console.log(alasql.databases.dbo.tables.Employees);
var res = alasql(
"INSERT INTO dbo.Employees(empid, mgrid, empname, salary) \
Expand All @@ -140,13 +140,13 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('12. UPDATE wrong NULL in NOT NULL column', function (done) {
it('12. UPDATE wrong NULL in NOT NULL column', function (done) {
var res = alasql('UPDATE dbo.Employees SET mgrid = 3 WHERE empid = 2');
assert(res == 1);
done();
});

it.skip('13. UPDATE table with constraints violation', function (done) {
it('13. UPDATE table with constraints violation', function (done) {
// console.log(alasql.databases.dbo.tables.Employees);
assert.throws(function () {
var res = alasql('UPDATE dbo.Employees SET mgrid = 1 WHERE empid = 1');
Expand All @@ -155,19 +155,27 @@ describe('Test 324 Roads samples', function () {
done();
});

it.skip('14. CURRENT_TIMESTAMP', function (done) {
it('14. CURRENT_TIMESTAMP', function (done) {
var res = alasql('SELECT VALUE CURRENT_TIMESTAMP');
assert(res.length == '2015.05.11 07:58:20.078'.length);
assert(res.substr(0, 2) == '20');
// Handle both string (when dateAsString=true) and Date object
if (typeof res === 'string') {
assert(res.length == '2015.05.11 07:58:20.078'.length);
assert(res.substr(0, 2) == '20');
} else {
assert(res instanceof Date);
assert(res.getFullYear() >= 2015);
}
done();
});
it.skip('19. DROP DATABASE', function (done) {
it('19. DROP DATABASE', function (done) {
alasql('DROP DATABASE test324b');
done();
});

it.skip('20. Full example', function (done) {
alasql('SOURCE "test324.sql"');
it('20. Full example', function (done) {
// Create tempdb database for the SQL file
alasql('CREATE DATABASE IF NOT EXISTS tempdb');
alasql('SOURCE "test/test324.sql"');
// Check NO COUNT
alasql.options.nocount = false;
done();
Expand Down
28 changes: 14 additions & 14 deletions test/test324.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ CREATE TABLE dbo.Employees
);

INSERT INTO dbo.Employees(empid, mgrid, empname, salary) VALUES
(1, NULL, 'David' , $10000.00),
(2, 1, 'Eitan' , $7000.00),
(3, 1, 'Ina' , $7500.00),
(4, 2, 'Seraph' , $5000.00),
(5, 2, 'Jiru' , $5500.00),
(6, 2, 'Steve' , $4500.00),
(7, 3, 'Aaron' , $5000.00),
(8, 5, 'Lilach' , $3500.00),
(9, 7, 'Rita' , $3000.00),
(10, 5, 'Sean' , $3000.00),
(11, 7, 'Gabriel', $3000.00),
(12, 9, 'Emilia' , $2000.00),
(13, 9, 'Michael', $2000.00),
(14, 9, 'Didi' , $1500.00);
(1, NULL, 'David' , 10000.00),
(2, 1, 'Eitan' , 7000.00),
(3, 1, 'Ina' , 7500.00),
(4, 2, 'Seraph' , 5000.00),
(5, 2, 'Jiru' , 5500.00),
(6, 2, 'Steve' , 4500.00),
(7, 3, 'Aaron' , 5000.00),
(8, 5, 'Lilach' , 3500.00),
(9, 7, 'Rita' , 3000.00),
(10, 5, 'Sean' , 3000.00),
(11, 7, 'Gabriel', 3000.00),
(12, 9, 'Emilia' , 2000.00),
(13, 9, 'Michael', 2000.00),
(14, 9, 'Didi' , 1500.00);

CREATE UNIQUE INDEX idx_unc_mgrid_empid ON dbo.Employees(mgrid, empid);
GO
Expand Down
Loading
Loading