From edc28c5372f9f2d557ef83ceb73b243ba2463ccb Mon Sep 17 00:00:00 2001 From: XueSeason Date: Mon, 5 Jun 2017 17:55:42 +0800 Subject: [PATCH] add range condition --- README.md | 5 +++-- lib/operator.js | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 59d30b9..3abc3c3 100644 --- a/README.md +++ b/README.md @@ -183,14 +183,15 @@ let rows = yield db.select('table-name'); ```js let rows = yield db.select('table-name', { where: { - type: 'javascript' + type: 'javascript', + date: [{ op: '>=', value: '20170504'}] }, columns: ['author', 'title'], orders: [['id', 'desc']] }); => SELECT `author`, `title` FROM `table-name` - WHERE `type` = 'javascript' ORDER BY `id` DESC + WHERE `type` = 'javascript' AND `date` >= '20170504' ORDER BY `id` DESC ``` ### Delete diff --git a/lib/operator.js b/lib/operator.js index fc4243b..99755f1 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -191,18 +191,27 @@ proto._where = function(where) { for (const key in where) { const value = where[key]; if (Array.isArray(value)) { - wheres.push('?? IN (?)'); + if (value.length > 0 && typeof value[0] === 'object') { + for (let i = 0; i < value.length; i++) { + wheres.push('?? ' + value[i].op + ' ?'); + values.push(key); + values.push(value[i].value); + } + } else { + wheres.push('?? IN (?)'); + values.push(key); + values.push(value); + } } else { wheres.push('?? = ?'); + values.push(key); + values.push(value); } - values.push(key); - values.push(value); } if (wheres.length > 0) { return this.format(' WHERE ' + wheres.join(' AND '), values); } return ''; - }; proto._selectColumns = function(table, columns) {