Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add range condition #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date: { op: '>=', value: '20170504'}

这样会更好吧,可以参考一下业务关于 where 的 api 实现。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dead-horse @coolme200 对于 api 有什么好建议吗?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里用数组的考虑是什么?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date >= 100 AND date <= 200 这种需求么?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对,就是这个需求。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好也支持只传一个 object 的场景吧 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
Expand Down
17 changes: 13 additions & 4 deletions lib/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down