Files
Umbraco-CMS/src/Umbraco.Cms.Persistence.SqlServer/Services/SqlServerDatabaseProviderMetadata.cs
Henrik c64ec51305 Nonbreaking performance tweaks (#17106)
* Avoid doing multiple lookups in dictionaries, avoid doing string interpolation & adding single char strings to a StringBuilder, made some private/internal classes & some private methods static when possible, use FrozenSet for InvalidFileNameChars

* Avoid some array + list allocations & async methods and made some private methods static

* Avoid double lookup of XML attribute (and double null check) & avoid an unneeded lookup before writing to a dictionary

* Avoid some double lookups

# Conflicts:
#	src/Umbraco.Core/Services/LocalizedTextService.cs

* Avoid double lookups

# Conflicts:
#	src/Umbraco.Core/Services/LocalizedTextService.cs

* Avoid double lookups

* List AsSpan, also to trigger a new build that hopefully goes through

* Avoid concatting strings when using writer & more static

* Updated CollectionBenchmarks to show that ToArray isn't always the fastest & Lists can be iterated nearly as fast as arrays (and that ToList is nearly as fast as ToArray on IReadOnlyLists in .NET 8)

* Fix rebase

* Use explicit types ❤️ (I thought it was the other way round...)

# Conflicts:
#	src/Umbraco.Core/Services/LocalizedTextService.cs

* Reduce number of lines in HtmlStringUtilities.Truncate to pass code quality analysis

* Avoid double lookups & allocating empty arrays

* Use native List Find instead of LINQ
2025-01-31 10:31:06 +01:00

110 lines
2.9 KiB
C#

using System.Data.Common;
using System.Runtime.Serialization;
using Microsoft.Data.SqlClient;
using Umbraco.Cms.Core.Install.Models;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Extensions;
namespace Umbraco.Cms.Persistence.SqlServer.Services;
/// <summary>
/// Provider metadata for SQL Server
/// </summary>
[DataContract]
public class SqlServerDatabaseProviderMetadata : IDatabaseProviderMetadata
{
/// <inheritdoc />
public Guid Id => new("5e1ad149-1951-4b74-90bf-2ac2aada9e73");
/// <inheritdoc />
public int SortOrder => 2;
/// <inheritdoc />
public string DisplayName => "SQL Server";
/// <inheritdoc />
public string DefaultDatabaseName => string.Empty;
/// <inheritdoc />
public string ProviderName => Constants.ProviderName;
/// <inheritdoc />
public bool SupportsQuickInstall => false;
/// <inheritdoc />
public bool IsAvailable => true;
/// <inheritdoc />
public bool RequiresServer => true;
/// <inheritdoc />
public string ServerPlaceholder => "(local)\\SQLEXPRESS";
/// <inheritdoc />
public bool RequiresCredentials => true;
/// <inheritdoc />
public bool SupportsIntegratedAuthentication => true;
/// <inheritdoc />
public bool RequiresConnectionTest => true;
/// <inheritdoc />
public bool ForceCreateDatabase => true;
/// <inheritdoc />
public bool CanRecognizeConnectionString(string? connectionString)
{
if (connectionString is null)
{
return false;
}
try
{
var builder = new SqlConnectionStringBuilder(connectionString);
return string.IsNullOrEmpty(builder.AttachDBFilename);
}
catch (ArgumentException)
{
return false;
}
}
/// <inheritdoc />
public string GenerateConnectionString(DatabaseModel databaseModel)
{
string connectionString = $"Server={databaseModel.Server};Database={databaseModel.DatabaseName};";
connectionString = HandleIntegratedAuthentication(connectionString, databaseModel);
connectionString = HandleTrustServerCertificate(connectionString, databaseModel);
return connectionString;
}
private static string HandleIntegratedAuthentication(string connectionString, DatabaseModel databaseModel)
{
if (databaseModel.IntegratedAuth)
{
connectionString += "Integrated Security=true";
}
else
{
connectionString += $"User Id={databaseModel.Login};Password={databaseModel.Password}";
}
return connectionString;
}
private static string HandleTrustServerCertificate(string connectionString, DatabaseModel databaseModel)
{
if (databaseModel.TrustServerCertificate)
{
connectionString += ";TrustServerCertificate=true;";
}
return connectionString;
}
}