mirror of
https://github.com/knex/knex.git
synced 2025-12-29 07:59:31 +00:00
oracle - fixed first and added more tests
This commit is contained in:
parent
3833824246
commit
f545c86081
@ -192,6 +192,8 @@ var components = [
|
||||
// Compiles the `select` statement, or nested sub-selects
|
||||
// by calling each of the component compilers, trimming out
|
||||
// the empties, and returning a generated query string.
|
||||
|
||||
QueryCompiler_Oracle.prototype.first =
|
||||
QueryCompiler_Oracle.prototype.select = function() {
|
||||
var self = this;
|
||||
var statements = _.map(components, function (component) {
|
||||
|
||||
@ -232,4 +232,4 @@ module.exports = function(knex) {
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@ -29,22 +29,33 @@ module.exports = function(knex) {
|
||||
'sum("logins")': 10
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select sum("logins") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
'SUM("LOGINS")': 10
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('has an avg', function() {
|
||||
|
||||
return knex('accounts').avg('logins').testSql(function(tester) {
|
||||
tester('mysql', 'select avg(`logins`) from `accounts`', [], [{
|
||||
'avg(`logins`)': 1.6667
|
||||
}]);
|
||||
tester('sqlite3', 'select avg("logins") from "accounts"', [], [{
|
||||
'avg("logins")': 1.6666666666666667
|
||||
}]);
|
||||
tester('postgresql', 'select avg("logins") from "accounts"', [], [{
|
||||
avg: '1.6666666666666667'
|
||||
}]);
|
||||
|
||||
function checkResRange(key, resp) {
|
||||
return Math.abs(10/6 - +(resp[0][key])) < 0.001;
|
||||
}
|
||||
|
||||
// mysql: 1.6667
|
||||
tester('mysql', 'select avg(`logins`) from `accounts`', [], checkResRange.bind(null, 'avg(`logins`)'));
|
||||
// sqlite: 1.6666666666666667
|
||||
tester('sqlite3', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg("logins")'));
|
||||
// postgres: '1.6666666666666667'
|
||||
tester('postgresql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg'));
|
||||
// oracle: 1.66666666666667
|
||||
tester('oracle', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'AVG("LOGINS")'));
|
||||
});
|
||||
|
||||
});
|
||||
@ -76,6 +87,14 @@ module.exports = function(knex) {
|
||||
'count("id")': 6
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select count("id") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
'COUNT("ID")': 6
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -113,6 +132,16 @@ module.exports = function(knex) {
|
||||
'min("logins")': 1
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select count("id"), max("logins"), min("logins") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
'COUNT("ID")': 6,
|
||||
'MAX("LOGINS")': 2,
|
||||
'MIN("LOGINS")': 1
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -150,6 +179,17 @@ module.exports = function(knex) {
|
||||
'count("id")': 4
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select count("id") from "accounts" group by "logins"',
|
||||
[],
|
||||
[{
|
||||
'COUNT("ID")': 2
|
||||
},{
|
||||
'COUNT("ID")': 4
|
||||
}]
|
||||
);
|
||||
|
||||
|
||||
}).then(function() {
|
||||
return knex('accounts').count('id').groupBy('first_name').testSql(function(tester) {
|
||||
@ -177,29 +217,20 @@ module.exports = function(knex) {
|
||||
'count("id")': 6
|
||||
}]
|
||||
);
|
||||
|
||||
tester(
|
||||
'oracle',
|
||||
'select count("id") from "accounts" group by "first_name"',
|
||||
[],
|
||||
[{
|
||||
'COUNT("ID")': 6
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('has an avg', function() {
|
||||
|
||||
return knex('accounts').avg('logins').testSql(function(tester) {
|
||||
tester('mysql', 'select avg(`logins`) from `accounts`', [], [{
|
||||
'avg(`logins`)': 1.6667
|
||||
}]);
|
||||
tester('postgresql', 'select avg("logins") from "accounts"', [], [{
|
||||
avg: '1.6666666666666667'
|
||||
}]);
|
||||
tester('sqlite3', 'select avg("logins") from "accounts"', [], [{
|
||||
'avg("logins")': 1.6666666666666667
|
||||
}]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -25,6 +25,12 @@ module.exports = function(knex) {
|
||||
[1],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'delete from "accounts" where "id" = ?',
|
||||
[1],
|
||||
1
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,6 +67,12 @@ module.exports = function(knex) {
|
||||
[2],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'delete from "accounts" where "id" = ?',
|
||||
[2],
|
||||
1
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -110,7 +110,28 @@ module.exports = function(knex) {
|
||||
account_id: 3,
|
||||
details: '',
|
||||
status: 1
|
||||
}], 'id').exec(function(err, resp) {
|
||||
}], 'id')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'oracle',
|
||||
"begin execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?;end;",
|
||||
[
|
||||
1,
|
||||
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
0,
|
||||
function (v) {return v.toString() === '[object ReturningHelper:id]';},
|
||||
2,
|
||||
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
1,
|
||||
function (v) {return v.toString() === '[object ReturningHelper:id]';},
|
||||
3,
|
||||
'',
|
||||
1,
|
||||
function (v) {return v.toString() === '[object ReturningHelper:id]';}
|
||||
],
|
||||
[1, 2, 3]
|
||||
);
|
||||
}).exec(function(err, resp) {
|
||||
if (err) return ok(err);
|
||||
ok();
|
||||
});
|
||||
@ -154,6 +175,29 @@ module.exports = function(knex) {
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d,'test4@example.com','Test','User',2, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test5@example.com','Test','User',2, d],
|
||||
[5]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"begin execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?; execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?;end;",
|
||||
[
|
||||
'Lorem ipsum Dolore labore incididunt enim.',
|
||||
d,
|
||||
'test4@example.com',
|
||||
'Test',
|
||||
'User',
|
||||
2,
|
||||
d,
|
||||
function (v) {return v.toString() === '[object ReturningHelper:id]';},
|
||||
'Lorem ipsum Dolore labore incididunt enim.',
|
||||
d,
|
||||
'test5@example.com',
|
||||
'Test',
|
||||
'User',
|
||||
2,
|
||||
d,
|
||||
function (v) {return v.toString() === '[object ReturningHelper:id]';}
|
||||
],
|
||||
[4, 5]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -188,6 +232,11 @@ module.exports = function(knex) {
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?",
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}]
|
||||
);
|
||||
})
|
||||
.then(function() {
|
||||
throw new Error('There should be a fail when multi-insert are made in unique col.');
|
||||
@ -227,6 +276,12 @@ module.exports = function(knex) {
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d],
|
||||
[6]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?",
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[7]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -252,6 +307,11 @@ module.exports = function(knex) {
|
||||
['d'],
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'insert into "datatype_test" ("enum_value") values (?)',
|
||||
['d']
|
||||
);
|
||||
})
|
||||
.then(function() {
|
||||
// No errors happen in sqlite3, which doesn't have native support
|
||||
|
||||
@ -6,10 +6,11 @@ module.exports = function(knex) {
|
||||
return knex('accounts')
|
||||
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.orderBy('accounts.id')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`', [], [{
|
||||
'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -47,7 +48,7 @@ module.exports = function(knex) {
|
||||
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id"', [], [{
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: '1',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -84,7 +85,7 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id"', [], [{
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -119,17 +120,56 @@ module.exports = function(knex) {
|
||||
details: ''
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select \"accounts\".*, \"test_table_two\".\"details\" from \"accounts\" inner join \"test_table_two\" on \"accounts\".\"id\" = \"test_table_two\".\"account_id\" order by \"accounts\".\"id\" asc", [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: 2,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: 3,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test3@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null // Oracle implicitly converted '' to NULL
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('has a leftJoin method parameter to specify the join type', function() {
|
||||
return knex('accounts')
|
||||
.leftJoin('test_table_two', 'accounts.id', '=', 'test_table_two.account_id')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.orderBy('accounts.id')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `accounts`.*, `test_table_two`.`details` from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`', [], [{
|
||||
'select `accounts`.*, `test_table_two`.`details` from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -199,7 +239,7 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id"', [], [{
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: '1',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -233,10 +273,10 @@ module.exports = function(knex) {
|
||||
phone: null,
|
||||
details: ''
|
||||
}, {
|
||||
id: '5',
|
||||
id: '4',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test5@example.com',
|
||||
email: 'test4@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
@ -244,10 +284,10 @@ module.exports = function(knex) {
|
||||
phone: null,
|
||||
details: null
|
||||
}, {
|
||||
id: '4',
|
||||
id: '5',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test4@example.com',
|
||||
email: 'test5@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
@ -269,7 +309,7 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id"', [], [{
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -337,6 +377,76 @@ module.exports = function(knex) {
|
||||
details: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select \"accounts\".*, \"test_table_two\".\"details\" from \"accounts\" left join \"test_table_two\" on \"accounts\".\"id\" = \"test_table_two\".\"account_id\" order by \"accounts\".\"id\" asc", [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: 2,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: 3,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test3@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null // Oracle implicitly converted '' to NULL
|
||||
}, {
|
||||
id: 4,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test4@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}, {
|
||||
id: 5,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test5@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}, {
|
||||
id: 7,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test6@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -346,10 +456,11 @@ module.exports = function(knex) {
|
||||
join.orOn('accounts.email', '=', 'test_table_two.details');
|
||||
})
|
||||
.select()
|
||||
.orderBy('accounts.id')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select * from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` or `accounts`.`email` = `test_table_two`.`details`', [], [{
|
||||
'select * from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` or `accounts`.`email` = `test_table_two`.`details` order by `accounts`.`id` asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -437,7 +548,7 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details"', [], [{
|
||||
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -525,7 +636,7 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details"', [], [{
|
||||
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
@ -686,6 +797,27 @@ module.exports = function(knex) {
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select * from (select \"accounts\".\"email\" \"e1\", \"a2\".\"email\" \"e2\" from \"accounts\" inner join \"accounts\" \"a2\" on \"a2\".\"email\" <> \"accounts\".\"email\" where \"a2\".\"email\" = ? order by \"e1\" asc) where rownum <= ?",
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test6@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -699,10 +831,11 @@ module.exports = function(knex) {
|
||||
.where('a2.email', 'test2@example.com')
|
||||
.select(['accounts.email as e1', 'a2.email as e2'])
|
||||
.limit(5)
|
||||
.orderBy('e1')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `accounts`.`email` <> `a2`.`email` or `accounts`.`id` = 2 where `a2`.`email` = ? limit ?',
|
||||
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `accounts`.`email` <> `a2`.`email` or `accounts`.`id` = 2 where `a2`.`email` = ? order by `e1` asc limit ?',
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test2@example.com',
|
||||
@ -723,28 +856,28 @@ module.exports = function(knex) {
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? limit ?',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?',
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test2@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
e1: 'test2@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test6@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? limit ?',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?',
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test2@example.com',
|
||||
@ -763,6 +896,27 @@ module.exports = function(knex) {
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select * from (select \"accounts\".\"email\" \"e1\", \"a2\".\"email\" \"e2\" from \"accounts\" inner join \"accounts\" \"a2\" on \"accounts\".\"email\" <> \"a2\".\"email\" or \"accounts\".\"id\" = 2 where \"a2\".\"email\" = ? order by \"e1\" asc) where rownum <= ?",
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test2@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test6@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -11,47 +11,59 @@ module.exports = function(knex) {
|
||||
});
|
||||
|
||||
it('returns an array of a single column with `pluck`', function() {
|
||||
return knex.pluck('id').from('accounts')
|
||||
return knex.pluck('id').orderBy('id').from('accounts')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `id` from `accounts`',
|
||||
'select `id` from `accounts` order by `id` asc',
|
||||
[],
|
||||
[1, 2, 3, 4, 5, 7]
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "id" from "accounts"',
|
||||
'select "id" from "accounts" order by "id" asc',
|
||||
[],
|
||||
['1', '2', '3', '4', '5', '7']
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "id" from "accounts"',
|
||||
'select "id" from "accounts" order by "id" asc',
|
||||
[],
|
||||
[1, 2, 3, 4, 5, 6]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select \"id\" from \"accounts\" order by \"id\" asc",
|
||||
[],
|
||||
[1, 2, 3, 4, 5, 7]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a single entry with first', function() {
|
||||
return knex.first('id', 'first_name').from('accounts')
|
||||
return knex.first('id', 'first_name').orderBy('id').from('accounts')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `id`, `first_name` from `accounts` limit ?',
|
||||
'select `id`, `first_name` from `accounts` order by `id` asc limit ?',
|
||||
[1],
|
||||
{ id: 1, first_name: 'Test' }
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "id", "first_name" from "accounts" limit ?',
|
||||
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
|
||||
[1],
|
||||
{ id: '1', first_name: 'Test' }
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "id", "first_name" from "accounts" limit ?',
|
||||
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
|
||||
[1],
|
||||
{ id: 1, first_name: 'Test' }
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
"select * from (select \"id\", \"first_name\" from \"accounts\" order by \"id\" asc) where rownum <= ?",
|
||||
[1],
|
||||
{ id: 1, first_name: 'Test' }
|
||||
);
|
||||
@ -110,8 +122,16 @@ module.exports = function(knex) {
|
||||
|
||||
it('uses `orderBy`', function() {
|
||||
return knex('accounts')
|
||||
.select()
|
||||
.orderBy('id', 'asc');
|
||||
.pluck('id')
|
||||
.orderBy('id', 'desc')
|
||||
.testSql(function(tester) {
|
||||
tester(
|
||||
'oracle',
|
||||
"select \"id\" from \"accounts\" order by \"id\" desc",
|
||||
[],
|
||||
[7, 5, 4, 3, 2, 1]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('simple "where" cases', function() {
|
||||
@ -125,17 +145,38 @@ module.exports = function(knex) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -149,17 +190,39 @@ module.exports = function(knex) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -185,6 +248,11 @@ module.exports = function(knex) {
|
||||
'select "email", "logins" from "accounts" where "id" > ?',
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select "email", "logins" from "accounts" where "id" > ?',
|
||||
[1]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -213,12 +281,50 @@ module.exports = function(knex) {
|
||||
tester(
|
||||
'postgresql',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
id: '1',
|
||||
first_name: "Test",
|
||||
last_name: "User",
|
||||
email: "test@example.com",
|
||||
logins: 1,
|
||||
about: "Lorem ipsum Dolore labore incididunt enim.",
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[1]
|
||||
[1],
|
||||
[{
|
||||
id: 1,
|
||||
first_name: "Test",
|
||||
last_name: "User",
|
||||
email: "test@example.com",
|
||||
logins: 1,
|
||||
about: "Lorem ipsum Dolore labore incididunt enim.",
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
id: 1,
|
||||
first_name: "Test",
|
||||
last_name: "User",
|
||||
email: "test@example.com",
|
||||
logins: 1,
|
||||
about: "Lorem ipsum Dolore labore incididunt enim.",
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -232,16 +338,25 @@ module.exports = function(knex) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select `first_name`, `email` from `accounts` where `id` is null',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select "first_name", "email" from "accounts" where "id" is null',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select "first_name", "email" from "accounts" where "id" is null',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select "first_name", "email" from "accounts" where "id" is null',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
});
|
||||
@ -257,17 +372,26 @@ module.exports = function(knex) {
|
||||
tester(
|
||||
'mysql',
|
||||
'select * from `accounts` where `id` = ?',
|
||||
[0]
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'postgresql',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[0]
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'sqlite3',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[0]
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'oracle',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -345,6 +469,20 @@ module.exports = function(knex) {
|
||||
details: 'One, Two, Zero',
|
||||
status: 0
|
||||
}]);
|
||||
tester('oracle',
|
||||
'select * from "composite_key_test" where ("column_a","column_b") in ((?, ?),(?, ?))',
|
||||
[1,1,1,2],
|
||||
[{
|
||||
column_a: 1,
|
||||
column_b: 1,
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
},{
|
||||
column_a: 1,
|
||||
column_b: 2,
|
||||
details: 'One, Two, Zero',
|
||||
status: 0
|
||||
}]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -374,6 +512,15 @@ module.exports = function(knex) {
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
}]);
|
||||
tester('oracle',
|
||||
'select * from "composite_key_test" where "status" = ? and ("column_a","column_b") in ((?, ?),(?, ?))',
|
||||
[1,1,1,1,2],
|
||||
[{
|
||||
column_a: 1,
|
||||
column_b: 1,
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
}]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -453,4 +600,4 @@ module.exports = function(knex) {
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@ -241,4 +241,4 @@ module.exports = function(knex) {
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@ -58,7 +58,7 @@ module.exports = function(testSuite) {
|
||||
this.then = function() {
|
||||
var promise = oldThen.apply(this, []);
|
||||
promise = promise.tap(function(resp) {
|
||||
expect(stripDates(resp)).to.eql(returnval);
|
||||
_.isFunction(returnval) ? expect(returnval(resp)).to.be.true : expect(stripDates(resp)).to.eql(returnval);
|
||||
});
|
||||
return promise.then.apply(promise, arguments);
|
||||
};
|
||||
@ -90,4 +90,4 @@ module.exports = function(testSuite) {
|
||||
return knex;
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@ -349,6 +349,12 @@ module.exports = function(pgclient, mysqlclient, sqlite3client, oracleclient) {
|
||||
expect(chain.bindings).to.eql([10, 5]);
|
||||
});
|
||||
|
||||
it("Oracle first", function() {
|
||||
chain = oracle().first('*').from('users').toSQL();
|
||||
expect(chain.sql).to.equal('select * from (select * from \"users\") where rownum <= ?');
|
||||
expect(chain.bindings).to.eql([1]);
|
||||
});
|
||||
|
||||
it("Oracle limits", function() {
|
||||
chain = oracle().select('*').from('users').limit(10).toSQL();
|
||||
expect(chain.sql).to.equal('select * from (select * from \"users\") where rownum <= ?');
|
||||
@ -854,4 +860,4 @@ module.exports = function(pgclient, mysqlclient, sqlite3client, oracleclient) {
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user