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

Introduce ConnectOptions to build a connection string from a builder #174

Closed
mehcode opened this issue Mar 29, 2020 · 4 comments
Closed
Labels
E-medium enhancement New feature or request

Comments

@mehcode
Copy link
Member

mehcode commented Mar 29, 2020

As of 0.3

use sqlx::Connect;

let mut conn = PgConnection::connect("postgres://user:pass@localhost/database?sslmode=require");

Proposal

let options = ConnectOptions::new()
    .host("localhost")
    .port(5432)
    .username("user")
    .password("pass")
    .param("sslmode", "require");

let mut conn = PgConnection::connect(options).await?;
let mut conn = options.connect().await?;
let pool = PgPool::new(options).await?;
let pool = PgPool::builder().build(options).await?;
  • Introduce ConnectOptions
  • Implement TryFrom<&str> for ConnectOptions to parse URI and DSN connection strings
  • Pool and Connection construction now take O: TryInto<ConnectOptions>
  • The url crate should not be used for parsing; in particular, a schema should not be a required piece

Additional ideas:

  • Introduce database-specific option extension traits to for parameters. E.g.. PgConnectOptionsExt would provide a ssl_mode method to set the sslmode parameter.
@mehcode
Copy link
Member Author

mehcode commented Apr 18, 2020

Here are some notes.

https://gist.github.com/rust-play/8434ec3de09f776f4376972824c6190e

cc @g-s-k

@g-s-k
Copy link
Contributor

g-s-k commented Apr 19, 2020

One thought, though it may be off-base, is that associated types on a driver's Database impl are used pretty heavily here. Might that be a good pattern to use (rather than extension traits) for consistency?

So, for example...
https://gist.github.com/rust-play/1f9387bd0176da6e511c89dfd2afa2fb

@mehcode
Copy link
Member Author

mehcode commented Apr 19, 2020

No, you're totally right. That is better. This would also keep it even more separate and allow for say SQLite to just use a PathBuf as storage for its filename. I didn't like how SqliteConnectOptions turned out in my gist.


I'm currently working on some refactoring efforts for postgres so I'll roll this into it. I wouldn't mind help on SQLite after I push up what I have.

@mehcode
Copy link
Member Author

mehcode commented May 31, 2020

This is now been refactored to separate option types. We can accept additional database specific option knobs quite easily now.

@mehcode mehcode closed this as completed May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-medium enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants