MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / query

Function query

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

Source from the content-addressed store, hash-verified

442 }
443
444 fn query<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
445 if args.is_empty() {
446 return Err(VmError::RuntimeError(
447 "query() requires a SQL query string.".into(),
448 ));
449 }
450
451 let query = args[0].as_string()?;
452 let ctx = state.get_context();
453
454 // Execute query with the active transaction
455 let result = ACTIVE_TRANSACTION.with(|cell| {
456 if let Some(tx) = (*cell.borrow_mut()).as_mut() {
457 let rows = execute_query(
458 &mut **tx,
459 query.to_str().unwrap(),
460 args.into_iter().skip(1).collect(),
461 );
462 Some(rows)
463 } else {
464 None
465 }
466 });
467
468 match result {
469 Some(Ok(rows)) => {
470 // Convert rows to array of objects
471 let mut results = Vec::new();
472 for row in rows {
473 results.push(row_to_object(ctx, &row));
474 }
475 Ok(Value::array(&ctx, results))
476 }
477 Some(Err(e)) => Err(VmError::RuntimeError(format!("Database error: {e}"))),
478 None => Err(VmError::RuntimeError("No active transaction".into())),
479 }
480 }
481
482 fn query_as<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
483 if args.len() < 2 {

Callers 1

execute_queryFunction · 0.70

Calls 9

RuntimeErrorClass · 0.85
as_stringMethod · 0.80
get_contextMethod · 0.80
as_mutMethod · 0.80
to_strMethod · 0.80
pushMethod · 0.80
execute_queryFunction · 0.70
row_to_objectFunction · 0.70
borrow_mutMethod · 0.45

Tested by

no test coverage detected