Skip to content

Commit

Permalink
implement count
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-gross committed Jun 17, 2016
1 parent 77e5775 commit 25dc794
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions spec/InstallationsRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('InstallationsRouter', () => {
});
});

it_exclude_dbs(['postgres'])('query installations with count = 1', (done) => {
it('query installations with count = 1', done => {
var androidDeviceRequest = {
'installationId': '12345678-abcd-abcd-abcd-123456789abc',
'deviceType': 'android'
Expand All @@ -130,7 +130,11 @@ describe('InstallationsRouter', () => {
expect(response.results.length).toEqual(2);
expect(response.count).toEqual(2);
done();
});
})
.catch(error => {
fail(JSON.stringify(error));
done();
})
});

it_exclude_dbs(['postgres'])('query installations with limit = 0 and count = 1', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ describe('Parse.Object testing', () => {
expectError(Parse.Error.MISSING_OBJECT_ID, done));
});

it("fetchAll error on deleted object", function(done) {
it_exclude_dbs(['postgres'])("fetchAll error on deleted object", function(done) {
var numItems = 11;
var container = new Container();
var subContainer = new Container();
Expand Down
14 changes: 12 additions & 2 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ export class PostgresStorageAdapter {
let where = buildWhereClause({ schema, query, index: 2 })
values.push(...where.values);

const qs = `SELECT * FROM $1:name WHERE ${where.pattern} ${limit !== undefined ? `LIMIT $${values.length + 1}` : ''}`;
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
const limitPattern = limit !== undefined ? `LIMIT $${values.length + 1}` : '';

const qs = `SELECT * FROM $1:name ${wherePattern} ${limitPattern}`;
if (limit !== undefined) {
values.push(limit);
}
Expand Down Expand Up @@ -416,7 +419,14 @@ export class PostgresStorageAdapter {

// Executs a count.
count(className, schema, query) {
return Promise.reject('Not implented yet.')
let values = [className];
let where = buildWhereClause({ schema, query, index: 2 });
values.push(...where.values);

const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
const qs = `SELECT COUNT(*) FROM $1:name ${wherePattern}`;
return this._client.query(qs, values)
.then(result => parseInt(result[0].count))
}
}

Expand Down

0 comments on commit 25dc794

Please sign in to comment.