From fe37c5c7c6bad068a460eb7c3de22c3d210b583c Mon Sep 17 00:00:00 2001 From: Charles Pick Date: Sun, 30 Nov 2014 02:44:33 +0000 Subject: [PATCH 1/3] don't cast RID to string --- lib/db/statement.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/db/statement.js b/lib/db/statement.js index 3d5fbd8..5f48d07 100644 --- a/lib/db/statement.js +++ b/lib/db/statement.js @@ -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); } @@ -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; } @@ -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) { From 02f92689f1ef1502e1b2d7c3c97192063342beb7 Mon Sep 17 00:00:00 2001 From: Charles Pick Date: Sun, 30 Nov 2014 02:44:56 +0000 Subject: [PATCH 2/3] fix named param regexp --- lib/utils.js | 4 ++-- test/core/utils.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/core/utils.js diff --git a/lib/utils.js b/lib/utils.js index c4e18da..7c06567 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; diff --git a/test/core/utils.js b/test/core/utils.js new file mode 100644 index 0000000..f30c7c7 --- /dev/null +++ b/test/core/utils.js @@ -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"); + }); +}); \ No newline at end of file From 95acdd891b171e18c501f0b7f68ae2a566af0cd2 Mon Sep 17 00:00:00 2001 From: Charles Pick Date: Mon, 1 Dec 2014 11:49:26 +0000 Subject: [PATCH 3/3] RID fixes --- lib/db/record.js | 3 +-- lib/transport/binary/protocol/operations/command.js | 3 ++- test/db/query-test.js | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/db/record.js b/lib/db/record.js index 136b589..5bebcf5 100644 --- a/lib/db/record.js +++ b/lib/db/record.js @@ -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; } @@ -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]; diff --git a/lib/transport/binary/protocol/operations/command.js b/lib/transport/binary/protocol/operations/command.js index a148481..343400a 100644 --- a/lib/transport/binary/protocol/operations/command.js +++ b/lib/transport/binary/protocol/operations/command.js @@ -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', diff --git a/test/db/query-test.js b/test/db/query-test.js index 5a2868f..43151fe 100644 --- a/test/db/query-test.js +++ b/test/db/query-test.js @@ -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) {