Skip to content

Commit

Permalink
feat: add count(table, where)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 8, 2015
1 parent 3b259af commit 6286c46
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ TBD
- *insert(table, row[s], options)
- *update(table, row, options)
- *delete(table, where)
- *count(table, where)

### Utils

Expand Down
19 changes: 17 additions & 2 deletions lib/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,33 @@ proto.format = function (sql, values, stringifyObjects, timeZone) {
return SqlString.format(sql, values, stringifyObjects, timeZone);
};

proto.query = function (sql, values) {
proto.query = function* (sql, values) {
// query(sql, values)
if (arguments.length >= 2) {
sql = this.format(sql, values);
}
return this._query(sql);
debug('query %j', sql);
try {
return yield this._query(sql);
} catch (err) {
err.message += ' (sql: ' + sql + ')';
debug('query error: %s', err);
throw err;
}
};

proto._query = function (/* sql */) {
throw new Error('SubClass must impl this');
};

proto.count = function* (table, where) {
let sql = this.format('SELECT COUNT(*) as count FROM ??', [table]) +
this._where(where);
debug('count(%j, %j) \n=> %j', table, where, sql);
var rows = yield this.query(sql);
return rows[0].count;
};

/**
* Select rows from a table
*
Expand Down
39 changes: 33 additions & 6 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ describe('client.test.js', function () {
yield tran.select(table);
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}

try {
yield tran.rollback();
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}

try {
yield tran.commit();
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}
});

Expand All @@ -119,21 +119,21 @@ describe('client.test.js', function () {
yield tran.select(table);
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}

try {
yield tran.commit();
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}

try {
yield tran.rollback();
throw new Error('should not run this');
} catch (err) {
assert.equal(err.message, 'transaction was commit or rollback');
assert.equal(err.message.indexOf('transaction was commit or rollback'), 0);
}
});

Expand Down Expand Up @@ -328,16 +328,19 @@ describe('client.test.js', function () {
let users = yield this.db.select(table, {
orders: 'id'
});
assert(users.length >= 2);
assert(users[0].id < users[1].id);

users = yield this.db.select(table, {
orders: [['id', 'desc'], null, 1]
});
assert(users.length >= 2);
assert(users[0].id > users[1].id);

users = yield this.db.select(table, {
orders: ['id', ['name', 'foo']]
});
assert(users.length >= 2);
assert(users[0].id < users[1].id);
});
});
Expand Down Expand Up @@ -608,4 +611,28 @@ describe('client.test.js', function () {
}
});
});

describe('count()', function () {
before(function* () {
yield this.db.query('insert into ??(name, email, gmt_create, gmt_modified) \
values(?, ?, now(), now())',
[table, prefix + 'fengmk2-count', prefix + 'm@fengmk2-count.com']);
yield this.db.query('insert into ??(name, email, gmt_create, gmt_modified) \
values(?, ?, now(), now())',
[table, prefix + 'fengmk3-count', prefix + 'm@fengmk2-count.com']);
});

it('should get total table rows count', function* () {
let count = yield this.db.count(table);
assert(count >= 2);

count = yield this.db.count(table, {
email: prefix + 'm@fengmk2-count.com'
});
assert.equal(count, 2);

count = yield this.db.count(table, { id: -1 });
assert.equal(count, 0);
});
});
});
2 changes: 1 addition & 1 deletion test/operator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('operator.test.js', function () {
try {
yield op.query('foo');
} catch (err) {
assert.equal(err.message, 'SubClass must impl this');
assert.equal(err.message.indexOf('SubClass must impl this'), 0);
}
});
});
Expand Down

0 comments on commit 6286c46

Please sign in to comment.