MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / begin_transaction

Function begin_transaction

aiscript-vm/src/stdlib/db/pg.rs:418–442  ·  view source on GitHub ↗
(
        state: &mut State<'gc>,
        _args: Vec<Value<'gc>>,
    )

Source from the content-addressed store, hash-verified

416 }
417
418 pub(super) fn begin_transaction<'gc>(
419 state: &mut State<'gc>,
420 _args: Vec<Value<'gc>>,
421 ) -> Result<Value<'gc>, VmError> {
422 // Check if there's already an active transaction
423 let has_active = ACTIVE_TRANSACTION.with(|tx| tx.borrow().is_some());
424 if has_active {
425 return Err(VmError::RuntimeError("Transaction already active".into()));
426 }
427
428 let ctx = state.get_context();
429 let conn = state.pg_connection.as_ref().unwrap();
430 let tx = Handle::current()
431 .block_on(async move { conn.begin().await })
432 .map_err(|e| VmError::RuntimeError(format!("Failed to begin transaction: {}", e)))?;
433
434 // Store transaction in thread local
435 ACTIVE_TRANSACTION.with(|cell| {
436 *cell.borrow_mut() = Some(tx);
437 });
438
439 // Create and return new instance
440 let instance = Instance::new(create_transaction_class(ctx));
441 Ok(Value::Instance(Gc::new(&ctx, RefLock::new(instance))))
442 }
443
444 fn query<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
445 if args.is_empty() {

Callers

nothing calls this directly

Calls 7

RuntimeErrorClass · 0.85
InstanceClass · 0.85
get_contextMethod · 0.80
create_transaction_classFunction · 0.70
borrowMethod · 0.45
as_refMethod · 0.45
borrow_mutMethod · 0.45

Tested by

no test coverage detected