Skip to content

Commit

Permalink
Fix JoinTo error.
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun committed Aug 7, 2023
1 parent c4578ba commit 4b5bcd2
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v2.2.1
- *Fixed:* Issue with `Joins` where the `JoinTo` property within the YAML was validating against an underlying table name versus the unqiue `Table.Name` property (as intended). The `JoinToSchema` property should have been internal only and has not been corrected.

## v2.2.0
- *Enhancement:* Added `publisher` option with values `Console|Function|None`, where `Console` is the default, and `Function` represents [Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview), for the `NTangle.Template` package.
- *Fixed:* The `IdentifierMappingMapper.cs` file was incorrrectly being generated when not required.
Expand Down
1 change: 0 additions & 1 deletion docs/generated/join.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Provides the _join to_ configuration.
Property | Description
-|-
**`joinTo`** | The name of the table to join to (must be previously specified).<br/>&dagger; Defaults to parent `Table.Name`.
**`joinToSchema`** | The schema name of the table to join to.<br/>&dagger; Defaults to parent `Table.Schema`.
`joinCardinality` | The join cardinality being whether there is a One-to-Many or One-to-One relationship. Valid options are: `OneToMany`, `OneToOne`.<br/>&dagger; Defaults to `OneToMany`. This represents the Parent (`JoinTo`) to child (_this_) relationship.

<br/>
Expand Down
2 changes: 1 addition & 1 deletion samples/SqlServerDemo/SqlServerDemo.Publisher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class Program
/// </summary>
/// <param name="args">The console arguments.</param>
internal static void Main(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(c => c.AddEnvironmentVariables(prefix: "SqlServerDemo_"))
.ConfigureAppConfiguration(c => c.AddEnvironmentVariables(prefix: "SqlServerDemo_"))
.ConfigureServices((services) =>
{
services.AddSettings<SqlServerDemoSettings>()
Expand Down
5 changes: 0 additions & 5 deletions schemas/ntangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,6 @@
"title": "The name of the table to join to (must be previously specified).",
"description": "Defaults to parent 'Table.Name'."
},
"joinToSchema": {
"type": "string",
"title": "The schema name of the table to join to.",
"description": "Defaults to parent 'Table.Schema'."
},
"joinCardinality": {
"type": "string",
"title": "The join cardinality being whether there is a One-to-Many or One-to-One relationship.",
Expand Down
42 changes: 25 additions & 17 deletions src/NTangle/Config/JoinConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ public class JoinConfig : ConfigBase<RootConfig, TableConfig>, ITableReference
Description = "Defaults to parent `Table.Name`.")]
public string? JoinTo { get; set; }

/// <summary>
/// Gets or sets the schema name of the parent table to join to.
/// </summary>
[JsonProperty("joinToSchema", DefaultValueHandling = DefaultValueHandling.Ignore)]
[CodeGenProperty("JoinTo", Title = "The schema name of the table to join to.", IsImportant = true,
Description = "Defaults to parent `Table.Schema`.")]
public string? JoinToSchema { get; set; }

/// <summary>
/// Get or sets the join cardinality.
/// </summary>
Expand Down Expand Up @@ -238,6 +230,16 @@ public class JoinConfig : ConfigBase<RootConfig, TableConfig>, ITableReference
/// </summary>
public DbTableSchema? DbTable { get; private set; }

/// <summary>
/// Gets the <see cref="JoinTo"/> schema.
/// </summary>
public string? JoinToSchema { get; set; }

/// <summary>
/// Gets the <see cref="JoinTo"/> table.
/// </summary>
public string? JoinToTable { get; set; }

/// <summary>
/// Gets the <see cref="JoinTo"/> alias.
/// </summary>
Expand All @@ -261,12 +263,12 @@ public class JoinConfig : ConfigBase<RootConfig, TableConfig>, ITableReference
/// <summary>
/// Gets the list of joined "directly related" children.
/// </summary>
public List<JoinConfig> JoinCdcChildren => Parent!.Joins!.Where(x => x.JoinTo == Name && x.JoinToSchema == Schema && CompareNullOrValue(x.Type, "Cdc")).ToList();
public List<JoinConfig> JoinCdcChildren => Parent!.Joins!.Where(x => x.JoinTo == Name && CompareNullOrValue(x.Type, "Cdc")).ToList();

/// <summary>
/// Gets the list of non-CDC joined "directly related" children.
/// </summary>
public List<JoinConfig> JoinNonCdcChildren => Parent!.Joins!.Where(x => x.JoinTo == Name && x.JoinToSchema == Schema && !CompareNullOrValue(x.Type, "Cdc")).ToList();
public List<JoinConfig> JoinNonCdcChildren => Parent!.Joins!.Where(x => x.JoinTo == Name && !CompareNullOrValue(x.Type, "Cdc")).ToList();

/// <summary>
/// Inidicates whether it is first in the JoinHierarchy.
Expand Down Expand Up @@ -325,8 +327,7 @@ protected override async Task PrepareAsync()

Type = DefaultWhereNull(Type, () => "Cdc");
Model = DefaultWhereNull(Model, () => StringConverter.ToPascalCase(Name));
JoinTo = DefaultWhereNull(JoinTo, () => Parent!.Table);
JoinToSchema = DefaultWhereNull(JoinToSchema, () => Parent!.Schema);
JoinTo = DefaultWhereNull(JoinTo, () => Parent!.Name);
JoinCardinality = DefaultWhereNull(JoinCardinality, () => "OneToMany");
CdcEnable = DefaultWhereNull(CdcEnable, () => Root.CdcEnable);
Property = DefaultWhereNull(Property, () => JoinCardinality == "OneToMany" ? StringConverter.ToPlural(Model) : Model);
Expand All @@ -342,19 +343,25 @@ protected override async Task PrepareAsync()

// Get the JoinTo CdcJoinConfig.
JoinConfig? jtc = null;
if (JoinTo != Parent!.Table || JoinToSchema != Parent!.Schema)
if (JoinTo != Parent!.Name)
{
var tables = Parent!.Joins!.Where(x => x.Table == JoinTo && x.Schema == JoinToSchema).ToList();
var tables = Parent!.Joins!.Where(x => x.Name == JoinTo).ToList();
if (tables.Count == 0 || Parent!.Joins!.IndexOf(this) < Parent!.Joins!.IndexOf(tables[0]))
throw new CodeGenException(this, nameof(JoinTo), $"Specified JoinTo table '[{JoinToSchema}].[{JoinTo}]' must be previously specified.");
throw new CodeGenException(this, nameof(JoinTo), $"Specified JoinTo name '{JoinTo}' must be previously specified.");
else if (tables.Count > 1)
throw new CodeGenException(this, nameof(JoinTo), $"Specified JoinTo table '[{JoinToSchema}].[{JoinTo}]' is ambiguous (more than one found).");
throw new CodeGenException(this, nameof(JoinTo), $"Specified JoinTo name '{JoinTo}' is ambiguous (more than one found).");

jtc = tables[0];
JoinToSchema = tables[0].Schema;
JoinToTable = tables[0].Table;
JoinToAlias = tables[0].Alias;
}
else
{
JoinToSchema = Parent!.Schema;
JoinToTable = Parent!.Table;
JoinToAlias = Parent!.Alias;
}

// Prepare the identifier mappings.
Mappings = await PrepareCollectionAsync(Mappings).ConfigureAwait(false);
Expand Down Expand Up @@ -514,7 +521,7 @@ private void JoinHierarchyAdd(JoinConfig jc)
{
foreach (var jci in JoinHierarchy)
{
if (jc.Name == jci.Name && jc.Schema == jci.Schema && jc.Table == jci.Table)
if (jc.Name == jci.Name)
throw new CodeGenException(this, nameof(JoinTo), $"Join table '{jc.Name} [{jc.Schema}].[{jc.Table}]' is self-referencing (within hierarchy) and has resulted in a circular reference.");
}

Expand All @@ -534,6 +541,7 @@ private JoinConfig PartialClone(bool isFirst, int indentIndex, JoinConfig? hiera
Alias = Alias,
JoinTo = JoinTo,
JoinToSchema = JoinToSchema,
JoinToTable = JoinToTable,
JoinToAlias = JoinToAlias,
JoinCardinality = JoinCardinality,
Model = Model,
Expand Down
12 changes: 6 additions & 6 deletions src/NTangle/Config/JoinOnConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ protected override Task PrepareAsync()
if (string.IsNullOrEmpty(ToStatement))
{
ToColumn = DefaultWhereNull(ToColumn, () => Name);
ToDbColumn = Root!.DbTables!.Where(x => x.Schema == Parent.JoinToSchema && x.Name == Parent.JoinTo).SingleOrDefault()?.Columns.Where(x => x.Name == ToColumn).SingleOrDefault();
ToDbColumn = Root!.DbTables!.Where(x => x.Schema == Parent.JoinToSchema && x.Name == Parent.JoinToTable).SingleOrDefault()?.Columns.Where(x => x.Name == ToColumn).SingleOrDefault();
if (ToDbColumn == null)
throw new CodeGenException(this, nameof(ToColumn), $"ToColumn '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinTo}]') not found in database.");
throw new CodeGenException(this, nameof(ToColumn), $"ToColumn '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinToTable}]') not found in database.");

if (Parent.JoinToSchema == Parent!.Parent!.Schema && Parent.JoinTo == Parent!.Parent!.Table)
if (Parent.JoinToSchema == Parent!.Parent!.Schema && Parent.JoinToTable == Parent!.Parent!.Table)
{
if (Parent!.Parent!.DbTable!.Columns.Where(x => x.Name == ToColumn).SingleOrDefault() == null)
throw new CodeGenException(this, nameof(ToColumn), $"JoinOn To '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinTo}]') not found in Table/Join configuration.");
throw new CodeGenException(this, nameof(ToColumn), $"JoinOn To '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinToTable}]') not found in Table/Join configuration.");

var jtc = Parent!.Parent!.Columns.SingleOrDefault(x => x.Name == ToColumn);
ToColumnAlias = jtc?.NameAlias ?? Name;
Expand All @@ -101,9 +101,9 @@ protected override Task PrepareAsync()
}
else
{
var t = Parent!.Parent!.Joins!.Where(x => Parent.JoinToSchema == x.Schema && Parent.JoinTo == x.Table).SingleOrDefault();
var t = Parent!.Parent!.Joins!.Where(x => Parent.JoinToSchema == x.Schema && Parent.JoinToTable == x.Table).SingleOrDefault();
if (t == null || t.DbTable!.Columns.Where(x => x.Name == ToColumn).SingleOrDefault() == null)
throw new CodeGenException(this, nameof(ToColumn), $"JoinOn To '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinTo}]') not found in Table/Join configuration.");
throw new CodeGenException(this, nameof(ToColumn), $"JoinOn To '{ToColumn}' (table '[{Parent.JoinToSchema}].[{Parent.JoinToTable}]') not found in Table/Join configuration.");

var jtc = t.Columns.SingleOrDefault(x => x.Name == ToColumn);
ToColumnAlias = jtc?.NameAlias ?? Name;
Expand Down
16 changes: 8 additions & 8 deletions src/NTangle/Config/TableConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ public class TableConfig : ConfigBase<RootConfig, RootConfig>, ITableReference
/// <summary>
/// Gets the list of CDC joined "directly related" children.
/// </summary>
public List<JoinConfig> JoinCdcChildren => Joins!.Where(x => x.JoinTo == Table && x.JoinToSchema == Schema && CompareNullOrValue(x.Type, "Cdc")).ToList();
public List<JoinConfig> JoinCdcChildren => Joins!.Where(x => x.JoinToTable == Table && x.JoinToSchema == Schema && CompareNullOrValue(x.Type, "Cdc")).ToList();

/// <summary>
/// Gets the list of non-CDC joined "directly related" children.
/// </summary>
public List<JoinConfig> JoinNonCdcChildren => Joins!.Where(x => x.JoinTo == Table && x.JoinToSchema == Schema && !CompareNullOrValue(x.Type, "Cdc")).ToList();
public List<JoinConfig> JoinNonCdcChildren => Joins!.Where(x => x.JoinToTable == Table && x.JoinToSchema == Schema && !CompareNullOrValue(x.Type, "Cdc")).ToList();

/// <summary>
/// Gets the Orchestrator constructor parameters.
Expand Down Expand Up @@ -542,8 +542,8 @@ protected override async Task PrepareAsync()
Columns.Add(cc);
}

await PrepareCtorParams().ConfigureAwait(false);
await PrepareJoins().ConfigureAwait(false);
await PrepareCtorParamsAsync().ConfigureAwait(false);
await PrepareJoinsAsync().ConfigureAwait(false);

Columns.RemoveAll(x => x.IsExcluded && !x.IsIsDeletedColumn && !x.IsUsedInJoinOn);

Expand Down Expand Up @@ -586,7 +586,7 @@ protected override async Task PrepareAsync()
UsesGlobalIdentifier = IdentifierMapping == true || Mappings!.Count > 0 || Joins!.Any(x => x.IdentifierMapping == true || (x.Mappings!.Count > 0));
SetUpExcludePropertiesFromETag();

ColumnConfigIsDeleted = await GetSpecialColumn(IsDeletedColumn).ConfigureAwait(false);
ColumnConfigIsDeleted = await GetSpecialColumnAsync(IsDeletedColumn).ConfigureAwait(false);

if (TenantIdColumns != null)
{
Expand Down Expand Up @@ -616,7 +616,7 @@ protected override async Task PrepareAsync()
/// <summary>
/// Prepares the constructor parameters.
/// </summary>
private async Task PrepareCtorParams()
private async Task PrepareCtorParamsAsync()
{
if (OrchestratorCtorParams == null || OrchestratorCtorParams.Count == 0)
return;
Expand Down Expand Up @@ -646,7 +646,7 @@ private async Task PrepareCtorParams()
/// <summary>
/// Prepares the joins.
/// </summary>
private async Task PrepareJoins()
private async Task PrepareJoinsAsync()
{
Joins ??= new List<JoinConfig>();

Expand Down Expand Up @@ -717,7 +717,7 @@ private void SetUpExcludePropertiesFromETag()
/// <summary>
/// Gets the named special column.
/// </summary>
private async Task<ColumnConfig?> GetSpecialColumn(string? name)
private async Task<ColumnConfig?> GetSpecialColumnAsync(string? name)
{
if (string.IsNullOrEmpty(name))
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/NTangle/NTangle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;netstandard2.1</TargetFrameworks>
<RootNamespace>NTangle</RootNamespace>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>NTangle Developers</Authors>
<Company>Avanade</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/NTangle/Templates/SpExecuteBatch_sql.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ BEGIN
INTO #{{Alias}}
FROM cdc.fn_cdc_get_all_changes_{{Schema}}_{{Table}}(@{{pascal Name}}MinLsn, @{{pascal Name}}MaxLsn, 'all') AS [_cdc]
{{#each JoinHierarchy}}
INNER JOIN [{{JoinToSchema}}].[{{JoinTo}}] AS [{{JoinToAlias}}] WITH (NOLOCK) ON ({{#each On}}{{#unless @first}} AND {{/unless}}{{#if Parent.IsFirstInJoinHierarchy}}[_cdc]{{else}}[{{Parent.Alias}}]{{/if}}.[{{Name}}] = {{#ifval ToStatement}}{{ToStatement}}{{else}}[{{Parent.JoinToAlias}}].[{{ToColumn}}]{{/ifval}}{{/each}})
INNER JOIN [{{JoinToSchema}}].[{{JoinToTable}}] AS [{{JoinToAlias}}] WITH (NOLOCK) ON ({{#each On}}{{#unless @first}} AND {{/unless}}{{#if Parent.IsFirstInJoinHierarchy}}[_cdc]{{else}}[{{Parent.Alias}}]{{/if}}.[{{Name}}] = {{#ifval ToStatement}}{{ToStatement}}{{else}}[{{Parent.JoinToAlias}}].[{{ToColumn}}]{{/ifval}}{{/each}})
{{/each}}
ORDER BY [_cdc].[__$start_lsn]

Expand Down
2 changes: 1 addition & 1 deletion tools/NTangle.Template/NTangle.Template.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Authors>NTangle Developers</Authors>
<Company>Avanade</Company>
<Description>NTangle template solution for use with 'dotnet new'.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NTangle" Version="2.2.0" />
<PackageReference Include="NTangle" Version="2.2.1" />
</ItemGroup>
<ItemGroup>
<None Update="ntangle.yaml">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NTangle" Version="2.2.0" />
<PackageReference Include="NTangle" Version="2.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--#endif -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NTangle" Version="2.2.0" />
<PackageReference Include="NTangle" Version="2.2.1" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class Program
/// </summary>
/// <param name="args">The console arguments.</param>
internal static void Main(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(c => c.AddEnvironmentVariables(prefix: "DomainName_"))
.ConfigureAppConfiguration(c => c.AddEnvironmentVariables(prefix: "DomainName_"))
.ConfigureServices((services) =>
{
// Adds the required _CoreEx_ services.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"ServiceBusSender": {
"QueueOrTopicName": "ntangle-stream" // Sets the Azure Servuce Bus queue or topic name for the event outbox.
},
"FileLockSynchronizerPath": ".", // Current executing directory for debugging purposes; this should be changed to a shared directory where executing proper.
//#endif
//#if (implement_publisher_function)
"NTangle": {
Expand All @@ -45,6 +44,8 @@
},
"ServiceBusSender": {
"QueueOrTopicName": "ntangle-stream" // Sets the Azure Servuce Bus queue or topic name for the event outbox.
}
},
//#else
"FileLockSynchronizerPath": "." // Current executing directory for debugging purposes; this should be changed to a shared directory where executing proper.
//#endif
}

0 comments on commit 4b5bcd2

Please sign in to comment.