From 367e4c8d8f7115d845e5c181d1c8cc309490723f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BC=82?= Date: Wed, 12 Dec 2018 11:09:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=AC=A6=E7=9A=84=E6=9D=A1=E4=BB=B6=EF=BC=8C=E5=A6=82like?= =?UTF-8?q?=E3=80=81=E5=A4=A7=E4=BA=8E=E3=80=81=E5=B0=8F=E4=BA=8E=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/operator.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/operator.js b/lib/operator.js index 51bf65a..b14de9c 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -316,13 +316,16 @@ proto._where = function(where) { const wheres = []; const values = []; for (const key in where) { - const value = where[key]; + let value = where[key]; if (Array.isArray(value)) { wheres.push('?? IN (?)'); } else { if (value === null || value === undefined) { wheres.push('?? IS ?'); - } else { + }else if (value && value.operator){ + wheres.push('?? ' + value.operator + ' ?'); + value = value.value; + }else { wheres.push('?? = ?'); } } From 28e6e2c797c57f2e0aefd590294776e52dab55e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BC=82?= Date: Wed, 12 Dec 2018 11:31:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E5=8F=8A=E8=B0=83=E6=95=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/operator.js | 4 ++-- test/operator.test.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/operator.js b/lib/operator.js index b14de9c..a3610b4 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -322,10 +322,10 @@ proto._where = function(where) { } else { if (value === null || value === undefined) { wheres.push('?? IS ?'); - }else if (value && value.operator){ + } else if (value && value.operator) { wheres.push('?? ' + value.operator + ' ?'); value = value.value; - }else { + } else { wheres.push('?? = ?'); } } diff --git a/test/operator.test.js b/test/operator.test.js index d39d00a..6848a43 100644 --- a/test/operator.test.js +++ b/test/operator.test.js @@ -25,6 +25,8 @@ describe('operator.test.js', function() { assert.equal(op._where({ id: 1, name: 'foo\'\"' }), ' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\''); assert.equal(op._where({ id: 1, name: 'foo\'\"', user: 'fengmk2' }), ' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\' AND `user` = \'fengmk2\''); + assert.equal(op._where({ name: { operator: 'like', value: '%wvv8oo%' } }), ' WHERE `name` like \'%wvv8oo%\''); + assert.equal(op._where({ age: { operator: '>', value: 10 } }), ' WHERE `age` > 20'); }); }); From 6ffc00377e8ef59a9caa06374d46e13c66c0bebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BC=82?= Date: Wed, 12 Dec 2018 11:37:01 +0800 Subject: [PATCH 3/3] update test case --- test/operator.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/operator.test.js b/test/operator.test.js index 6848a43..c71dc5b 100644 --- a/test/operator.test.js +++ b/test/operator.test.js @@ -26,7 +26,7 @@ describe('operator.test.js', function() { assert.equal(op._where({ id: 1, name: 'foo\'\"', user: 'fengmk2' }), ' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\' AND `user` = \'fengmk2\''); assert.equal(op._where({ name: { operator: 'like', value: '%wvv8oo%' } }), ' WHERE `name` like \'%wvv8oo%\''); - assert.equal(op._where({ age: { operator: '>', value: 10 } }), ' WHERE `age` > 20'); + assert.equal(op._where({ age: { operator: '>', value: 10 } }), ' WHERE `age` > 10'); }); });