load method

  1. @override
Future<Map<String, dynamic>?> load(
  1. Entity entity,
  2. Iterable<String>? fields,
  3. AccessOption? option
)
override

Loads the data of the given OID. *

    • fields - a collection of fields to load. If null, it means all.
  • If the plugin doesn't support partial load, it can ignore fields.
  • Note: it shall return a Future carrying null if not found.
    • option - an option for loading the entity.
  • It is the option passed from load and loadIfAny.
  • It can be application specific.
  • You can ignore it if not supported.
  • For SQL, it is better to supported null, forShare and forUpdate.

Implementation

@override
Future<Map<String, dynamic>?> load(Entity entity,
    Iterable<String>? fields, AccessOption? option) async {
  final sql = StringBuffer("select ");
  if (fields == null) {
    sql.write("*");
  } else if (fields.isEmpty) {
    sql.write("1");
  } else {
    assert(_assertNoDup(fields));

    bool first = true;
    for (final fd in fields) {
      if (first) first = false;
      else sql.write(',');

      if (StringUtil.isChar(fd[0], digit: true, match: "("))
        sql.write(fd);
      else
        sql..write('"')..write(fd)..write('"');
    }
  }

  sql..write(' from "')..write(entity.otype)
    ..write('" where "$fdOid"=@$fdOid limit 1');
  if (option == forUpdate)
    sql.write(' for update');
  else if (option == forShare)
    sql.write(' for share');

  await for (final row in access.query(sql.toString(), {fdOid: entity.oid})) {
    final data = HashMap<String, dynamic>();
    if (fields?.isNotEmpty ?? true)
      row.forEach((name, value) => data[name] = value);
    _cache?.put(entity); //update cache
    return data;
  }

  return null;
}