Skip to content

Sqlite Support (presently not possible) #7

@justinturpin

Description

@justinturpin

Currently the crate doesn't work with sqlite, or if it does the interface is different for it:

use sqlx::Connection;
use tide::prelude::*;
use tide::Request;
use tera::Tera;
use tide_tera::prelude::*;
use sqlx::Acquire;
use sqlx::sqlite::Sqlite;
use sqlx::postgres::Postgres;

use tide_sqlx::SQLxMiddleware;
use tide_sqlx::SQLxRequestExt;

#[async_std::main]
async fn main() -> tide::Result<()> {
    let mut tera = Tera::new("templates/**/*.html")?;
    tera.autoescape_on(vec!["html"]);

    let mut app = tide::with_state(tera);

    app.with(SQLxMiddleware::<Sqlite>::new("sqlite3://testdatabase.sqlite3").await?);

    app.at("/").get(|req: Request<Tera>| async move {
        let tera = req.state();
        tera.render_response("index.html", &tera::Context::new())
    });

    app.listen("127.0.0.1:8080").await?;
    Ok(())
}

Fails to compile with:


error[E0599]: no function or associated item named `new` found for struct `SQLxMiddleware<Sqlite>` in the current scope
  --> src\main.rs:51:40
   |
51 |     app.with(SQLxMiddleware::<Sqlite>::new("sqlite3://testdatabase.sqlite3").await?);
   |                                        ^^^ function or associated item not found in `SQLxMiddleware<Sqlite>`
   | 
  ::: C:\Users\Justin\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-core-0.4.0\src\sqlite\connection\mod.rs:23:1
   |
23 | pub struct SqliteConnection {
   | --------------------------- doesn't satisfy `SqliteConnection: Sync`
   |
   = note: the method `new` exists but the following trait bounds were not satisfied:
           `SqliteConnection: Sync`

error[E0277]: `NonNull<libsqlite3_sys::bindings::sqlite3>` cannot be shared between threads safely
   --> src\main.rs:51:14
    |
51  |     app.with(SQLxMiddleware::<Sqlite>::new("sqlite3://testdatabase.sqlite3").await?);
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NonNull<libsqlite3_sys::bindings::sqlite3>` cannot be shared between threads safely
    | 
   ::: C:\Users\Justin\.cargo\registry\src\github.com-1ecc6299db9ec823\tide-sqlx-0.3.1\src\lib.rs:196:28
    |
196 |     DB::Connection: Send + Sync + 'static,
    |                            ---- required by this bound in `SQLxMiddleware`
    |
    = help: within `SqliteConnection`, the trait `Sync` is not implemented for `NonNull<libsqlite3_sys::bindings::sqlite3>`
    = note: required because it appears within the type `sqlite::connection::handle::ConnectionHandle`
    = note: required because it appears within the type `SqliteConnection`

I'm guessing this is do to weirdness around Sqlite itself not being thread safe, and Sqlx does some thread-wrapping trickery. I'd be happy to take a look and try and fix this if someone had some ideas about where to go to do this. I'm playing around with a small Tide web app right now to try and figure out the best approach to having an app that uses Sqlite for many readers but only one writer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions