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
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
run: dotnet build Ling.Configuration.Database.slnx --configuration Release --no-restore
- name: Test
run: dotnet test Ling.Configuration.Database.slnx --configuration Release --no-build --verbosity normal
- name: Verify Native AOT publish and execution
run: |
dotnet publish tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj --configuration Release --runtime linux-x64 --self-contained true -warnaserror
./tests/Ling.Configuration.Database.NativeAotSmoke/bin/Release/net11.0/linux-x64/publish/Ling.Configuration.Database.NativeAotSmoke
- name: Pack NuGet packages
run: |
mkdir -p artifacts/packages
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<VersionPrefix>0.2.0</VersionPrefix>
<VersionPrefix>0.3.0</VersionPrefix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
1 change: 1 addition & 0 deletions Ling.Configuration.Database.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</Folder>
<Folder Name="/tests/">
<Project Path="tests/Ling.Configuration.Database.Tests/Ling.Configuration.Database.Tests.csproj" />
<Project Path="tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj" />
</Folder>
<Folder Name="/samples/">
<Project Path="samples/Ling.Configuration.Database.Sample/Ling.Configuration.Database.Sample.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ling.Configuration.Database;

internal sealed class AdoNetDatabaseConfigurationLoader : IDatabaseConfigurationLoader
internal sealed partial class AdoNetDatabaseConfigurationLoader : IDatabaseConfigurationLoader
{
private readonly string _connectionString;
private readonly DbProviderFactory _providerFactory;
Expand Down Expand Up @@ -91,7 +91,7 @@ private DbCommand CreateCommand(DbConnection connection)
{
string QuoteIdentifier(string identifier)
{
if (!Regex.IsMatch(identifier, @"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant))
if (!IdentifierRegex().IsMatch(identifier))
{
throw new ArgumentException($"Database identifier '{identifier}' is invalid. Use letters, digits, and underscores, or provide a provider-specific IdentifierQuoter.");
}
Expand Down Expand Up @@ -134,4 +134,7 @@ private ConfigurationEntry ReadEntry(DbDataReader reader)
&& Convert.ToBoolean(reader.GetValue(2), CultureInfo.InvariantCulture);
return new ConfigurationEntry(key, value, isEncrypted);
}

[GeneratedRegex(@"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)]
private static partial Regex IdentifierRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0;net11.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsTrimmable>true</IsTrimmable>
<IsAotCompatible>true</IsAotCompatible>
<PackageId>Ling.Configuration.Database</PackageId>
<Title>Ling.Configuration.Database</Title>
<Description>Database backed Microsoft.Extensions.Configuration provider with polling reload support.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Using Remove="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Ling.Configuration.Database/Ling.Configuration.Database.csproj" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="11.0.0" />
</ItemGroup>
</Project>
128 changes: 128 additions & 0 deletions tests/Ling.Configuration.Database.NativeAotSmoke/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using Ling.Configuration.Database;
using Microsoft.Extensions.Configuration;

var entries = new List<(string Key, string? Value, bool IsEncrypted)>
{
("Feature:Enabled", "true", false),
("Secrets:Token", "cipher", true),
};

var factory = new InMemoryProviderFactory(entries);
var configuration = new ConfigurationBuilder()
.AddDatabase(
"InMemory",
factory,
options =>
{
options.PollingInterval = TimeSpan.FromMinutes(5);
options.Decryptor = entry => $"decrypted:{entry.Value}";
})
.Build();

try
{
if (configuration["Feature:Enabled"] != "true"
|| configuration["Secrets:Token"] != "decrypted:cipher")
{
return 1;
}

Console.WriteLine("Ling.Configuration.Database Native AOT smoke passed.");
return 0;
}
finally
{
((IDisposable)configuration).Dispose();
}

file sealed class InMemoryProviderFactory(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbProviderFactory
{
public override DbConnection CreateConnection() => new InMemoryConnection(entries);
}

file sealed class InMemoryConnection(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbConnection
{
private ConnectionState _state = ConnectionState.Closed;

[AllowNull]
public override string ConnectionString { get; set; } = string.Empty;
public override string Database => "InMemory";
public override string DataSource => "InMemory";
public override string ServerVersion => "1.0";
public override ConnectionState State => _state;
public override void ChangeDatabase(string databaseName) { }
public override void Close() => _state = ConnectionState.Closed;
public override void Open() => _state = ConnectionState.Open;
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) => throw new NotSupportedException();
protected override DbCommand CreateDbCommand() => new InMemoryCommand(entries);
}

file sealed class InMemoryCommand(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbCommand
{
[AllowNull]
public override string CommandText { get; set; } = string.Empty;
public override int CommandTimeout { get; set; }
public override CommandType CommandType { get; set; }
public override bool DesignTimeVisible { get; set; }
public override UpdateRowSource UpdatedRowSource { get; set; }
protected override DbConnection? DbConnection { get; set; }
protected override DbParameterCollection DbParameterCollection => throw new NotSupportedException();
protected override DbTransaction? DbTransaction { get; set; }
public override void Cancel() { }
public override int ExecuteNonQuery() => 0;
public override object? ExecuteScalar() => null;
public override void Prepare() { }
protected override DbParameter CreateDbParameter() => throw new NotSupportedException();
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) => new InMemoryDataReader(entries);
}

file sealed class InMemoryDataReader(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbDataReader
{
private int _index = -1;

public override object this[int ordinal] => GetValue(ordinal);
public override object this[string name] => throw new NotSupportedException();
public override int Depth => 0;
public override int FieldCount => 3;
public override bool HasRows => entries.Count > 0;
public override bool IsClosed => false;
public override int RecordsAffected => 0;

public override bool Read() => ++_index < entries.Count;
public override bool IsDBNull(int ordinal) => ordinal == 1 && entries[_index].Value is null;

public override object GetValue(int ordinal) => ordinal switch
{
0 => entries[_index].Key,
1 => entries[_index].Value ?? (object)DBNull.Value,
2 => entries[_index].IsEncrypted,
_ => throw new ArgumentOutOfRangeException(nameof(ordinal)),
};

public override bool GetBoolean(int ordinal) => throw new NotSupportedException();
public override byte GetByte(int ordinal) => throw new NotSupportedException();
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length) => throw new NotSupportedException();
public override char GetChar(int ordinal) => throw new NotSupportedException();
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length) => throw new NotSupportedException();
public override string GetDataTypeName(int ordinal) => string.Empty;
public override DateTime GetDateTime(int ordinal) => throw new NotSupportedException();
public override decimal GetDecimal(int ordinal) => throw new NotSupportedException();
public override double GetDouble(int ordinal) => throw new NotSupportedException();
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
public override Type GetFieldType(int ordinal) => typeof(object);
public override float GetFloat(int ordinal) => throw new NotSupportedException();
public override Guid GetGuid(int ordinal) => throw new NotSupportedException();
public override short GetInt16(int ordinal) => throw new NotSupportedException();
public override int GetInt32(int ordinal) => throw new NotSupportedException();
public override long GetInt64(int ordinal) => throw new NotSupportedException();
public override string GetName(int ordinal) => string.Empty;
public override int GetOrdinal(string name) => throw new NotSupportedException();
public override string GetString(int ordinal) => (string)GetValue(ordinal);
public override int GetValues(object[] values) => throw new NotSupportedException();
public override bool NextResult() => false;
public override IEnumerator GetEnumerator() => throw new NotSupportedException();
}
Loading