Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get raw blob data? #5

Closed
swuecho opened this issue Jul 13, 2018 · 4 comments
Closed

get raw blob data? #5

swuecho opened this issue Jul 13, 2018 · 4 comments

Comments

@swuecho
Copy link

swuecho commented Jul 13, 2018

I have multiple f32 stored together as blob. it there a way to read the raw blob. or even better to read the f32 to a vec.

I figure out sqlValue.get<f32>() should get a single one, but I have multiple of them. Thanks.

@kubo
Copy link
Owner

kubo commented Jul 13, 2018

I was going to suggest that "Get the blob data as Vec<u8> and convert them to Vec<f32>.
However it couldn't. Fetching RAW data type columns as Vec<u8> is available but fetching BLOB data type columns as Vec<u8> is missing. I'll fix it at the next release.

Instead of it, get the blob data as String and convert them to Vec<f32>. When binary data are fetched as string, they are converted to hexadecimal strings as sqlplus does. So you can get f32 data in blob as hexadecimal string.

  1. Add hex crate to Cargo.toml
    [dependencies]
    hex = "0.3"
    
  2. Create a conversion function from hexadecimal string to Vec<f32>.
    extern crate hex;
    use std::mem;
    use std::slice;
    
    fn hexstr_to_f32vec(s: &str) -> Vec<f32> {
        let raw = hex::decode(s).unwrap();
        let len = raw.len() / mem::size_of::<f32>();
        unsafe { slice::from_raw_parts(raw.as_ptr() as *const f32, len) }.to_vec()
    }
  3. Fetch blob data as String and convert it to Vec<f32>
    let conn = oracle::Connection::connect(username, password, database, &[]).unwrap();
    let hexstr = conn.query_row_as::<String>("select blob_column from table_name where ...", &[]).unwrap());
    let f32vec = hexstr_to_f32vec(&hexstr);
  4. Otherwise, fetch blob data as a custom type implementing FromSql trait.
    #[derive(Debug)]
    struct CustomType(Vec<f32>);
    
    impl oracle::FromSql for CustomType {
        fn from_sql(val: &oracle::SqlValue) -> oracle::Result<CustomType> {
            let hexstr = val.get::<String>()?;
            Ok(CustomType(hexstr_to_f32vec(&hexstr)))
        }
    }
    
    ...
    
    let conn = oracle::Connection::connect(username, password, database, &[]).unwrap();
    let custom_type = conn.query_row_as::<CustomType>("select blob_column from table_name where ...", &[]).unwrap());
    let f32vec = custom_type.0;

@swuecho
Copy link
Author

swuecho commented Jul 15, 2018

Thanks for the detailed answer.

the approach works great. the only problem is the endianess.

fn hexstr_to_f32vec(s: &str) -> Vec<f32> {
    let raw = hex::decode(s).unwrap();
    let len = raw.len() / mem::size_of::<f32>();
    unsafe { slice::from_raw_parts(raw.as_ptr() as *const f32, len) }.to_vec()
}

the raw is in bigendian. so I have to add raw.reverse to get the right value.

also, get the blob value as Vec<u8> is great, will save the round trip conversion.

@swuecho swuecho closed this as completed Jul 15, 2018
@kubo
Copy link
Owner

kubo commented Jul 16, 2018

@swuecho
oracle 0.1.1 was published. It can get blob as Vec<u8>.

I checked that bigendian f32 values in blob could be fetch as Vec<f32> by the following code.

use std::mem;
use std::slice;

fn raw_to_f32vec(raw: &[u8]) -> Vec<f32> {
    let len = raw.len() / mem::size_of::<f32>();
    let mut f32vec = Vec::with_capacity(len);
    for u32val in unsafe { slice::from_raw_parts(raw.as_ptr() as *const u32, len) } {
        f32vec.push(f32::from_bits(u32::from_be(*u32val)))
    }
    f32vec
}

...

let conn = oracle::Connection::connect(username, password, database, &[]).unwrap();
let raw = conn.query_row_as::<Vec<u8>>("select blob_column from table_name where ...", &[]).unwrap());
let f32vec = raw_to_f32vec(&raw);

@swuecho
Copy link
Author

swuecho commented Jul 17, 2018

Thanks @kubo. this works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants