more tests, a little bit of cleanup internally

This commit is contained in:
Tim Griesser 2013-05-05 13:54:43 -04:00
parent 09e9c60912
commit da457290b3
7 changed files with 91 additions and 84 deletions

View File

@ -49,7 +49,7 @@ _.extend(PostgresClient.prototype, base.protoProps, {
if (resp.command === 'SELECT') {
resp = resp.rows;
} else if (resp.command === 'INSERT') {
resp = _.map(resp.rows, function(row) { return row[builder.idAttr]; });
resp = _.map(resp.rows, function(row) { return row[builder._idAttribute]; });
} else if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
resp = resp.rowCount;
} else {
@ -91,8 +91,8 @@ PostgresClient.grammar = {
// inserts using a single query statement.
compileInsert: function(qb) {
var sql = require('../knex').Grammar.compileInsert.call(this, qb);
if (qb.idAttr) {
sql += ' returning "' + qb.idAttr + '"';
if (qb._idAttribute) {
sql += ' returning "' + qb._idAttribute + '"';
}
return sql;
}

64
knex.js
View File

@ -39,6 +39,12 @@
return run.nodeify(callback);
},
// The promise interface for the query builder.
then: function(onFulfilled, onRejected) {
var run = Knex.runQuery(this);
return run.then(onFulfilled, onRejected);
},
// Specifies to resolve the statement with the `data` rather
// than a promise... useful in testing/debugging.
toString: function() {
@ -56,22 +62,16 @@
}).join('; ');
},
// The promise interface for the query builder.
then: function(onFulfilled, onRejected) {
var run = Knex.runQuery(this);
return run.then(onFulfilled, onRejected);
},
// The connection the current query is being run on, optionally
// specified by the connection method.
_connection: false,
// Sets the connection
connection: function(connection) {
this._connection = connection;
return this;
},
// The connection the current query is being run on, optionally
// specified by the `connection` method.
_connection: false,
// Sets the "type" of the current query, so we can potentially place
// `select`, `update`, `del`, etc. anywhere in the query statement
// and have it come out fine.
@ -85,10 +85,12 @@
// Returns all bindings excluding the `Knex.Raw` types.
_cleanBindings: function() {
return _.reduce(this.bindings, function(memo, binding) {
if (!(binding instanceof Raw)) memo.push(binding);
return memo;
}, []);
var bindings = this.bindings;
var cleaned = [];
for (var i = 0, l = bindings.length; i < l; i++) {
if (!(bindings[i] instanceof Raw)) cleaned.push(bindings[i]);
}
return cleaned;
}
};
@ -114,20 +116,21 @@
if (_.isEmpty(qb.columns)) qb.columns = ['*'];
for (var i = 0, l = components.length; i < l; i++) {
var component = components[i];
if (_.result(qb, component) != null) {
sql[component] = this['compile' + capitalize(component)](qb, _.result(qb, component));
var result = _.result(qb, component);
if (result != null) {
sql[component] = this['compile' + capitalize(component)](qb, result);
}
}
return _.compact(sql).join(' ');
},
// Compiles an aggregate query.
compileAggregate: function(qb, aggregate) {
var column = this.columnize(aggregate.columns);
compileAggregate: function(qb) {
var column = this.columnize(qb.aggregate.columns);
if (qb.isDistinct && column !== '*') {
column = 'distinct ' + column;
}
return 'select ' + aggregate.type + '(' + column + ') as aggregate';
return 'select ' + qb.aggregate.type + '(' + column + ') as aggregate';
},
// Compiles the columns in the query, specifying if an item was distinct.
@ -406,13 +409,13 @@
_source: 'Builder',
idAttr: 'id',
_idAttribute: 'id',
// Sets the `returning` for the query - only necessary
// to set the "returning" value for the postgres insert,
// defaults to `id`.
returning: function(val) {
this.idAttr = val;
idAttribute: function(val) {
this._idAttribute = val;
return this;
},
@ -447,7 +450,7 @@
item.client = this.client;
item.grammar = this.grammar;
var items = [
'idAttr', 'isDistinct', 'joins', 'wheres', 'orders',
'_idAttribute', 'isDistinct', 'joins', 'wheres', 'orders',
'columns', 'bindings', 'grammar', 'transaction', 'unions'
];
for (var i = 0, l = items.length; i < l; i++) {
@ -733,7 +736,7 @@
return this._counter(column, amount, '-');
},
// Performs a `select` query, returning a promise.
// Sets the values for a `select` query.
select: function(columns) {
if (columns) {
this.columns = this.columns.concat(_.isArray(columns) ? columns : _.toArray(arguments));
@ -741,25 +744,25 @@
return this._setType('select');
},
// Performs an `insert` query, returning a promise.
// Sets the values for an `insert` query.
insert: function(values) {
this.values = this._prepValues(values);
return this._setType('insert');
},
// Performs an `update` query, returning a promise.
// Sets the values for an `update` query.
update: function(values) {
var obj = sortObject(values);
var bindings = [];
for (var i = 0, l = obj.length; i < l; i++) {
bindings[i] = (obj[i][1]);
bindings[i] = obj[i][1];
}
this.bindings = bindings.concat(this.bindings);
this.bindings = bindings.concat(this.bindings || []);
this.values = obj;
return this._setType('update');
},
// Alias to del
// Alias to del.
"delete": function() {
return this._setType('delete');
},
@ -834,8 +837,9 @@
},
_aggregate: function(type, columns) {
if (!_.isArray(columns)) columns = [columns];
this.aggregate = {type: type, columns: columns};
return this.select();
return this._setType('select');
},
_counter: function(column, amount, symbol) {

12
test/lib/aggregate.js Normal file
View File

@ -0,0 +1,12 @@
var Q = require('q');
module.exports = function(Knex, dbName, handler, type) {
it('has a sum', function(ok) {
Knex('accounts')
.sum('logins')
.then(handler(ok), ok);
});
};

View File

@ -8,7 +8,7 @@ module.exports = function(Knex, handler, error) {
Knex.Schema.dropTableIfExists('test_table_three'),
Knex.Schema.dropTableIfExists('accounts')
]).then(function(resp) {
res = resp;
res = [resp[0]]; // only really need one of these for the test output.
return Q.all([
Knex.Schema.createTable('test_table_one', function(table) {
table.increments('id');

View File

@ -32,6 +32,10 @@ module.exports = function(Knex, type) {
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'DB');
});
describe('Aggregate', function() {
require('./lib/aggregate')(Knex, type, handler(type, 'aggregate'), 'DB');
});
describe('Joins', function() {
require('./lib/joins')(Knex, type, handler(type, 'joins'), 'DB');
});

View File

@ -15,48 +15,6 @@ module.exports = {
}
},
'schema.2': {
mysql: {
sql: ['drop table if exists `test_table_two`'],
bindings: []
},
postgres: {
sql: ['drop table if exists "test_table_two"'],
bindings: []
},
sqlite3: {
sql: ['drop table if exists "test_table_two"'],
bindings: []
}
},
'schema.3': {
mysql: {
sql: ['drop table if exists `test_table_three`'],
bindings: []
},
postgres: {
sql: ['drop table if exists "test_table_three"'],
bindings: []
},
sqlite3: {
sql: ['drop table if exists "test_table_three"'],
bindings: []
}
},
'schema.4': {
mysql: {
sql: ['drop table if exists `accounts`'],
bindings: []
},
postgres: {
sql: ['drop table if exists "accounts"'],
bindings: []
},
sqlite3: {
sql: ['drop table if exists "accounts"'],
bindings: []
}
},
'schema.5': {
mysql: {
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null)','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
bindings: []
@ -70,7 +28,7 @@ module.exports = {
bindings: []
}
},
'schema.6': {
'schema.3': {
mysql: {
sql: ['create table `test_table_two` (`id` int(11) not null auto_increment primary key, `account_id` int(11) not null, `details` text not null)'],
bindings: []
@ -84,7 +42,7 @@ module.exports = {
bindings: []
}
},
'schema.7': {
'schema.4': {
mysql: {
sql: ['create table `test_table_three` (`main` int(11) not null, `paragraph` text not null)','alter table `test_table_three` add primary key test_table_three_main_primary(`main`)'],
bindings: []
@ -98,7 +56,7 @@ module.exports = {
bindings: []
}
},
'schema.8': {
'schema.5': {
mysql: {
sql: ['alter table `test_table_one` add `phone` varchar(255) null'],
bindings: []
@ -112,7 +70,7 @@ module.exports = {
bindings: []
}
},
'schema.9': {
'schema.6': {
mysql: {
sql: ['drop table if exists `items`'],
bindings: []
@ -126,7 +84,7 @@ module.exports = {
bindings: []
}
},
'schema.10': {
'schema.7': {
mysql: {
sql: ['select * from information_schema.tables where table_schema = ? and table_name = ?'],
bindings: ['knex_test','test_table_two']
@ -140,7 +98,7 @@ module.exports = {
bindings: ['test_table_two']
}
},
'schema.11': {
'schema.8': {
mysql: {
sql: ['rename table `test_table_one` to `accounts`'],
bindings: []
@ -154,7 +112,7 @@ module.exports = {
bindings: []
}
},
'schema.12': {
'schema.9': {
mysql: {
sql: ['drop table `test_table_three`'],
bindings: []
@ -490,6 +448,20 @@ module.exports = {
bindings: [1,100,200,300]
}
},
'aggregate.1': {
mysql: {
sql: 'select sum(`logins`) as aggregate from `accounts`',
bindings: []
},
postgres: {
sql: 'select sum("logins") as aggregate from "accounts"',
bindings: []
},
sqlite3: {
sql: 'select sum("logins") as aggregate from "accounts"',
bindings: []
}
},
'joins.1': {
mysql: {
sql: 'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`',
@ -1786,6 +1758,17 @@ module.exports = {
phone: null
}]
},
'aggregate.1': {
mysql: [{
aggregate: 10
}],
postgres: [{
aggregate: 10
}],
sqlite3: [{
aggregate: 10
}]
},
'joins.1': {
mysql: [{
id: 1,

View File

@ -32,6 +32,10 @@ module.exports = function(Knex, type) {
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'String');
});
describe('Aggregate', function() {
require('./lib/aggregate')(Knex, type, handler(type, 'aggregate'), 'String');
});
describe('Joins', function() {
require('./lib/joins')(Knex, type, handler(type, 'joins'), 'String');
});
@ -40,7 +44,7 @@ module.exports = function(Knex, type) {
require('./lib/deletes')(Knex, type, handler(type, 'deletes'), 'String');
});
describe('Aggregates, Truncate', function() {
describe('Additional', function() {
require('./lib/additional')(Knex, type, handler(type, 'additional'), 'String');
});