Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#23 select distinct #24

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions libs/apollo_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var AERROR_TYPES = {
'model.find.limittype': {
msg: 'Invalid limit value'
},
'model.find.invaliddistinct': {
msg: 'Invalid distinct columns. Must be Array'
},
'model.find.dberror': {
msg: 'Error during find query on DB -> %s'
},
Expand Down
32 changes: 28 additions & 4 deletions libs/base_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,14 @@ BaseModel._create_table_query = function(table_name,schema){
continue;
}
field_type = schemer.get_field_type(schema, k);
rows.push(util.format('"%s" %s',k,field_type));
rows.push(
util.format(
'"%s" %s %s',
k,
field_type,
schema.fields[k]["static"] ? "static" : " "
)
);
}

var partition_key = schema.key[0],
Expand Down Expand Up @@ -371,6 +378,11 @@ BaseModel._get_db_table_schema = function (callback){
db_schema.key = [[]];
db_schema.key[row.component_index+1] = row.column_name;
}
else if(row.type == 'static'){
if(!db_schema['static'])
db_schema['static'] = [];
db_schema['static'].push(row.column_name);
}
if(row.index_name){
if(!db_schema.indexes)
db_schema.indexes = [];
Expand Down Expand Up @@ -564,12 +576,22 @@ BaseModel._create_find_query = function(query_ob, options){
}
}
var where = this._create_where_clause(query_ob);
var distinct = '*';
if( options.distinct != null){
if( !options.distinct instanceof Array ){
throw(build_error('model.find.invaliddistinct'));
}
distinct = 'DISTINCT '+options.distinct.join(', ');
}

var query = util.format(
'SELECT * FROM "%s" %s %s %s ALLOW FILTERING;',
'SELECT %s FROM "%s" %s %s %s %s;',
distinct,
this._properties.table_name,
where,
order_keys.length ? 'ORDER BY '+ order_keys.join(', '):' ',
limit ? 'LIMIT '+limit : ' '
limit ? 'LIMIT '+limit : ' ',
options.allowfiltering ? 'ALLOW FILTERING' : ' '
);
return query;
};
Expand Down Expand Up @@ -671,7 +693,9 @@ BaseModel.find = function(query_ob, options, callback){

var defaults = {
raw : false,
consistency : CONSISTENCY_FIND
consistency : CONSISTENCY_FIND,
allowfiltering : false,
distinct : null
};

options = lodash.defaults(options, defaults);
Expand Down
125 changes: 122 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ describe('Apollo > ', function(){
},
key:[["name", "surname"]]
};

var model_test_static = {
fields:{
username: "text",
name:{
"type":"text",
"static":true
},
surname:{
"type":"text",
"static":true
},
favorites:{
"type":"text",
"static":false
},
likes:"text",
},
key:[["username"],"favorites","likes"]
};

it('add model', function(){
var TestModel = ap.add_model("test1", model_test1);
Expand Down Expand Up @@ -180,6 +200,23 @@ describe('Apollo > ', function(){
assert.notOk(ins.complete_name);
});

it('adds model with static fields', function(done){
var TestModel = ap.add_model("teststaticfields", model_test_static);
TestModel._create_table(function (err) {
TestModel._get_db_table_schema(function(err, schema){
assert.notOk(err);
assert.property(schema,'static');
assert.isArray(schema['static']);
assert.equal(schema['static'][0], 'name');
assert.equal(schema['static'][1], 'surname');
assert.isFunction(TestModel);
assert.property(TestModel,'find');
assert.isFalse(TestModel.is_table_ready());
done();
});
})
});

describe('Validation >', function(){

it('field validation', function(){
Expand Down Expand Up @@ -711,7 +748,7 @@ describe('Apollo > ', function(){
});

it('basic find', function(done){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},function(err, results){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ allowfiltering: true },function(err, results){
assert.lengthOf(results, 1);
var result = results[0];
assert.instanceOf(result, TestModel);
Expand All @@ -725,7 +762,7 @@ describe('Apollo > ', function(){
});

it('basic find with raw results', function(done){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ raw: true },function(err, results){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ raw: true, allowfiltering: true },function(err, results){
assert.lengthOf(results, 1);
var result = results[0];
assert.notInstanceOf(result, TestModel);
Expand Down Expand Up @@ -753,7 +790,7 @@ describe('Apollo > ', function(){
});

it('using >= ($gte) in clustering key', function(done){
TestModel.find({'v3':{'$gte':1 } },function(err, results){
TestModel.find({'v3':{'$gte':1 } }, { allowfiltering: true },function(err, results){
assert.lengthOf(results, 3);
done();
});
Expand Down Expand Up @@ -832,6 +869,88 @@ describe('Apollo > ', function(){
});
});

describe('Distinct > ', function() {

var TestFindDistinctModel;

beforeEach(function(done) {
this.timeout(15000);

TestFindDistinctModel = ap.add_model("testfinddistinct", model_test_static, {'mismatch_behaviour':'drop'});

TestFindDistinctModel.init(function(err,result){

if(err) return done(err);
var ins = new TestFindDistinctModel();
async.series([
function(callback){
ins.username = 'ab';
ins.name = 'a';
ins.surname = 'b';
ins.favorites = 'x';
ins.likes = 'xx';
ins.save(callback);
},
function(callback){
ins.username = 'ab';
ins.name = 'a';
ins.surname = 'b';
ins.favorites = 'y';
ins.likes = 'yy';
ins.save(callback);
},
function(callback){
ins.username = 'ab';
ins.name = 'a';
ins.surname = 'b';
ins.favorites = 'z';
ins.likes = 'zz';
ins.save(callback);
}

],done);
});
});

it('selecting distinct static columns', function(done){
TestFindDistinctModel.find({},{ distinct : ['username','name','surname']},function(err, results){
assert.notOk(err);
assert.lengthOf(results, 1);
assert.propertyVal(results[0],'username','ab');
assert.propertyVal(results[0],'name','a');
assert.propertyVal(results[0],'surname','b');
done();
});
});

it('selecting distinct static columns querying by primary key', function(done){
TestFindDistinctModel.find({ username : 'ab' },{ distinct : ['username','name','surname']},function(err, results){
assert.notOk(err);
assert.lengthOf(results, 1);
assert.propertyVal(results[0],'username','ab');
assert.propertyVal(results[0],'name','a');
assert.propertyVal(results[0],'surname','b');
done();
});
});

it('selecting non static columns throws error', function(done){
TestFindDistinctModel.find({},{ distinct : ['username','name','favorites']},function(err, results){
assert.ok(err);
assert.propertyVal(err, 'name', 'apollo.model.find.dberror');
done();
});
});

it('selecting distinct static columns querying by only static columns throws error', function(done){
TestFindDistinctModel.find({ name : 'a' },{ distinct : ['username','name','surname']},function(err, results){
assert.ok(err);
assert.propertyVal(err, 'name', 'apollo.model.find.dberror');
done();
});
});
});

});

describe('Delete > ',function(){
Expand Down