Skip to content

Commit

Permalink
Merge pull request #278 from HicServices/feature/db-enumeration
Browse files Browse the repository at this point in the history
Feature/db enumeration
  • Loading branch information
JFriel committed May 21, 2024
2 parents c1184fb + 2eb402a commit d28e2ca
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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]

- Fix bug in PostgreSQL boolean handling (use booleans, not BIT)
- Fix bug in PostgreSQL where database listing failed if a database named 'postgres' was not present
- Make database enumeration an Enumerable not an array

## [3.2.1] - 2024-03-11

- Add Setter for DiscoveredDatabaseHelper Create Database Timeout
Expand Down
7 changes: 4 additions & 3 deletions FAnsiSql/Discovery/DiscoveredServerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using FAnsi.Connections;
Expand Down Expand Up @@ -105,9 +106,9 @@ public virtual DbConnectionStringBuilder ChangeDatabase(DbConnectionStringBuilde
}

public abstract IEnumerable<string> ListDatabases(DbConnectionStringBuilder builder);
public abstract string[] ListDatabases(DbConnection con);
public abstract IEnumerable<string> ListDatabases(DbConnection con);

public string[] ListDatabasesAsync(DbConnectionStringBuilder builder, CancellationToken token)
public IAsyncEnumerable<string> ListDatabasesAsync(DbConnectionStringBuilder builder, CancellationToken token)
{
//list the database on the server
using var con = GetConnection(builder);
Expand All @@ -116,7 +117,7 @@ public string[] ListDatabasesAsync(DbConnectionStringBuilder builder, Cancellati
var openTask = con.OpenAsync(token);
openTask.Wait(token);

return ListDatabases(con);
return ListDatabases(con).ToAsyncEnumerable();
}

public abstract DbConnectionStringBuilder EnableAsync(DbConnectionStringBuilder builder);
Expand Down
3 changes: 2 additions & 1 deletion FAnsiSql/Discovery/IDiscoveredServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public interface IDiscoveredServerHelper
DbConnectionStringBuilder EnableAsync(DbConnectionStringBuilder builder);

IEnumerable<string> ListDatabases(DbConnectionStringBuilder builder);
string[] ListDatabasesAsync(DbConnectionStringBuilder builder, CancellationToken token);
IEnumerable<string> ListDatabases(DbConnection con);
IAsyncEnumerable<string> ListDatabasesAsync(DbConnectionStringBuilder builder, CancellationToken token);

IDiscoveredDatabaseHelper GetDatabaseHelper();
IQuerySyntaxHelper GetQuerySyntaxHelper();
Expand Down
3 changes: 2 additions & 1 deletion FAnsiSql/FAnsi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="23.4.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<Compile Update="FAnsiStrings.Designer.cs">
Expand Down
17 changes: 6 additions & 11 deletions FAnsiSql/Implementations/MicrosoftSQL/MicrosoftSQLServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,15 @@ public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder buil

using var con = new SqlConnection(b.ConnectionString);
con.Open();
return ListDatabases(con);
foreach (var listDatabase in ListDatabases(con)) yield return listDatabase;
}

public override string[] ListDatabases(DbConnection con)
public override IEnumerable<string> ListDatabases(DbConnection con)
{
var databases = new List<string>();

using (var cmd = GetCommand("select name [Database] from master..sysdatabases", con))
using (var r = cmd.ExecuteReader())
while (r.Read())
databases.Add((string)r["Database"]);

con.Close();
return [.. databases];
using var cmd = GetCommand("select name [Database] from master..sysdatabases", con);
using var r = cmd.ExecuteReader();
while (r.Read())
yield return (string)r["Database"];
}

public override DbConnectionStringBuilder EnableAsync(DbConnectionStringBuilder builder)
Expand Down
10 changes: 4 additions & 6 deletions FAnsiSql/Implementations/MySql/MySqlServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder buil

using var con = new MySqlConnection(b.ConnectionString);
con.Open();
return ListDatabases(con);
foreach (var listDatabase in ListDatabases(con)) yield return listDatabase;
}
public override string[] ListDatabases(DbConnection con)
{
var databases = new List<string>();

public override IEnumerable<string> ListDatabases(DbConnection con)
{
using var cmd = GetCommand("show databases;", con);
using var r = cmd.ExecuteReader();
while (r.Read())
databases.Add((string)r["Database"]);
return [.. databases];
yield return (string)r["Database"];
}
}
18 changes: 7 additions & 11 deletions FAnsiSql/Implementations/Oracle/OracleServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override void CreateDatabase(DbConnectionStringBuilder builder, IHasRunti
public override Version GetVersion(DiscoveredServer server)
{
using var tcon = server.GetConnection();
if (tcon is not OracleConnection con) throw new ArgumentException("Oracle helped called on non-Oracle server",nameof(server));
if (tcon is not OracleConnection con) throw new ArgumentException("Oracle helper called on non-Oracle server",nameof(server));

con.UseHourOffsetForUnsupportedTimezone = true;
con.Open();
Expand All @@ -120,18 +120,14 @@ public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder buil
using var con = new OracleConnection(builder.ConnectionString);
con.UseHourOffsetForUnsupportedTimezone = true;
con.Open();
return ListDatabases(con);
foreach (var listDatabase in ListDatabases(con)) yield return listDatabase;
}

public override string[] ListDatabases(DbConnection con)
public override IEnumerable<string> ListDatabases(DbConnection con)
{
var databases = new List<string>();

using(var cmd = GetCommand("select * from all_users", con)) //already comes as single column called Database
using (var r = cmd.ExecuteReader())
while (r.Read())
databases.Add((string) r["username"]);

return [.. databases];
using var cmd = GetCommand("select * from all_users", con);
using var r = cmd.ExecuteReader();
while (r.Read())
yield return (string)r["username"];
}
}
19 changes: 7 additions & 12 deletions FAnsiSql/Implementations/PostgreSql/PostgreSqlServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,21 @@ public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder buil
//create a copy so as not to corrupt the original
var b = new NpgsqlConnectionStringBuilder(builder.ConnectionString)
{
Database = "postgres",
Database = null,
Timeout = 5
};

using var con = new NpgsqlConnection(b.ConnectionString);
con.Open();
return ListDatabases(con);
foreach (var listDatabase in ListDatabases(con)) yield return listDatabase;
}

public override string[] ListDatabases(DbConnection con)
public override IEnumerable<string> ListDatabases(DbConnection con)
{
var databases = new List<string>();

using(var cmd = GetCommand("SELECT datname FROM pg_database;", con))
using(var r = cmd.ExecuteReader())
while (r.Read())
databases.Add((string) r["datname"]);

con.Close();
return [.. databases];
using var cmd = GetCommand("SELECT datname FROM pg_database;", con);
using var r = cmd.ExecuteReader();
while (r.Read())
yield return (string)r["datname"];
}

public override DbCommand GetCommand(string s, DbConnection con, DbTransaction? transaction = null) => new NpgsqlCommand(s, (NpgsqlConnection)
Expand Down
1 change: 1 addition & 0 deletions Packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
| Oracle.ManagedDataAccess.Core | Closed Source | [OTNLA](https://www.oracle.com/downloads/licenses/distribution-license.html) | Enables interaction with Oracle databases |
| HIC.TypeGuesser | [GitHub](https://github.com/HicServices/TypeGuesser) | [MIT](https://opensource.org/licenses/MIT)| Allows picking system Types for untyped strings e.g. `"12.3"`| |
| Npgsql | [GitHub](https://github.com/npgsql/npgsql) | [PostgreSQL](https://github.com/npgsql/npgsql/blob/dev/LICENSE)| Enables interaction with Postgres databases | |
| System.Linq.Async | [GitHub](https://github.com/dotnet/reactive) | [MIT](https://opensource.org/licenses/MIT) | Adds async support to LINQ | |

0 comments on commit d28e2ca

Please sign in to comment.