this post was submitted on 14 Sep 2024
823 points (99.0% liked)

Programmer Humor

32051 readers
1655 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Cian@hispagatos.space 1 points 5 days ago (2 children)

@bappity @RustyNova I was stuck on the same thing, there's no way to make it compatible? How do you handle dates?

[–] RustyNova@lemmy.world 2 points 4 days ago* (last edited 4 days ago)

No idea for Tiberius, but for SQLite I'm stuck with converting to timestamp and back. Ugly but works

P.S. add a getter to your data struct and you can be "seamless"

[–] bappity@lemmy.world 1 points 4 days ago* (last edited 4 days ago)

I switched to using tiberius

bit different but not too hard don't have my code on hand atm but this is how I started with it

    let mut config = Config::new();
    config.host("your_server_name");
    config.database("your_database_name");
    config.authentication(tiberius::AuthMethod::sql_server("your_username", "your_password"));
    config.trust_cert();

    let tcp = TcpStream::connect(config.get_addr()).await?;
    tcp.set_nodelay(true)?;
    
    let mut client = Client::connect(config, tcp.compat_write()).await?;

then I did something along the lines of

fn main() {
        let stream = client.query(&query, &[]).await?;
        let rows = stream.into_first_result().await?;

        let db_data: Vec<MyObject> = rows.into_iter().map(mapping_function_i_made_for_myobject).collect();
}

fn mapping_function_i_made_for_myobject(row: Row) -> MyObject {
    MyObject {
        my_date_field: row.get::<NaiveDateTime, _>("my_date_field").map(|dt| Local.from_local_datetime(&dt).unwrap()),
    }
}