static string Query(DatabasePlan databasePlan, UnionPlan unionPlan) => $@" /// <summary> /// Build and execute a query filtered to {unionPlan.CommonName} documents. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// <see cref=""https://github.com/Azure/azure-cosmos-dotnet-v3/blob/bb72ba5786d99d928b4774e16810f2655029e8a2/Microsoft.Azure.Cosmos/src/Linq/CosmosLinqExtensions.cs"" /> /// </summary> public virtual System.Collections.Generic.IAsyncEnumerable<T> {unionPlan.CommonName.Pluralize()}<T>( System.Func<System.Linq.IQueryable<{unionPlan.FullCommonTypeName}>, System.Linq.IQueryable<T>> createQuery, System.Threading.CancellationToken cancellationToken = default) => this.{databasePlan.DbClassName} .ExecuteQueryAsync( query: createQuery(this.{databasePlan.QueryBuilderClassName}.Unions.{unionPlan.CommonName.Pluralize()}()), cancellationToken: cancellationToken); /// <summary> /// Execute a query filtered to {unionPlan.CommonName} documents. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> public virtual System.Collections.Generic.IAsyncEnumerable<{unionPlan.FullCommonTypeName}> {unionPlan.CommonName.Pluralize()}( System.Threading.CancellationToken cancellationToken = default) => this.{databasePlan.DbClassName} .ExecuteQueryAsync( query: this.{databasePlan.QueryBuilderClassName}.Unions.{unionPlan.CommonName.Pluralize()}(), cancellationToken: cancellationToken); ";
static string Read(DatabasePlan databasePlan, UnionPlan unionPlan) => $@" /// <summary> /// Try to load a {unionPlan.CommonName} by id. /// id should be transformed using Cosmogenesis.Core.DbDocHelper.GetValidId. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> protected virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}?> {unionPlan.CommonName}Async(string id) => ({unionPlan.FullCommonTypeName}?)await this.{databasePlan.DbClassName}.ReadByIdAsync( partitionKey: this.PartitionKey, id: id); /// <summary> /// Try to load a {unionPlan.CommonName} by id. /// Returns the {unionPlan.CommonName} or null if not found. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> public virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}?> {unionPlan.CommonName}Async({unionPlan.GetIdPlan.AsInputParameters()}) => ({unionPlan.FullCommonTypeName}?)await this.{databasePlan.DbClassName}.ReadByIdAsync( partitionKey: this.PartitionKey, id: Cosmogenesis.Core.DbDocHelper.GetValidId({unionPlan.GetIdPlan.FullMethodName}({unionPlan.GetIdPlan.AsInputParameterMapping()}))); ";
static string BuildQuery(UnionPlan unionPlan) => $@" static readonly string[] {unionPlan.CommonName}_Types = new[] {{ {string.Join(", ", unionPlan.Documents.Select(x => x.ConstDocType))} }}; /// <summary> /// Build a query filtered to {unionPlan.CommonName} documents. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// Additional Linq transformations can be appended. /// Use ExecuteQueryAsync to execute. /// <see cref=""https://github.com/Azure/azure-cosmos-dotnet-v3/blob/bb72ba5786d99d928b4774e16810f2655029e8a2/Microsoft.Azure.Cosmos/src/Linq/CosmosLinqExtensions.cs"" /> /// </summary> public virtual System.Linq.IQueryable<{unionPlan.FullCommonTypeName}> {unionPlan.CommonName.Pluralize()}() => this.BuildQueryByTypes<{unionPlan.FullCommonTypeName}>(types: {unionPlan.CommonName}_Types); ";
static string ReadOrThrow(DatabasePlan databasePlan, UnionPlan unionPlan) => $@" /// <summary> /// Try to load a {unionPlan.CommonName} by id. /// id should be transformed using Cosmogenesis.Core.DbDocHelper.GetValidId. /// Returns the {unionPlan.CommonName} or throws DbConflictException if not found. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbConflictException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> protected virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}> {unionPlan.CommonName}Async(string id) {{ var result = ({unionPlan.FullCommonTypeName}?)await this.{databasePlan.DbClassName}.ReadByIdAsync( partitionKey: this.PartitionKey, id: id); if (result is null) {{ throw Cosmogenesis.Core.DbModelFactory.CreateDbConflictException(dbConflictType: Cosmogenesis.Core.DbConflictType.Missing); }} return result; }} /// <summary> /// Try to load a {unionPlan.CommonName} by id. /// Returns the {unionPlan.CommonName} or throws DbConflictException if not found. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbConflictException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> public virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}> {unionPlan.CommonName}Async({unionPlan.GetIdPlan.AsInputParameters()}) {{ var result = ({unionPlan.FullCommonTypeName}?)await this.{databasePlan.DbClassName}.ReadByIdAsync( partitionKey: this.PartitionKey, id: Cosmogenesis.Core.DbDocHelper.GetValidId({unionPlan.GetIdPlan.FullMethodName}({unionPlan.GetIdPlan.AsInputParameterMapping()}))); if (result is null) {{ throw Cosmogenesis.Core.DbModelFactory.CreateDbConflictException(dbConflictType: Cosmogenesis.Core.DbConflictType.Missing); }} return result; }} ";
static string ReadById(DatabasePlan databasePlan, UnionPlan unionPlan) => $@" /// <summary> /// Try to load {unionPlan.CommonName} documents by id. /// id should be transformed using Cosmogenesis.Core.DbDocHelper.GetValidId. /// Returns an array of {unionPlan.CommonName} documents (or null if not found) in the same order as the ids were provided. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> protected virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}?[]> {unionPlan.CommonName.Pluralize()}ByIdAsync(System.Collections.Generic.IEnumerable<string> ids) {{ var docs = await this.{databasePlan.DbClassName}.ReadByIdsAsync( partitionKey: this.PartitionKey, ids: ids); var results = new {unionPlan.FullCommonTypeName}?[docs.Length]; for (var x = 0; x < docs.Length; ++x) {{ results[x] = ({unionPlan.FullCommonTypeName}?)docs[x]; }} return results; }} ";
static string Read(DatabasePlan databasePlan, UnionPlan unionPlan) { var singleType = unionPlan.GetIdPlan.Arguments[0].FullTypeName; var singleTypeParam = unionPlan.GetIdPlan.Arguments[0].ArgumentName.Pluralize(); var inputParams = unionPlan.GetIdPlan.Arguments.Count == 1 ? $"System.Collections.Generic.IEnumerable<{singleType}> {singleTypeParam}" : $"System.Collections.Generic.IEnumerable<({unionPlan.GetIdPlan.AsInputParameters()})> ids"; var toId = unionPlan.GetIdPlan.Arguments.Count == 1 ? $"{singleTypeParam}.Select({unionPlan.GetIdPlan.FullMethodName}).Select(Cosmogenesis.Core.DbDocHelper.GetValidId)" : $"ids.Select(x => {unionPlan.GetIdPlan.FullMethodName}({unionPlan.GetIdPlan.ParametersToParametersMapping("x")})).Select(Cosmogenesis.Core.DbDocHelper.GetValidId)"; return($@" /// <summary> /// Try to load {unionPlan.CommonName} documents by id. /// Returns an array of {unionPlan.CommonName} documents (or null if not found) in the same order as the ids were provided. /// {unionPlan.CommonName} is a union of: {string.Join(", ", unionPlan.Documents.Select(x => x.ClassName))} /// </summary> /// <exception cref=""Cosmogenesis.Core.DbOverloadedException"" /> /// <exception cref=""Cosmogenesis.Core.DbUnknownStatusCodeException"" /> public virtual async System.Threading.Tasks.Task<{unionPlan.FullCommonTypeName}?[]> {unionPlan.CommonName.Pluralize()}Async({inputParams}) {{ var docs = await this.{databasePlan.DbClassName}.ReadByIdsAsync( partitionKey: this.PartitionKey, ids: {toId}); var results = new {unionPlan.FullCommonTypeName}?[docs.Length]; for (var x = 0; x < docs.Length; ++x) {{ results[x] = ({unionPlan.FullCommonTypeName}?)docs[x]; }} return results; }} "); }