MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / insert

Function insert

crates/bindings/src/table.rs:1350–1373  ·  view source on GitHub ↗
(mut row: T::Row, mut buf: IterBuf)

Source from the content-addressed store, hash-verified

1348/// Insert a row of type `T` into the table identified by `table_id`.
1349#[track_caller]
1350fn insert<T: Table>(mut row: T::Row, mut buf: IterBuf) -> Result<T::Row, TryInsertError<T>> {
1351 let table_id = T::table_id();
1352 // Encode the row as bsatn into the buffer `buf`.
1353 buf.clear();
1354 buf.serialize_into(&row).unwrap();
1355
1356 // Insert row into table.
1357 // When table has an auto-incrementing column, we must re-decode the changed `buf`.
1358 let res = sys::datastore_insert_bsatn(table_id, &mut buf).map(|gen_cols| {
1359 // Let the caller handle any generated columns written back by `sys::datastore_insert_bsatn` to `buf`.
1360 T::integrate_generated_columns(&mut row, gen_cols);
1361 row
1362 });
1363 res.map_err(|e| {
1364 let err = match e {
1365 sys::Errno::UNIQUE_ALREADY_EXISTS => {
1366 T::UniqueConstraintViolation::get().map(TryInsertError::UniqueConstraintViolation)
1367 }
1368 sys::Errno::AUTO_INC_OVERFLOW => T::AutoIncOverflow::get().map(TryInsertError::AutoIncOverflow),
1369 _ => None,
1370 };
1371 err.unwrap_or_else(|| panic!("unexpected insertion error: {e}"))
1372 })
1373}
1374
1375/// Update a row of type `T` to `row` using the index identified by `index_id`.
1376#[track_caller]

Callers

nothing calls this directly

Calls 6

clearMethod · 0.65
datastore_insert_bsatnFunction · 0.50
getFunction · 0.50
unwrapMethod · 0.45
serialize_intoMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…