|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using MySqlConnector;
namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// A resource that represents a MySQL database. This is a child resource of a <see cref="MySqlServerResource"/>.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="parent">The MySQL parent resource associated with this database.</param>
public class MySqlDatabaseResource(string name, string databaseName, MySqlServerResource parent)
: Resource(name), IResourceWithParent<MySqlServerResource>, IResourceWithConnectionString
{
/// <summary>
/// Gets the parent MySQL container resource.
/// </summary>
public MySqlServerResource Parent { get; } = parent ?? throw new ArgumentNullException(nameof(parent));
/// <summary>
/// Gets the connection string expression for the MySQL database.
/// </summary>
public ReferenceExpression ConnectionStringExpression
{
get
{
var connectionStringBuilder = new MySqlConnectionStringBuilder
{
["Database"] = DatabaseName
};
return ReferenceExpression.Create($"{Parent};{connectionStringBuilder.ToString()}");
}
}
/// <summary>
/// Gets the connection URI expression for the MySQL database.
/// </summary>
/// <remarks>
/// Format: <c>mysql://{user}:{password}@{host}:{port}/{database}</c>.
/// </remarks>
public ReferenceExpression UriExpression => ReferenceExpression.Create($"{Parent.UriExpression}/{DatabaseName:uri}");
/// <summary>
/// Gets the JDBC connection string for the MySQL database.
/// </summary>
/// <remarks>
/// Format: <c>jdbc:mysql://{host}:{port}/{database}?user={user}&password={password}</c>.
/// </remarks>
public ReferenceExpression JdbcConnectionString => Parent.BuildJdbcConnectionString(DatabaseName);
/// <summary>
/// Gets the database name.
/// </summary>
public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName);
private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
return argument;
}
IEnumerable<KeyValuePair<string, ReferenceExpression>> IResourceWithConnectionString.GetConnectionProperties() =>
Parent.CombineProperties([
new("Database", ReferenceExpression.Create($"{DatabaseName}")),
new("Uri", UriExpression),
new("JdbcConnectionString", JdbcConnectionString),
]);
}
|