Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    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
    (i've never used chrono ā€” I'm not the one who added those bits to rusqlite)
    turning on the chrono feature, if you dont have it already, is just rusqlite = { version = "...", features = ["chrono"] }, or something like that (if you already have featuers, just add "chrono" to the list).
    ah
    not sure we parse Date<Utc>
    we parse DateTime<Utc>
    id accept a patch adding that, but am probably not going to add it myself. in the mean time, i'd just handle it yourself.
    Moritz Helm
    @mohelm
    hey - many thanks. I managed to get it done for timestamps that are naive with respect to the timzone (which is OK for me for a start). I will try your suggestion to at least be able to parse the timestamps. Regarding your point with the chrono features - I did activate this.
    Many thanks again. Will play around with it later.
    realuncleshelby
    @realuncleshelby:matrix.org
    [m]
    Is there any way to show debug statements with the actual SQL run by rusqlite?
    Thom Chiovoloni
    @thomcc
    hm, you could use the FFI to set a SQLLOG callback https://sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigsqllog, but we dont have a wrapper for it
    there might be a better way too, not sure
    rusqlite/rusqlite#946 woudl more-or-less give you a way, although an awkward one. i probably should just apply the changes i'd like and merge it for them
    realuncleshelby
    @realuncleshelby:matrix.org
    [m]
    Cool, that gives me a place to start looking at least
    Thanks for the help
    Alexander
    @FoxLisk
    is it safe to pass around Arc<>s to a Connection in a tokio project?
    or do i need to do some kind of locking on my own or whatnot