Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ jobs:
- name: dotnet format style and analyzers (any finding fails the run)
run: |
$projects = @(
'Source\Core', 'Source\SqlClient', 'Source\MSSqlClient', 'Source\Channel', 'Source\Dataflow', 'Source\Extensions',
'Source\Core', 'Source\MSSqlClient', 'Source\Channel', 'Source\Dataflow', 'Source\Extensions',
'Tests\Open.Database.Extensions.Core.Tests', 'Tests\Open.Database.Extensions.LegacyCompiler.Tests', 'docsite\samples', 'Benchmarks\Open.Database.Extensions.Benchmarks',
'Source\Generator', 'Tests\Open.Database.Extensions.Generator.Models', 'Tests\Open.Database.Extensions.Generator.Tests', 'Benchmarks\Open.Database.Extensions.Benchmarks.Generated')
$failures = @()
Expand Down
14 changes: 0 additions & 14 deletions Open.Database.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Database.Extensions.Co
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Database.Extensions.Dataflow", "Source\Dataflow\Open.Database.Extensions.Dataflow.csproj", "{862710B0-C5B4-458D-803E-AD79F70DDEF1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Database.Extensions.SqlClient", "Source\SqlClient\Open.Database.Extensions.SqlClient.csproj", "{F2402B6B-652B-479F-8F7E-885ED175D584}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Database.Extensions.Channel", "Source\Channel\Open.Database.Extensions.Channel.csproj", "{EE37225F-0B2B-4225-B07D-2BCE007B780C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Database.Extensions.Core.Tests", "Tests\Open.Database.Extensions.Core.Tests\Open.Database.Extensions.Core.Tests.csproj", "{B2C2E89F-3B08-4CF8-B363-468C0918F867}"
Expand Down Expand Up @@ -68,18 +66,6 @@ Global
{862710B0-C5B4-458D-803E-AD79F70DDEF1}.Release|x64.Build.0 = Release|Any CPU
{862710B0-C5B4-458D-803E-AD79F70DDEF1}.Release|x86.ActiveCfg = Release|Any CPU
{862710B0-C5B4-458D-803E-AD79F70DDEF1}.Release|x86.Build.0 = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|x64.ActiveCfg = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|x64.Build.0 = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|x86.ActiveCfg = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Debug|x86.Build.0 = Debug|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|Any CPU.Build.0 = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|x64.ActiveCfg = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|x64.Build.0 = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|x86.ActiveCfg = Release|Any CPU
{F2402B6B-652B-479F-8F7E-885ED175D584}.Release|x86.Build.0 = Release|Any CPU
{EE37225F-0B2B-4225-B07D-2BCE007B780C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE37225F-0B2B-4225-B07D-2BCE007B780C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE37225F-0B2B-4225-B07D-2BCE007B780C}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down
1 change: 0 additions & 1 deletion Source/Core/Open.Database.Extensions.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<ItemGroup>
<InternalsVisibleTo Include="Open.Database.Extensions.Channel" />
<InternalsVisibleTo Include="Open.Database.Extensions.Dataflow" />
<InternalsVisibleTo Include="Open.Database.Extensions.SqlClient" />
<InternalsVisibleTo Include="Open.Database.Extensions.MSSqlClient" />
<InternalsVisibleTo Include="Open.Database.Extensions.Core.Tests" />
<InternalsVisibleTo Include="Open.Database.Extensions.Generator.Tests" />
Expand Down
34 changes: 3 additions & 31 deletions Source/SqlClient/README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
# Open.Database.Extensions.SqlClient

[![Open.Database.Extensions.SqlClient: You write the SQL. ODX writes the other twenty lines.](https://raw.githubusercontent.com/Open-NET-Libraries/Open.Database.Extensions/master/docsite/images/hero-sqlclient.svg)](https://open-net-libraries.github.io/Open.Database.Extensions/)
This package is not built and not published for version 11. Its last release is 10.3.0, which works with Core 10.x only.

**Not published for version 11.** Use [Open.Database.Extensions.MSSqlClient](https://www.nuget.org/packages/Open.Database.Extensions.MSSqlClient/), which uses `Microsoft.Data.SqlClient` and has the same API. It includes [Open.Database.Extensions.Core](https://www.nuget.org/packages/Open.Database.Extensions.Core/).
For SQL Server, use [Open.Database.Extensions.MSSqlClient](https://www.nuget.org/packages/Open.Database.Extensions.MSSqlClient/). It is built on `Microsoft.Data.SqlClient`, has the same API, and includes [Open.Database.Extensions.Core](https://www.nuget.org/packages/Open.Database.Extensions.Core/).

```cs
using Open.Database.Extensions;

public record Post(int Id, string Title);

// Create the factory once per application.
// connectionString is yours. System.Data.SqlClient comes with this package.
var connectionFactory = new SqlConnectionFactory(connectionString);
int author = 42;

Post[] posts = await connectionFactory
.Command("select * from posts where author = @author")
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToArrayAsync();
```

It is used the same way as MSSqlClient: `Command(...)`, `StoredProcedure(...)`, `AddParam(...)` and `Map<T>()`. The [MSSqlClient README](https://www.nuget.org/packages/Open.Database.Extensions.MSSqlClient/) has the stored-procedure examples, including parameter names taken from the argument.

## The packages

| Package | What it adds | Targets |
|---|---|---|
| [Open.Database.Extensions.Core](https://www.nuget.org/packages/Open.Database.Extensions.Core/) | The foundation, expressive commands and `Map<T>()`. Provider-agnostic: start here. | netstandard2.0, netstandard2.1, net10.0, net11.0 |
| [Open.Database.Extensions.MSSqlClient](https://www.nuget.org/packages/Open.Database.Extensions.MSSqlClient/) | `SqlConnectionFactory` and stored procedures on `Microsoft.Data.SqlClient`. | net472, net10.0, net11.0 |
| [Open.Database.Extensions.Channel](https://www.nuget.org/packages/Open.Database.Extensions.Channel/) | Rows into `System.Threading.Channels`. | netstandard2.0, netstandard2.1, net10.0, net11.0 |
| [Open.Database.Extensions.Dataflow](https://www.nuget.org/packages/Open.Database.Extensions.Dataflow/) | Rows into TPL Dataflow blocks. | netstandard2.0, netstandard2.1, net10.0, net11.0 |

[Documentation](https://open-net-libraries.github.io/Open.Database.Extensions/) · [API reference](https://open-net-libraries.github.io/Open.Database.Extensions/api/index.html) · [Source and issues](https://github.com/Open-NET-Libraries/Open.Database.Extensions) · MIT license
The code of this folder stays in the repository for reference. It is not part of the solution, and nothing in it is maintained.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ static ExpressiveCommand Classic()
static ExpressiveDbCommand Db()
=> new ConnectionProbe(ConnectionState.Open, dbConnection: true).Db!.Command("select 1");

#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
static ExpressiveSqlCommand Legacy()
=> new(new System.Data.SqlClient.SqlConnection(), CommandType.Text, "select 1");
#pragma warning restore CS0618

static mssql::Open.Database.Extensions.ExpressiveSqlCommand Microsoft()
=> new(new Microsoft.Data.SqlClient.SqlConnection(), CommandType.Text, "select 1");

Expand All @@ -31,18 +26,15 @@ public static void FalseCondition_AddsNothing_AndNeverLooksAtTheArgument()
{
var classic = Classic();
var db = Db();
var legacy = Legacy();
var microsoft = Microsoft();

// The literal 1 has no derivable name: a true condition would throw (see the next test).
Assert.Same(classic, classic.AddParamIf(false, 1));
Assert.Same(db, db.AddParamIf(false, 1));
Assert.Same(legacy, legacy.AddParamIf(false, 1));
Assert.Same(microsoft, microsoft.AddParamIf(false, 1));

AssertNone(classic.Params);
AssertNone(db.Params);
AssertNone(legacy.Params);
AssertNone(microsoft.Params);
}

Expand All @@ -51,18 +43,15 @@ public static void FalseCondition_WithAnArgumentTheCompilerDidNotSupply_AddsNoth
{
var classic = Classic();
var db = Db();
var legacy = Legacy();
var microsoft = Microsoft();

// A compiler before C# 10 passes no text; with a false condition that is no error either.
classic.AddParamIf(false, value: 1, expression: null);
db.AddParamIf(false, value: 1, expression: null);
legacy.AddParamIf(false, value: 1, expression: null);
microsoft.AddParamIf(false, value: 1, expression: null);

AssertNone(classic.Params);
AssertNone(db.Params);
AssertNone(legacy.Params);
AssertNone(microsoft.Params);
}

Expand All @@ -71,7 +60,6 @@ public static void TrueCondition_WithAnArgumentThatHasNoName_Throws_AndAddsNothi
{
var classic = Classic();
var db = Db();
var legacy = Legacy();
var microsoft = Microsoft();

static void Throws(Action add)
Expand All @@ -83,13 +71,11 @@ static void Throws(Action add)

Throws(() => classic.AddParamIf(true, 1));
Throws(() => db.AddParamIf(true, 1));
Throws(() => legacy.AddParamIf(true, 1));
Throws(() => microsoft.AddParamIf(true, 1));
Throws(() => classic.AddParamIf(true, value: 1, expression: null));

AssertNone(classic.Params);
AssertNone(db.Params);
AssertNone(legacy.Params);
AssertNone(microsoft.Params);
}

Expand All @@ -99,21 +85,17 @@ public static void TrueCondition_WithADerivableArgument_AddsTheParameter_UnderTh
const int author = 42;
var classic = Classic();
var db = Db();
var legacy = Legacy();
var microsoft = Microsoft();

classic.AddParamIf(true, author);
db.AddParamIf(true, author);
legacy.AddParamIf(true, author);
microsoft.AddParamIf(true, author);

Assert.Equal("author", Assert.Single(classic.Params).Name);
Assert.Equal("author", Assert.Single(db.Params).Name);
Assert.Equal("author", Assert.Single(legacy.Params).Name);
Assert.Equal("author", Assert.Single(microsoft.Params).Name);
Assert.Equal(42, classic.Params[0].Value);
Assert.Equal(42, db.Params[0].Value);
Assert.Equal(42, legacy.Params[0].Value);
Assert.Equal(42, microsoft.Params[0].Value);
}

Expand All @@ -123,17 +105,14 @@ public static void FalseCondition_WithADerivableArgument_AddsNothing()
const int author = 42;
var classic = Classic();
var db = Db();
var legacy = Legacy();
var microsoft = Microsoft();

classic.AddParamIf(false, author);
db.AddParamIf(false, author);
legacy.AddParamIf(false, author);
microsoft.AddParamIf(false, author);

AssertNone(classic.Params);
AssertNone(db.Params);
AssertNone(legacy.Params);
AssertNone(microsoft.Params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Open.Database.Extensions.Tests;

// The library's own SQL Server commands are decided without a lookup, as the
// base commands are: the exact type test is in each assembly.
// The library's own SQL Server command is decided without a lookup, as the
// base commands are: the exact type test is in its assembly.
[ExcludeFromCodeCoverage]
public static class CaptureLibraryCommandTests
{
Expand All @@ -14,13 +14,8 @@ public sealed record Row(int Id);
// A query capture checks the command's type at the call, so nothing is
// opened or run.
[Fact]
public static void TheSqlCommands_NeedNoLookup_ForACapture()
public static void TheSqlCommand_NeedsNoLookup_ForACapture()
{
#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
using var legacy = new System.Data.SqlClient.SqlConnection();
_ = new ExpressiveSqlCommand(legacy, CommandType.StoredProcedure, "p")
.Map<Row>()
.CaptureOutput(out DbCapture<int> _, "total");
using var connection = new Microsoft.Data.SqlClient.SqlConnection();
_ = new mssql::Open.Database.Extensions.ExpressiveSqlCommand(
connection,
Expand All @@ -29,10 +24,6 @@ public static void TheSqlCommands_NeedNoLookup_ForACapture()
.Map<Row>()
.CaptureOutput(out DbCapture<int> _, "total");

Assert.Equal(
0,
ReaderReplacement.LookupCount(typeof(ExpressiveSqlCommand)));
#pragma warning restore CS0618
Assert.Equal(
0,
ReaderReplacement.LookupCount(
Expand Down
58 changes: 0 additions & 58 deletions Tests/Open.Database.Extensions.Core.Tests/NameCacheSourceTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern alias mssql;
using Microsoft.Data.SqlClient;
using System.Data.Common;
using LegacyCommand = global::Open.Database.Extensions.ExpressiveSqlCommand;
using MsCommand = mssql::Open.Database.Extensions.ExpressiveSqlCommand;
using MsExtensions = mssql::Open.Database.Extensions.SqlConnectionExtensions;

Expand Down Expand Up @@ -130,12 +129,6 @@ static async Task RunAsync(MsCommand command)
=> await command.AddParam(value: 1, expression: "filter.Region")
.ExecuteNonQueryAsync(Token);

#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
static async Task RunAsync(LegacyCommand command)
=> await command.AddParam(value: 1, expression: "filter.Region")
.ExecuteNonQueryAsync(Token);
#pragma warning restore CS0618

static string Procedure()
=> ParameterNameHarness.NewProcedure();

Expand Down Expand Up @@ -315,25 +308,6 @@ static Func<SqlConnection> CountingMs(Trips trips)
return connection;
};

#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
static Func<System.Data.SqlClient.SqlConnection> CountingLegacy(Trips trips)
=> () =>
{
var connection = new System.Data.SqlClient.SqlConnection(LocalDbFixture.ConnectionString)
{
StatisticsEnabled = true,
};
connection.StateChange += (_, e) =>
{
if (e.CurrentState == ConnectionState.Closed)
{
trips.Add((long)connection.RetrieveStatistics()["ServerRoundtrips"]!);
}
};
return connection;
};
#pragma warning restore CS0618

[Fact]
public static async Task TwoCommands_FromOneSqlServerFunction_ReadTheNamesOnce()
{
Expand All @@ -356,29 +330,6 @@ await RunAsync(
Assert.Equal(second, third);
}

#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
[Fact]
public static async Task TwoCommands_FromOneLegacySqlServerFunction_ReadTheNamesOnce()
{
LocalDbFixture.SkipUnlessAvailable();
using LocalDbFixture.Scratch procedure = LocalDbFixture.CreateProcedure("@region int");
string name = "dbo." + procedure.Name;
var trips = new Trips();
Func<System.Data.SqlClient.SqlConnection> create = CountingLegacy(trips);

await RunAsync(create.StoredProcedure(name));
long first = trips.Take();
await RunAsync(create.StoredProcedure(name));
long second = trips.Take();
await RunAsync(create.Command(name, CommandType.StoredProcedure));
long third = trips.Take();

Assert.True(first > 0);
Assert.True(second < first, $"The second command paid for the names again: {first} round trips, then {second}.");
Assert.Equal(second, third);
}
#pragma warning restore CS0618

// ---- distinct sources stay distinct (for a name without a schema) ----

[Fact]
Expand Down Expand Up @@ -560,14 +511,5 @@ public static void ANullFunction_IsRefused_BySqlServerExtensions()
{
Assert.Equal("ArgumentNullException:factory", Refusal(() => MsExtensions.StoredProcedure((Func<SqlConnection>)null!, "proc")));
Assert.Equal("ArgumentNullException:factory", Refusal(() => MsExtensions.Command((Func<SqlConnection>)null!, "proc", CommandType.StoredProcedure)));
#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
Func<System.Data.SqlClient.SqlConnection> none = null!;
Assert.Equal(
"ArgumentNullException:factory",
Refusal(() => none.StoredProcedure("proc")));
Assert.Equal(
"ArgumentNullException:factory",
Refusal(() => none.Command("proc", CommandType.StoredProcedure)));
#pragma warning restore CS0618
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<ProjectReference Include="..\..\Source\Core\Open.Database.Extensions.Core.csproj" />
<ProjectReference Include="..\..\Source\Dataflow\Open.Database.Extensions.Dataflow.csproj" />
<ProjectReference Include="..\..\Source\Extensions\Open.Database.Extensions.csproj" />
<ProjectReference Include="..\..\Source\SqlClient\Open.Database.Extensions.SqlClient.csproj" />
<ProjectReference Include="..\..\Source\MSSqlClient\Open.Database.Extensions.MSSqlClient.csproj" Aliases="mssql" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,13 @@ public static TheoryData<LanguageVersion> Versions
return [.. paths.Select(static p => (MetadataReference)MetadataReference.CreateFromFile(p))];
});

// The same, without the legacy System.Data.SqlClient adapter, which
// defines a type of the same name (CS0433), and with the
// Microsoft.Data.SqlClient package.
// The same, with the Microsoft.Data.SqlClient package.
static readonly Lazy<MetadataReference[]> SqlReferences = new(static () =>
[
.. References.Value.Where(static r => !IsLegacySqlClient(r)),
.. References.Value,
MetadataReference.CreateFromFile(typeof(mssql::Open.Database.Extensions.ExpressiveSqlCommand).Assembly.Location),
]);

static bool IsLegacySqlClient(MetadataReference reference)
=> reference is PortableExecutableReference { FilePath: { } path }
&& Path.GetFileName(path).Equals(
"Open.Database.Extensions.SqlClient.dll",
StringComparison.OrdinalIgnoreCase);

// Each statement is compiled in a method with: c (ExpressiveCommand),
// q (the SQL Server command, when sql is true), s (string), i (int),
// l (long), t (DbType?), st (SqlDbType?).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ public static class ParameterByteZeroCompileTests
{
foreach (string path in tpa.Split(Path.PathSeparator))
{
// The legacy adapter defines types of the same name as the SQL
// Server package (CS0433): only the package is referenced.
string file = Path.GetFileName(path);
if (file.Equals(
"Open.Database.Extensions.SqlClient.dll",
StringComparison.OrdinalIgnoreCase))
{
continue;
}

paths.Add(path);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Open.Database.Extensions.Core.Tests/RowMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public static void EveryOverrideParameterIsAMap()
Assembly[] assemblies =
[
typeof(RowMap<>).Assembly, typeof(ChannelDbExtensions).Assembly, typeof(DataflowExtensions).Assembly,
Assembly.Load("Open.Database.Extensions"), Assembly.Load("Open.Database.Extensions.SqlClient"), Assembly.Load("Open.Database.Extensions.MSSqlClient"),
Assembly.Load("Open.Database.Extensions"), Assembly.Load("Open.Database.Extensions.MSSqlClient"),
];
const BindingFlags all = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase[] methods = [.. assemblies.SelectMany(a => a.GetExportedTypes()).SelectMany(t => t.GetMethods(all).Cast<MethodBase>().Concat(t.GetConstructors(all)))];
Expand Down
Loading