Skip to content
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
3 changes: 1 addition & 2 deletions lib/db/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ function recordIdResolver () {
return obj.map(replaceRecordIds.bind(this, records));
}
else if (obj instanceof RIDBag) {
/*jshint validthis:true */
obj._prefetchedRecords = records;
return obj;
}
Expand All @@ -167,10 +166,10 @@ function recordIdResolver () {
seen[obj['@rid']] = obj;
}
}

var keys = Object.keys(obj),
total = keys.length,
i, key, value;

for (i = 0; i < total; i++) {
key = keys[i];
value = obj[key];
Expand Down
15 changes: 13 additions & 2 deletions lib/db/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Statement.prototype.buildStatement = function () {
if (state.upsert) {
statement.push('UPSERT');
}

if ((state.update || state.insert || state.delete) && state.return) {
statement.push('RETURN ' + state.return);
}
Expand Down Expand Up @@ -472,6 +472,17 @@ Statement.prototype.buildStatement = function () {
return item;
}
}
else if (item && typeof item === 'object') {
var keys = Object.keys(item),
length = keys.length,
parts = new Array(length),
key, i;
for (i = 0; i < length; i++) {
key = keys[i];
parts.push(key, item[key]);
}
return parts.join(' ');
}
else {
return ''+item;
}
Expand Down Expand Up @@ -559,7 +570,7 @@ Statement.prototype._objectToCondition = function (obj, operator) {
key = keys[i];
paramName = 'param' + paramify(key) + (this._state.paramIndex++);
conditions.push(key + ' ' + operator + ' :' + paramName);
this.addParam(paramName, obj[key] instanceof RID ? ''+obj[key] : obj[key]);
this.addParam(paramName, obj[key]);
}

if (conditions.length === 0) {
Expand Down
3 changes: 2 additions & 1 deletion lib/transport/binary/protocol/operations/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var Operation = require('../operation'),
constants = require('../constants'),
serializer = require('../serializer'),
writer = require('../writer');
writer = require('../writer'),
RID = require('../../../../recordid');

module.exports = Operation.extend({
id: 'REQUEST_COMMAND',
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ exports.prepare = function (query, params) {
if (!params) {
return query;
}
var pattern = /"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|:([A-Za-z][A-Za-z0-9_-]*|\/\*[\s\S]*?\*\/)/g;
var pattern = /"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|\s:([A-Za-z][A-Za-z0-9_-]*|\/\*[\s\S]*?\*\/)/g;
return query.replace(pattern, function (all, double, single, param) {
if (param) {
return exports.encode(params[param]);
return ' ' + exports.encode(params[param]);
}
else {
return all;
Expand Down
12 changes: 12 additions & 0 deletions test/core/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var utils = require('../../lib/utils');

describe('utils.prepare', function () {
it("should prepare SQL statements", function () {
utils.prepare("select from index:foo").should.equal("select from index:foo");
});
it("should prepare SQL statements with parameters", function () {
utils.prepare("select from index:foo where key = :key", {key: 123}).should.equal("select from index:foo where key = 123");
});
});
7 changes: 7 additions & 0 deletions test/db/query-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ describe("Database API - Query", function () {
user.name.should.equal('reader');
});
});
it('should select a record by its RID', function () {
return this.db.select().from('OUser').where({'@rid': new LIB.RID('#5:0')}).one()
.then(function (user) {
expect(typeof user).to.equal('object');
user.name.should.equal('admin');
});
});
it('should select a user with a fetch plan', function () {
return this.db.select().from('OUser').where({name: 'reader'}).fetch({roles: 3}).one()
.then(function (user) {
Expand Down