put method

int put (
  1. T object,
  2. {_PutMode mode: _PutMode.Put}
)

Puts the given Object in the box (aka persisting it). If this is a new entity (its ID property is 0), a new ID will be assigned to the entity (and returned). If the entity was already put in the box before, it will be overwritten.

Performance note: if you want to put several entities, consider putMany instead.

Implementation

int put(T object, {_PutMode mode = _PutMode.Put}) {
  var propVals = _entityReader(object);

  int id = propVals[_modelEntity.idProperty.name];
  if (id == null || id == 0) {
    id = bindings.obx_box_id_for_put(_cBox, 0);
    if (id == 0) throw latestNativeError();
    propVals[_modelEntity.idProperty.name] = id;
  }

  // put object into box and free the buffer
  // ignore: omit_local_variable_types
  final Pointer<OBX_bytes> bytesPtr = _fbManager.marshal(propVals);
  try {
    final bytes = bytesPtr.ref;
    checkObx(bindings.obx_box_put5(
        _cBox, id, bytes.ptr, bytes.length, _getOBXPutMode(mode)));
  } finally {
    // because fbManager.marshal() allocates the inner bytes, we need to clean those as well
    OBX_bytes.freeManaged(bytesPtr);
  }
  return id;
}