Skip to content

Commit

Permalink
feat: impl DbDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Aug 7, 2024
1 parent c9e4aa6 commit 18ff149
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Ydb.Sdk/src/Ado/YdbDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#if NET7_0_OR_GREATER

using System.Data.Common;

namespace Ydb.Sdk.Ado;

public class YdbDataSource : DbDataSource
{
private readonly YdbConnectionStringBuilder _ydbConnectionStringBuilder;

public YdbDataSource(YdbConnectionStringBuilder connectionStringBuilder)
{
_ydbConnectionStringBuilder = connectionStringBuilder;
}

public YdbDataSource(string connectionString)
{
_ydbConnectionStringBuilder = new YdbConnectionStringBuilder(connectionString);
}

public YdbDataSource()
{
_ydbConnectionStringBuilder = new YdbConnectionStringBuilder();
}

protected override YdbConnection CreateDbConnection()
{
return new YdbConnection(_ydbConnectionStringBuilder);
}

protected override YdbConnection OpenDbConnection()
{
var dbConnection = CreateDbConnection();
try
{
dbConnection.Open();
return dbConnection;
}
catch
{
dbConnection.Close();
throw;
}
}

public new YdbConnection CreateConnection() => CreateDbConnection();

public new YdbConnection OpenConnection() => OpenDbConnection();

public new async ValueTask<YdbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default)
{
var ydbConnection = CreateDbConnection();

try
{
await ydbConnection.OpenAsync(cancellationToken);
return ydbConnection;
}
catch
{
await ydbConnection.CloseAsync();
throw;
}
}

public override string ConnectionString => _ydbConnectionStringBuilder.ConnectionString;
}

#endif
55 changes: 55 additions & 0 deletions src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Xunit;

Check warning on line 1 in src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs
using Ydb.Sdk.Ado;

Check warning on line 2 in src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs(2,13)

#if NET7_0_OR_GREATER
namespace Ydb.Sdk.Tests.Ado;

[Trait("Category", "Integration")]
public class YdbDataSourceTests
{
private const int SelectedCount = 100;

private readonly YdbDataSource _dataSource = new("MaxSessionPool=10");

[Fact]
public async Task OpenConnectionAsync_WhenMaxSessionPool10_ReturnOpenConnection()
{
var tasks = new Task[SelectedCount];
for (var i = 0; i < SelectedCount; i++)
{
tasks[i] = Task.Run(async () =>
{
await using var ydbConnection = await _dataSource.OpenConnectionAsync();
var ydbCommand = new YdbCommand(ydbConnection) { CommandText = "SELECT 1" };
Assert.Equal(1, await ydbCommand.ExecuteScalarAsync());
});
}

await Task.WhenAll(tasks);
}

[Fact]
public void CreateCommand_FromDataSource_ReturnDbCommand()
{
for (var i = 0; i < SelectedCount; i++)
{
Assert.Equal(1, _dataSource.CreateCommand("SELECT 1;").ExecuteScalar());
}
}

[Fact]
public void CreateConnection_FromDataSource_ReturnNotOpenConnection()
{
using var ydbConnection = _dataSource.CreateConnection();
ydbConnection.Open();
Assert.Equal(1, new YdbCommand(ydbConnection) { CommandText = "SELECT 1" }.ExecuteScalar());
}

[Fact]
public void OpenConnection_FromDataSource_ReturnOpenConnection()
{
using var ydbConnection = _dataSource.OpenConnection();
Assert.Equal(1, new YdbCommand(ydbConnection) { CommandText = "SELECT 1" }.ExecuteScalar());
}
}
#endif

0 comments on commit 18ff149

Please sign in to comment.