Skip to content

Commit

Permalink
Add convenience function to return errors for sqlite contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
z33ky committed Sep 11, 2020
1 parent e9df8e7 commit a6384ea
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions diesel/src/sqlite/connection/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ extern "C" fn run_custom_function<F>(
let f = match data_ptr.as_mut() {
Some(f) => f,
None => {
ffi::sqlite3_result_error(
ctx,
NULL_DATA_ERR.as_ptr() as *const _ as *const _,
NULL_DATA_ERR.len() as _,
);
context_error_str(ctx, NULL_DATA_ERR);
return;
}
};
Expand All @@ -259,19 +255,15 @@ extern "C" fn run_custom_function<F>(
internal_connection: conn,
},
None => {
ffi::sqlite3_result_error(
ctx,
NULL_DATA_ERR.as_ptr() as *const _ as *const _,
NULL_DATA_ERR.len() as _,
);
context_error_str(ctx, NULL_DATA_ERR);
return;
}
};
match f(&conn, args) {
Ok(value) => value.result_of(ctx),
Err(e) => {
let msg = e.to_string();
ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _);
context_error_str(ctx, &msg);
}
}

Expand Down Expand Up @@ -352,11 +344,11 @@ extern "C" fn run_aggregator_step_function<ArgsSqlType, RetSqlType, Args, Ret, A
Ok(Ok(())) => (),
Ok(Err(e)) => {
let msg = e.to_string();
unsafe { ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _) };
context_error_str(ctx, &msg);
}
Err(_) => {
let msg = format!("{}::step() panicked", std::any::type_name::<A>());
ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _);
context_error_str(ctx, &msg);
}
};
}
Expand Down Expand Up @@ -397,11 +389,11 @@ extern "C" fn run_aggregator_final_function<ArgsSqlType, RetSqlType, Args, Ret,
Ok(Ok(value)) => value.result_of(ctx),
Ok(Err(e)) => {
let msg = e.to_string();
ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _);
context_error_str(ctx, &msg);
}
Err(_) => {
let msg = format!("{}::finalize() panicked", std::any::type_name::<A>());
ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _);
context_error_str(ctx, &msg);
}
}
}
Expand All @@ -410,11 +402,11 @@ extern "C" fn run_aggregator_final_function<ArgsSqlType, RetSqlType, Args, Ret,
unsafe fn null_aggregate_context_error(ctx: *mut ffi::sqlite3_context) {
static NULL_AG_CTX_ERR: &str = "An unknown error occurred. sqlite3_aggregate_context returned a null pointer. This should never happen.";

ffi::sqlite3_result_error(
ctx,
NULL_AG_CTX_ERR.as_ptr() as *const _ as *const _,
NULL_AG_CTX_ERR.len() as _,
);
context_error_str(ctx, NULL_AG_CTX_ERR)
}

unsafe fn context_error_str(ctx: *mut ffi::sqlite3_context, error: &str) {
ffi::sqlite3_result_error(ctx, error.as_ptr() as *const _, error.len() as _);
}

#[allow(warnings)]
Expand Down

0 comments on commit a6384ea

Please sign in to comment.