Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Thom Chiovoloni
    @thomcc
    (oh sorry for the ping, i just realized this convo is only a minute old so you're probably still around)
    hutchisr
    @hutchisr
    passing the vec by reference or using as_slice, etc. still gets a type mismatch. That does work if I'm just using an unnamed &Vec<Box<dyn ToSql>> but not for the named variant. I was trying to avoid that, however, since the complexity of the query and user input means I can't used numbered parameters and just using ? parameters results in numerous copies of several parameters that get used multiple times in the query
    Thom Chiovoloni
    @thomcc
    right, you'd have to transmute actually
    like, unsafe
    it should be sound but
    yeah
    hmm
    let me check if coherence would allow me to fix this for you with the way we reworked params on the master branch
    hutchisr
    @hutchisr
    @thomcc thanks for your assistance, not too big of a deal for me to use ordered parameters for the moment but if this does become more straightforward in a future version that will be great
    Thom Chiovoloni
    @thomcc
    @hutchisr ah
    well
    i just wrote a patch fixing your issue for the master branch
    šŸ˜…
    Thom Chiovoloni
    @thomcc

    if this does become more straightforward in a future version that will be great

    The main question is whether or not this causes people to get wierd type inference errors when using named params directly

    right now the code on master is a big (mostly compatible, or at least with small changes) change from the current API design, mostly intended to get rid of NO_PARAMS and the duplication of _named vs non-_named functionality
    but it relies on type inference a lot
    Scott Bronson
    @bronson
    I'm trying to recognize when there's a constraint violation or a missing table error... but the code I'm ending up with is large duplication: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a8b93f8e39f0716b61ae07d02285c001
    Is this the right way to match 2 errors? Or am I making this harder than it needs to be?
        let rows = match result {
            Ok(rows) => rows,
    
            Err(e @
                rusqlite::Error::SqliteFailure(
                    libsqlite3_sys::Error { extended_code: 2067, .. },
                    _,
                ),
            ) => panic!("constraint violation: {:#?}", e),
    
            Err(
                rusqlite::Error::SqliteFailure(
                    libsqlite3_sys::Error { extended_code: 1, .. },
                    msg,
                ),
            ) => panic!("no such table: {:#?}", msg.unwrap()),
    
            _ => panic!("some other error: {:#?}", result)
        };
    Scott Bronson
    @bronson
    (That's mostly cribbed from rusqlite/rusqlite#760 )
    Thom Chiovoloni
    @thomcc
    seems fine. someday iā€™d like to improve the error type since its a bit wonky, but yeah what you have now is likely best
    you might be able to use constants instead of the integer literals though
    Scott Bronson
    @bronson
    Hah, yes, it doesn't seem very ergonomic.
    I've been having difficulty getting the constants to work. I'll try again.
    Thanks!
    Thom Chiovoloni
    @thomcc

    either

            Err(e @
                rusqlite::Error::SqliteFailure(
                    libsqlite3_sys::Error { extended_code: SOME_CONSTANT, .. },
                    _,
                ),
            ) => ...

    or

            Err(e @
                rusqlite::Error::SqliteFailure(
                    libsqlite3_sys::Error { extended_code: libsqlite3_sys::SOME_CONSTANT, .. },
                    _,
                ),
            ) => ...

    should work

    in general rust doesn't have the best erogonomics with matching complex types. you see this here but also when working with certain libraries ā€” event handling with winit requires very complex match statements like this too
    i don't know a great solution honestly
    i think most of my goals for improving the error type wouldn't really mke this problem go away
    Scott Bronson
    @bronson
    image.png
    I can use libsqlite3_sys all day on the Rust playground... but when I try to do it locally, I get
    26 |                 libsqlite3_sys::Error { extended_code: libsqlite3_sys::SQLITE_ERROR, .. },
       |                 ^^^^^^^^^^^^^^ use of undeclared crate or module `libsqlite3_sys`
    Scott Bronson
    @bronson
    Just did this:
    • cargo new /tmp/rusqtest
    • echo 'rusqlite = "0.25"' >> /tmp/rusqtest/Cargo.toml
    • echo 'fn main() { libsqlite3_sys::SQLITE_CONSTRAINT_UNIQUE }' > /tmp/rusqtest/src/main.rs
    cd /tmp/rusqtest; cargo build And I get that error.
    Is there something weird about my system?
    Thom Chiovoloni
    @thomcc
    ah, sorry
    I didnt get notifications about these and had accidentally closed the pinned tab for gitter, and mostly forgotten about it
    @bronson you probably need to enable the "bundled" feature
    did you, uh, figure that out
    sorry for not being around
    Moritz Helm
    @mohelm

    Hey and thanks for your help in advance.

    use chrono::prelude::*;
    use clap::{AppSettings, Clap};
    use rusqlite::{params, Connection, Result, NO_PARAMS};
    
    struct Cell {
        text: String,
        start_time: DateTime<Utc>,
        end_time: DateTime<Utc>,
        date: Date<Utc>,
        revision: i32,
    }

    Hey, I have a code like the above and the sqllite table looks like this:

    1|2021-05-02 09:15:00 UTC|2021-05-02 14:45:00 UTC|2021-05-02UTC|Once again - what?|1

    I am having trouble extracting into start_time (the first column). I am trying:

        let mut stmt = conn.prepare("Select start_time, end_time, date, text, revision from cells")?;
        let cells_iter = stmt.query_map([], |row| {
            Ok(Cell {
                start_time: row.get(0)?,
                end_time: row.get(1)?,
                date: row.get(2),
                text: row.get(3)?,
                revision: row.get(4)?,
            })
        })?;

    But this does not work. How can I go about this (sorry, am a total noob)?
    Many thanks.

    Thom Chiovoloni
    @thomcc
    can you elaborate on "doesn't work"?
    Moritz Helm
    @mohelm
    Hey thanks - this is the error:
    [rustc E0308] [E] mismatched types  
    expected struct `chrono::Date`, found enum `std::result::Result`
    note: expected struct `chrono::Date<chrono::Utc>`  
    found enum `std::result::Result<_, rusqlite::Error>`
    Thom Chiovoloni
    @thomcc
    oh right
    you forgot the ? after row.get(2), e.g. that line should be date: row.get(2)?,
    Moritz Helm
    @mohelm
    hey thanks - than I get this compiler error:
    
    error[E0277]: the trait bound `chrono::Date<chrono::Utc>: FromSql` is not satisfied
       --> src/main.rs:106:23
        |
    106 |             date: row.get(2)?,
        |                       ^^^ the trait `FromSql` is not implemented for `chrono::Date<chrono::Utc>`
    
    error: aborting due to previous error; 2 warnings emitted
    
    For more information about this error, try `rustc --explain E0277`.
    error: could not compile `morg`
    i think however that it also does not manage to pass 0, 1 correctly from my checks
    you kind of assuming it should parse stuff automatically, right? Can I also try to parse the content of row.get(2) manually?
    Thom Chiovoloni
    @thomcc

    i think we should be able to read it out if you turn on the "chrono" cargo feature, but you're totally able to get a String or whatever out and parse it how you want:

       let cells_iter = stmt.query_map([], |row| {
            let date: String = row.get(2)?;
            Ok(Cell {
                start_time: row.get(0)?,
                end_time: row.get(1)?,
                date: parse_date(date),
                text: row.get(3)?,
                revision: row.get(4)?,
            })
        })?;

    (where parse_date is a function you write)

    or use from chrono
    or something