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

fix: operator.update, columns in where cant be updated #27

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
7 changes: 4 additions & 3 deletions lib/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ proto.update = function* (table, row, options) {
options.where = {
id: row.id,
};
const indexOfId = options.columns.indexOf('id');
if (indexOfId !== -1) {
[].splice.call(options.columns, indexOfId, 1);
}
}

const sets = [];
const values = [];
for (let i = 0; i < options.columns.length; i++) {
const column = options.columns[i];
if (column in options.where) {
continue;
}
sets.push('?? = ?');
values.push(column);
values.push(row[column]);
Expand Down
37 changes: 37 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,43 @@ describe('client.test.js', function() {
const row = yield this.db.get(table, { id: user.id });
assert.equal(row.email, user.email);
});

it('should update columns in options.where', function* () {
const user = yield this.db.get(table, {
name: prefix + 'fengmk2-update',
});
const result = yield this.db.update(table, {
name: prefix + 'fengmk2-update-1',
email: prefix + '[email protected]',
gmt_modified: this.db.literals.now,
}, {
where: {
id: user.id,
name: prefix + 'fengmk2-update',
email: user.email,
},
});
assert.equal(result.affectedRows, 1);

const row = yield this.db.get(table, {
id: user.id,
});

assert.equal(user.id, row.id);
assert.notEqual(row.name, user.name);
assert.notEqual(row.email, user.email);

const _query = this.db._query;
this.db._query = function* (sql) { return sql; };

const sql = yield this.db.update(table, {
id: user.id,
name: prefix + 'fengmk2-update-2',
email: prefix + '[email protected]',
});
assert.equal(sql.split('WHERE').shift().indexOf('`id` = ') === -1, true);
this.db._query = _query;
});
});

describe('delete(table, where)', function() {
Expand Down