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

Parameterized Queries #21

Merged
merged 2 commits into from
Dec 10, 2014
Merged
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
5 changes: 4 additions & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ module.exports = (function() {
options: {
// Waterline only allows populating 1 level below. fetchPlanLevel allows to
// to populate further levels below
fetchPlanLevel : 1
fetchPlanLevel : 1,
// Turns on parameterized queries and it should be enabled, but breaks 2 tests against 1.7.*.
// https://github.com/appscot/waterline-orientdb/issues/20
parameterized : false
}
},

Expand Down
32 changes: 20 additions & 12 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = (function () {
createCustomIndex: false,
idProperty: 'id',
options: {
fetchPlanLevel: 1
fetchPlanLevel: 1,
parameterized: false
}
},
dbDefaults = {
Expand Down Expand Up @@ -332,7 +333,7 @@ module.exports = (function () {
var _query;
// Catch errors from building query and return to the callback
try {
_query = new Query(options, collection);
_query = new Query(options, collection, this.config);
} catch(err) {
return cb(err);
}
Expand Down Expand Up @@ -362,10 +363,11 @@ module.exports = (function () {
dbHelper.prototype.find = function(collection, options, cb) {
var collectionInstance = this.collections[collection];
var schema = collectionInstance.waterline.schema;
var attributes = collectionInstance.attributes;
var _query, query;

try {
_query = new Query(options, schema);
_query = new Query(options, schema, this.config);
} catch(err) { return cb(err); }

var edge;
Expand All @@ -387,16 +389,16 @@ module.exports = (function () {
}

this.db
.query(query)
.query(query.query[0], { params: query.params || {} })
.all()
.then(function (res) {
if (res && options.fetchPlan) {
var cleanRes = _.map(res, function(record) {
return JSON.parse(record.this);
});
cb(null, utils.rewriteIdsRecursive(cleanRes, schema.attributes));
cb(null, utils.rewriteIdsRecursive(cleanRes, attributes));
} else {
cb(null, utils.rewriteIds(res, schema.attributes));
cb(null, utils.rewriteIds(res, attributes));
}
})
.error(function (e) {
Expand Down Expand Up @@ -527,7 +529,7 @@ module.exports = (function () {

// Catch errors from building query and return to the callback
try {
_query = new Query(options, schema);
_query = new Query(options, schema, self.config);
_document = new Document(values, attributes);
where = _query.getWhereQuery(collection);
} catch(e) {
Expand All @@ -540,8 +542,11 @@ module.exports = (function () {
.transform(transformers)
.return('AFTER');

if(where){
query.where(where);
if(where.query[0]){
query.where(where.query[0]);
if(where.params){
query.addParams(where.params);
}
}

query
Expand Down Expand Up @@ -573,7 +578,7 @@ module.exports = (function () {

// Catch errors from building query and return to the callback
try {
_query = new Query(options, schema);
_query = new Query(options, schema, this.config);
where = _query.getWhereQuery(collection);
} catch(e) {
log.error('Failed to compose destroy SQL query.', e);
Expand All @@ -585,8 +590,11 @@ module.exports = (function () {
.transform(transformers)
.return('BEFORE');

if(where){
query.where(where);
if(where.query[0]){
query.where(where.query[0]);
if(where.params){
query.addParams(where.params);
}
}

query.all()
Expand Down
34 changes: 24 additions & 10 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
var _ = require('lodash'),
utils = require('./utils'),
hop = utils.object.hasOwnProperty,
Sequel = require('waterline-sequel-orientdb');
Sequel = require('waterline-sequel-orientdb'),
RID = require('oriento').RID;


// waterline-sequel-orientdb options
var sqlOptions = {
parameterized : false,
parameterized : true,
caseSensitive : false,
escapeCharacter : '',
casting : false,
canReturnValues : false,
canReturnValues : true,
escapeInserts : true
};

Expand All @@ -26,7 +27,9 @@ var sqlOptions = {
* @param {Object} options
* @api private
*/
var Query = module.exports = function Query(options, schema) {
var Query = module.exports = function Query(options, schema, adapterConfig) {
// Apply configs
sqlOptions.parameterized = _.isUndefined(adapterConfig.options.parameterized) ? sqlOptions.parameterized : adapterConfig.options.parameterized;

// Cache the schema for use in parseTypes
this.schema = schema;
Expand Down Expand Up @@ -189,14 +192,20 @@ Query.prototype.processIdValue = function processIdValue(idValue) {
*
* @api public
* @param {String} collection
* @returns {String}
* @returns {Object}
*/
Query.prototype.getSelectQuery = function getSelectQuery(collection) {
var self = this;

var _query = self.sequel.find(collection, self.criteria);
_query.query[0] = _query.query[0].replace(collection.toUpperCase(), collection);
_query.params = _.reduce(_query.values[0], function(accumulator, value, index){
var paramValue = _.isString(value) && utils.matchRecordId(value) ? new RID(value) : value;
accumulator['param' + index] = paramValue;
return accumulator;
}, {});

return _query.query[0].replace(collection.toUpperCase(), collection);
return _query;
};


Expand All @@ -205,15 +214,20 @@ Query.prototype.getSelectQuery = function getSelectQuery(collection) {
*
* @api public
* @param {String} collection
* @returns {String}
* @returns {Object}
*/
Query.prototype.getWhereQuery = function getWhereQuery(collection) {
var self = this;

var _where = self.sequel.simpleWhere(collection, self.criteria, sqlOptions);
where = _where.query.replace('WHERE ', '');
var _where = self.sequel.find(collection, self.criteria);
_where.query[0] = _where.query[0].split('WHERE')[1];
_where.query[0] = _where.query[0] && _where.query[0].trim() !== '' ? _where.query[0] : null;
_where.params = _.reduce(_where.values[0], function(accumulator, value, index){
accumulator['param' + index] = value;
return accumulator;
}, {});

return where !== '' ? where : null;
return _where;
};


Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waterline-orientdb",
"version": "0.10.15",
"version": "0.10.16",
"description": "OrientDB adapter for Waterline / Sails.js ORM",
"main": "./lib/adapter.js",
"scripts": {
Expand Down Expand Up @@ -34,8 +34,8 @@
"lodash": "^2.4.1",
"oriento": "git://github.com/codemix/oriento",
"q": "^1.0.1",
"waterline-cursor": "^0.0.5",
"waterline-sequel-orientdb": ">=0.0.20"
"waterline-cursor": "~0.0.5",
"waterline-sequel-orientdb": "~0.0.20"
},
"devDependencies": {
"waterline-adapter-tests": "^0.10.4",
Expand Down
20 changes: 18 additions & 2 deletions test/integration-orientdb/tests/associations/manyThrough.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Association Interface', function() {
assert(!stadium.out_venueTable);
assert(!stadium.in_ownsTable);
assert(!stadium['@type']);
assert(!stadium.sponsor['@type']);
assert(_.isString(stadium.sponsor));
done();
})
.catch(function(err){
Expand All @@ -66,7 +66,7 @@ describe('Association Interface', function() {
assert(!stadiums[0].out_venueTable);
assert(!stadiums[0].in_ownsTable);
assert(!stadiums[0]['@type']);
assert(!stadiums[0].sponsor['@type']);
assert(_.isString(stadiums[0].sponsor));
done();
})
.catch(function(err){
Expand All @@ -89,6 +89,7 @@ describe('Association Interface', function() {
assert(!stadium.teams[0].in_venueTable);
assert(!stadium['@type']);
assert(stadium.sponsor);
assert(stadium.sponsor.name === 'someBrand');
assert(!stadium.sponsor['@type']);
done();
})
Expand All @@ -109,6 +110,7 @@ describe('Association Interface', function() {
assert(!stadiums[0].teams[0].in_venueTable);
assert(!stadiums[0]['@type']);
assert(stadiums[0].sponsor);
assert(stadiums[0].sponsor.name === 'someBrand');
assert(!stadiums[0].sponsor['@type']);
done();
})
Expand All @@ -123,13 +125,27 @@ describe('Association Interface', function() {
.then(function(stadium){
assert(!stadium['@type']);
assert(stadium.sponsor);
assert(stadium.sponsor.name === 'someBrand');
assert(!stadium.sponsor['@type']);
done();
})
.catch(function(err){
done(err);
});
});

it('findOne: should not have sponsor object', function(done) {
Associations.Stadium.findOne(stadiumRecord.id)
.populate('teams')
.then(function(stadium){
assert(stadium.teams.length === 1);
assert(_.isString(stadium.sponsor));
done();
})
.catch(function(err){
done(err);
});
});
});


Expand Down