update method

Future<List<T>> update(
  1. QueryExecutor executor
)

Implementation

Future<List<T>> update(QueryExecutor executor) async {
  var updateSql = StringBuffer('UPDATE $tableName ');
  var valuesClause = values?.compileForUpdate(this);

  if (valuesClause == '') {
    throw StateError('No values have been specified for update.');
  }
  updateSql.write(' $valuesClause');
  var whereClause = where?.compile();
  if (whereClause?.isNotEmpty == true) {
    updateSql.write(' WHERE $whereClause');
  }
  if (_limit != null) updateSql.write(' LIMIT $_limit');

  var returning = fields.map(adornWithTableName).join(', ');
  var sql = compile({});
  var returningSql = '';
  if (executor.dialect is PostgreSQLDialect) {
    sql = 'WITH $tableName as ($updateSql RETURNING $returning) $sql';
  } else if (executor.dialect is MySQLDialect) {
    returningSql = sql;
    sql = '$updateSql';
  } else {
    throw ArgumentError("Unsupported database dialect.");
  }
  //_log.fine("Update Query = $sql");

  return executor
      .query(tableName, sql, substitutionValues, returningQuery: returningSql)
      .then((it) => deserializeList(it));
}