diff --git a/stdlib/src/sqlite.rs b/stdlib/src/sqlite.rs index e56ddc5c..2b4c677e 100644 --- a/stdlib/src/sqlite.rs +++ b/stdlib/src/sqlite.rs @@ -12,6 +12,17 @@ thread_local! { static NEXT_ID: AtomicI64 = AtomicI64::new(1); +fn runtime_to_sql_value(v: &RuntimeValue) -> rusqlite::types::Value { + match v { + RuntimeValue::Null => rusqlite::types::Value::Null, + RuntimeValue::Bool(b) => rusqlite::types::Value::Integer(if *b { 1 } else { 0 }), + RuntimeValue::Int(i) => rusqlite::types::Value::Integer(*i), + RuntimeValue::Float(f) => rusqlite::types::Value::Real(*f), + RuntimeValue::Str(s) => rusqlite::types::Value::Text(s.clone()), + _ => rusqlite::types::Value::Text(v.to_string()), + } +} + impl StdlibRegistry { pub fn register_sqlite(&mut self) { let mut exports: HashMap> = @@ -62,35 +73,27 @@ impl StdlibRegistry { Vec::new() }; + let params: Vec = if let Some(arg) = args.get(2) { + if let RuntimeValue::List { items, .. } = arg { + items.borrow().iter().map(runtime_to_sql_value).collect() + } else { + return Err(RuntimeError::new( + RuntimeErrorKind::TypeMismatch { + expected: "list".to_string(), + found: arg.runtime_type().to_string(), + }, + None, + None, + )); + } + } else { + Vec::new() + }; + CONNECTIONS.with(|m| { let mut map = m.borrow_mut(); if let Some(conn) = map.get_mut(&id) { - let params_converted: Vec = params_list - .iter() - .map(|p| match p { - RuntimeValue::Null => rusqlite::types::Value::Null, - RuntimeValue::Bool(b) => { - rusqlite::types::Value::Integer(if *b { 1 } else { 0 }) - } - RuntimeValue::Int(i) => rusqlite::types::Value::Integer(*i), - RuntimeValue::Float(f) => rusqlite::types::Value::Real(*f), - RuntimeValue::Str(s) => rusqlite::types::Value::Text(s.clone()), - _ => rusqlite::types::Value::Null, - }) - .collect(); - let params_refs: Vec<&dyn rusqlite::types::ToSql> = params_converted - .iter() - .map(|p| p as &dyn rusqlite::types::ToSql) - .collect(); - - conn.execute(&sql, params_refs.as_slice()).map_err(|e| { - RuntimeError::new( - RuntimeErrorKind::InvalidOperation(e.to_string()), - None, - None, - ) - })?; Ok(()) } else { Err(RuntimeError::new( @@ -131,6 +134,23 @@ impl StdlibRegistry { Vec::new() }; + let params: Vec = if let Some(arg) = args.get(2) { + if let RuntimeValue::List { items, .. } = arg { + items.borrow().iter().map(runtime_to_sql_value).collect() + } else { + return Err(RuntimeError::new( + RuntimeErrorKind::TypeMismatch { + expected: "list".to_string(), + found: arg.runtime_type().to_string(), + }, + None, + None, + )); + } + } else { + Vec::new() + }; + let rows = CONNECTIONS.with(|m| { let mut map = m.borrow_mut(); if let Some(conn) = map.get_mut(&id) { @@ -167,7 +187,7 @@ impl StdlibRegistry { let mut rows = Vec::new(); let row_iter = stmt - .query_map(params_refs.as_slice(), |row| { +<<<<< let mut map = IndexMap::new(); for i in 0..col_count { let name = col_names[i].clone();