Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to a type based implementation #1

Merged
merged 3 commits into from
Jun 21, 2024

Conversation

Snapstromegon
Copy link
Owner

This switches the stringly based implementation to a type based implementation.

Signed-off-by: Raphael Höser <raphael@hoeser.info>
@Snapstromegon Snapstromegon added the help wanted Extra attention is needed label Jun 13, 2024
@Snapstromegon
Copy link
Owner Author

Right now this PR has the following issue:

When having a Postgres and SQLite Connection enabled via env vars and running this code:

use sqlx::{query, SqlitePool, Sqlite};
let sqlite = SqlitePool::connect("sqlite::memory:").await.unwrap();

query!(
    Sqlite,
    "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"
)
.execute(&sqlite)
.await
.unwrap();

This is valid for Postgres and SQLite, so it finds two working_drivers. Because of this, it will generate this code:

{
    match std::any::type_name::<Sqlite>() {
        "sqlx_postgres::database::Postgres" => {
            use ::sqlx::Arguments as _;
            let query_args = ::core::result::Result::<
                _,
                ::sqlx::error::BoxDynError,
            >::Ok(
                <sqlx::postgres::Postgres as ::sqlx::database::Database>::Arguments::<
                    '_,
                >::default(),
            );
            ::sqlx::__query_with_result::<
                sqlx::postgres::Postgres,
                _,
            >(
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                query_args,
            )
        }
        "sqlx_sqlite::database::Sqlite" => {
            use ::sqlx::Arguments as _;
            let query_args = ::core::result::Result::<
                _,
                ::sqlx::error::BoxDynError,
            >::Ok(
                <sqlx::sqlite::Sqlite as ::sqlx::database::Database>::Arguments::<
                    '_,
                >::default(),
            );
            ::sqlx::__query_with_result::<
                sqlx::sqlite::Sqlite,
                _,
            >(
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                query_args,
            )
        }
    }
}
    .execute(&sqlite)
    .await
    .unwrap();

This doesn't work because the match arms have different types and you can also not store the result in a Box<dyn Database>, because Database: Sized.

If anyone has a good idea how to resolve this, I'm very open to it. Aside from this (and maybe some performance optimizations) this should be a working solution.

I also tried working with the type_ids, but that doesn't work because the types are not identical for the driver passed in and the traitbounds on the real drivers.

One last note: Right now this enables the unstable #![feature(const_type_name)]. If someone finds a workaround for this, it would be great.

…ferenced

Signed-off-by: Raphael Höser <raphael@hoeser.info>
Signed-off-by: Raphael Höser <raphael@hoeser.info>
@Snapstromegon Snapstromegon merged commit f21274b into 121-multiple-dbs Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant