3

I'm new to rust and was working on a generic database viewer app using SQLX. Now I've stumbled upon a problem. While querying like a simple

SELECT * FROM TABLE_NAME

Now the problem is that I don't know the type of rows and columns before hand. So how should I parse the queries?

I've tried the following from this post

                for r in row {
                    let mut row_result: Vec<String> = Vec::new();
                    for col in r.columns() {
                        let value = r.try_get_raw(col.ordinal()).unwrap();
                        let value = match value.is_null() {
                            true => "NULL".to_string(),
                            false => {
                                let mat = value.as_str();
                                match mat {
                                    Ok(m) => m.to_string(),
                                    Err(err) => {
                                        dbg!(err);
                                        "ERROR".to_string()
                                    }
                                }
                            }
                        };
                        // println!("VALUE-- {:?}", value);
                        row_result.push(value);
                    }
                    result.push(row_result);
                }

The problem is that for some columns it's returning like this. Like for the ID columns

"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0004"

and for some i'm getting the following error in the dbg! macro

Utf8Error {
    valid_up_to: 2,
    error_len: Some(
        1,
    ),
}

Anyone can help me here? BTW I'm using Postgres so all row types are of PgRow

2: Follow up. I was able to get the types of the columns from the information_schema but the problem seems to be that those will be in String and I couldn't find any way to convert those into rust types, like INT8 -> i64, TEXT -> String. Something like that.

Ayan Banerjee
  • 151
  • 1
  • 3
  • 11

0 Answers0