ali-sdk / ali-rds

Aliyun RDS client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

update 有 bug

musicode opened this issue · comments

proto.update = function* (table, row, options) {
  // TODO: support multi rows
  options = options || {};
  if (!options.columns) {
    options.columns = Object.keys(row);
  }
  if (!options.where) {
    if (!('id' in row)) {
      throw new Error('Can not auto detect update condition, please set options.where, or make sure obj.id exists');
    }
    options.where = {
      id: row.id,
    };
  }

  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]);
  }
  const sql = this.format('UPDATE ?? SET ', [ table ]) +
    this.format(sets.join(', '), values) +
    this._where(options.where);
  debug('update(%j, %j, %j) \n=> %j', table, row, options, sql);
  return yield this.query(sql);
};

用例

update(
  'table',
  {
    status: 1,
  },
  {
    status: 0
  }
)

把所有未读的 status 改成已读,这里执行下来,sets 是个 []

这个库是不是不维护了。。。

@musicode

update(
'table',
{
status: 1,
},
{
where: {status: 0}
}
)

if (!options.columns) {
    options.columns = Object.keys(row);
  }
for (let i = 0; i < options.columns.length; i++) {
    const column = options.columns[i];
    if (column in options.where) {
      continue;
    }

这两句过滤了,所以没用

commented

嗯,我也遇到这个问题了,这种 update 的字段同时在 where 里的,目前都是自己拼的SQL……不太明白,库里为什么要加这个限制

我也感觉了,更改的时候,会出现错乱

这个库是不是不维护了,这样的bug都不修复了