Skip to content

Support URI-like connection strings (postgres://) - #2512

Closed
jgilchrist wants to merge 1 commit into
npgsql:devfrom
jgilchrist:detect-and-parse-uri-type-connection-strings
Closed

jgilchrist wants to merge 1 commit into
npgsql:devfrom
jgilchrist:detect-and-parse-uri-type-connection-strings

Conversation

@jgilchrist

Copy link
Copy Markdown

This is a first attempt at a solution for #2090. It may be way off base and it's not yet complete (it doesn't handle query parameters yet for example) but I wanted to file this to get some initial feedback on the direction so I can course correct if this doesn't look like the right way to go.

A couple of thoughts

  • The last part of this comment Accept connecting with PostgreSQL URLs instead of connection strings #2090 (comment) isn't addressed as I'm still getting my head round the various different connection strings in the code and when/how they are set/changed (NpgsqlConnection.ConnectionString, NpgsqlConnection._connectionString, NpgsqlConnection._userFacingConnectionString, NpgsqlConnectionStringBuilder.ConnectionString). As far as I can tell, line 267 would need to know whether the initial connection string was passed as a URI and would need to call a different ToString... method?
  • Query string parameter parsing is in System.Web in .NET Framework, but is in Microsoft.AspNetCore.WebUtilities for .NET Core - is this a dependency that would be OK to add?
  • I haven't added tests yet, just in case this approach ends up being a non-starter - if this goes ahead I'll have a look at tests too

@roji

roji commented Jul 7, 2019

Copy link
Copy Markdown
Member

The approach looks good to me - as far as I'm concerned you can move forward with a more complete PR.

Query string parameter parsing is in System.Web in .NET Framework, but is in Microsoft.AspNetCore.WebUtilities for .NET Core - is this a dependency that would be OK to add?

I don't think it makes sense to add this dependency only for parsing query string parameters. Parsing these manually shouldn't extremely hard...

As far as I can tell, line 267 would need to know whether the initial connection string was passed as a URI and would need to call a different ToString... method?

Ideally yes...

One more thought: PostgreSQL has a set of recognized keywords, which don't necessarily correspond to what we currently support via NpgsqlConnectionStringBuilder. If the goal here is to allow using PG URIs as-is, then we should probably think about recognizing these as well, and mapping them to our connection string parameters. This could be done by annotating properties on NpgsqlConnectionStringBuilder with new attributes for their URI keyword, and use reflection to extract them.

@austindrenski austindrenski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited to see this moving forward. Just some initial thoughts.


NpgsqlConnectionStringBuilder ParseConnectionString(string connectionString)
{
return Uri.TryCreate(connectionString, UriKind.Absolute, out var uri)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also check that the scheme is postgres:// or postgresql://.

{
var builder = new NpgsqlConnectionStringBuilder();

var userInfo = uri.UserInfo?.Split(':');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll also need to unescape some values here.

@jgilchrist

Copy link
Copy Markdown
Author

A very belated thanks for the reviews here everyone - unfortunately I didn't get a lot of time to follow up on this but it seems a more fleshed-out PR has been filed here: #2733 so I'm going to close this out

@jgilchrist jgilchrist closed this May 31, 2020
@jgilchrist
jgilchrist deleted the detect-and-parse-uri-type-connection-strings branch May 31, 2020 16:03
@roji

roji commented May 31, 2020

Copy link
Copy Markdown
Member

Thanks @jgilchrist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants