From 0a619fb26328d469bf24a3683e56fb6d2f691632 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 16 Apr 2014 16:37:28 -0400 Subject: [PATCH 1/4] updateOrCreate assumes numeric primary key(s) I removed the check to see if a property is an ID field while building the values clause to allow it to work if primary key(s) are strings. --- lib/mysql.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index a026435..e796df7 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -213,11 +213,7 @@ MySQL.prototype.updateOrCreate = function (model, data, callback) { if (props[key] || mysql.id(model, key)) { var k = mysql.columnEscaped(model, key); var v; - if (!mysql.id(model, key)) { - v = mysql.toDatabase(props[key], data[key]); - } else { - v = data[key]; - } + v = mysql.toDatabase(props[key], data[key]); if (v !== undefined) { fieldsNames.push(k); fieldValues.push(v); From 1103a9e9195976791132268731f53c39d927cd28 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 14 May 2014 21:21:00 -0700 Subject: [PATCH 2/4] Add support for logical operators (AND/OR) --- lib/mysql.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index e796df7..502e6be 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -388,11 +388,22 @@ MySQL.prototype.getColumns = function (model, props) { return names.join(', '); }; -function buildWhere(self, model, conds) { +MySQL.prototype.buildWhere = function (model, conds) { + var self = this; var props = self._models[model].properties; var cs = []; Object.keys(conds).forEach(function (key) { + if (key === 'and' || key === 'or') { + var clauses = conds[key]; + if (Array.isArray(clauses)) { + clauses = clauses.map(function (c) { + return '(' + self.buildWhere(model, c) + ')'; + }); + return clauses.join(' ' + key.toUpperCase() + ' '); + } + // The value is not an array, fall back to regular fields + } var keyEscaped = self.columnEscaped(model, key); var val = self.toDatabase(props[key], conds[key]); if (conds[key] === null || conds[key] === undefined) { @@ -482,7 +493,7 @@ MySQL.prototype.all = function all(model, filter, callback) { if (filter) { if (filter.where) { - sql += ' ' + buildWhere(self, model, filter.where); + sql += ' ' + self.buildWhere(model, filter.where); } if (filter.order) { @@ -528,8 +539,9 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) { where = undefined; } this.query('DELETE FROM ' - + this.tableEscaped(model) + ' ' + buildWhere(this, model, where || {}), function (err, data) { - callback && callback(err, data); + + this.tableEscaped(model) + ' ' + this.buildWhere(model, where || {}), + function (err, data) { + callback && callback(err, data); }.bind(this)); }; From 7332f129729450e814e2f993080c62029bffad12 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 15 May 2014 10:32:31 -0700 Subject: [PATCH 3/4] Fix buildWhere --- lib/mysql.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 502e6be..7033a80 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -389,6 +389,11 @@ MySQL.prototype.getColumns = function (model, props) { }; MySQL.prototype.buildWhere = function (model, conds) { + var where = this._buildWhere(model, conds); + return where? 'WHERE ' + where : ''; +}; + +MySQL.prototype._buildWhere = function (model, conds) { var self = this; var props = self._models[model].properties; @@ -398,9 +403,9 @@ MySQL.prototype.buildWhere = function (model, conds) { var clauses = conds[key]; if (Array.isArray(clauses)) { clauses = clauses.map(function (c) { - return '(' + self.buildWhere(model, c) + ')'; + return '(' + self._buildWhere(model, c) + ')'; }); - return clauses.join(' ' + key.toUpperCase() + ' '); + return cs.push(clauses.join(' ' + key.toUpperCase() + ' ')); } // The value is not an array, fall back to regular fields } @@ -450,7 +455,7 @@ MySQL.prototype.buildWhere = function (model, conds) { if (cs.length === 0) { return ''; } - return 'WHERE ' + cs.join(' AND '); + return cs.join(' AND '); } function buildOrderBy(self, model, order) { From 0910d2aee94b1727b72ab6215dd1fa8efdaece61 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 16 May 2014 09:59:39 -0700 Subject: [PATCH 4/4] Bump versions --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a482007..2f366ef 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "loopback-connector-mysql", - "version": "1.2.0", + "version": "1.2.1", "description": "MySQL connector for loopback-datasource-juggler", "main": "index.js", "scripts": { "test": "mocha" }, "dependencies": { - "mysql": "~2.1.1", - "async": "~0.7.0", + "mysql": "~2.3.0", + "async": "~0.9.0", "debug": "~0.8.0" }, "devDependencies": {