Skip to content

Commit

Permalink
remove psql null db
Browse files Browse the repository at this point in the history
  • Loading branch information
JFriel committed Jul 15, 2024
1 parent 11cfcd0 commit 5658b50
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Listing databases on Postgres now respects connection string database and timeout

## [3.2.5] - 2024-06-07

- Bugfix for resource lifetime in ListDatabasesAsync, add unit tests
Expand Down
18 changes: 9 additions & 9 deletions FAnsiSql/Implementations/PostgreSql/PostgreSqlServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ public override void CreateDatabase(DbConnectionStringBuilder builder, IHasRunti

using var con = new NpgsqlConnection(b.ConnectionString);
con.Open();
using var cmd = GetCommand($"CREATE DATABASE \"{newDatabaseName.GetRuntimeName()}\"",con);
using var cmd = GetCommand($"CREATE DATABASE \"{newDatabaseName.GetRuntimeName()}\"", con);
cmd.CommandTimeout = CreateDatabaseTimeoutInSeconds;
cmd.ExecuteNonQuery();
}

public override Dictionary<string, string> DescribeServer(DbConnectionStringBuilder builder) => throw new NotImplementedException();

public override string? GetExplicitUsernameIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder) builder).Username;
public override string? GetExplicitUsernameIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder)builder).Username;

public override string? GetExplicitPasswordIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder) builder).Password;
public override string? GetExplicitPasswordIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder)builder).Password;

public override Version? GetVersion(DiscoveredServer server)
{
using var con = server.GetConnection();
con.Open();
using var cmd = server.GetCommand("SHOW server_version",con);
using var cmd = server.GetCommand("SHOW server_version", con);
using var r = cmd.ExecuteReader();
if(r.Read())
return r[0] == DBNull.Value ? null: CreateVersionFromString((string)r[0]);
if (r.Read())
return r[0] == DBNull.Value ? null : CreateVersionFromString((string)r[0]);

return null;
}
Expand All @@ -64,10 +64,10 @@ public override void CreateDatabase(DbConnectionStringBuilder builder, IHasRunti
public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder builder)
{
//create a copy so as not to corrupt the original

var b = new NpgsqlConnectionStringBuilder(builder.ConnectionString)
{
Database = null,
Timeout = 5
Timeout = builder.TryGetValue("Timeout", out var timeout) ? (int)timeout : 5
};

using var con = new NpgsqlConnection(b.ConnectionString);
Expand All @@ -88,7 +88,7 @@ public override IEnumerable<string> ListDatabases(DbConnection con)

public override DbDataAdapter GetDataAdapter(DbCommand cmd) => new NpgsqlDataAdapter((NpgsqlCommand)cmd);

public override DbCommandBuilder GetCommandBuilder(DbCommand cmd) => new NpgsqlCommandBuilder(new NpgsqlDataAdapter((NpgsqlCommand) cmd));
public override DbCommandBuilder GetCommandBuilder(DbCommand cmd) => new NpgsqlCommandBuilder(new NpgsqlDataAdapter((NpgsqlCommand)cmd));

public override DbParameter GetParameter(string parameterName) => new NpgsqlParameter { ParameterName = parameterName };

Expand Down

0 comments on commit 5658b50

Please sign in to comment.