Skip to content

Commit 010d1dc

Browse files
authored
Merge pull request #1163 from progressonderwijs/fons/view_lookup
DatabaseDescription.TryGetViewByName utility method added.
2 parents 051543b + ebf669f commit 010d1dc

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/ProgressOnderwijsUtils/ProgressOnderwijsUtils.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\NugetPackagesCommon.props" />
33
<PropertyGroup Label="Configuration">
4-
<Version>107.0.0</Version>
5-
<PackageReleaseNotes>The _class attribute can use string based classnames again and is no longer marked as obsolete.</PackageReleaseNotes>
4+
<Version>107.1.0</Version>
5+
<PackageReleaseNotes>DatabaseDescription.TryGetViewByName utility method added.</PackageReleaseNotes>
66
<Title>ProgressOnderwijsUtils</Title>
77
<Description>Collection of utilities developed by ProgressOnderwijs</Description>
88
<PackageTags>ProgressOnderwijs</PackageTags>

src/ProgressOnderwijsUtils/SchemaReflection/DatabaseDescription.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public sealed class DatabaseDescription
77
readonly IReadOnlyDictionary<DbObjectId, Table> tableById;
88
readonly IReadOnlyDictionary<DbObjectId, View> viewById;
99
readonly IReadOnlyDictionary<string, Table> tableByQualifiedName;
10+
readonly IReadOnlyDictionary<string, View> viewByQualifiedName;
1011
public readonly ILookup<string, ForeignKey> ForeignKeyConstraintsByUnqualifiedName;
1112
readonly ILookup<DbObjectId, ForeignKey> fksByReferencedParentObjectId;
1213
readonly ILookup<DbObjectId, ForeignKey> fksByReferencingChildObjectId;
@@ -23,6 +24,7 @@ public DatabaseDescription(RawDatabaseDescription rawDescription)
2324
fksByReferencedParentObjectId = fkObjects.ToLookup(fk => fk.ReferencedParentTable.ObjectId);
2425
fksByReferencingChildObjectId = fkObjects.ToLookup(fk => fk.ReferencingChildTable.ObjectId);
2526
tableByQualifiedName = tableById.Values.ToDictionary(o => o.QualifiedName, StringComparer.OrdinalIgnoreCase);
27+
viewByQualifiedName = viewById.Values.ToDictionary(o => o.QualifiedName, StringComparer.OrdinalIgnoreCase);
2628
ForeignKeyConstraintsByUnqualifiedName = fkObjects.ToLookup(o => o.UnqualifiedName, StringComparer.OrdinalIgnoreCase);
2729
}
2830

@@ -42,11 +44,14 @@ public Table GetTableByName(string qualifiedName)
4244
=> TryGetTableByName(qualifiedName) ?? throw new ArgumentException($"Unknown table '{qualifiedName}'.", nameof(qualifiedName));
4345

4446
public Table? TryGetTableByName(string qualifiedName)
45-
=> tableByQualifiedName.TryGetValue(qualifiedName, out var id) ? id : null;
47+
=> tableByQualifiedName.TryGetValue(qualifiedName, out var table) ? table : null;
4648

4749
public Table? TryGetTableById(DbObjectId id)
4850
=> tableById.GetValueOrDefault(id);
4951

52+
public View? TryGetViewByName(string qualifiedName)
53+
=> viewByQualifiedName.TryGetValue(qualifiedName, out var view) ? view : null;
54+
5055
public sealed record ForeignKeyColumn(Column<Table> ReferencedParentColumn, Column<Table> ReferencingChildColumn);
5156

5257
public sealed class ForeignKey

test/ProgressOnderwijsUtils.Tests/Data/DatabaseDescriptionTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,23 @@ public void User_table_dbo_dtproperties_is_not_part_of_the_db_description()
220220

221221
PAssert.That(() => db.TryGetTableByName("dbo.dtproperties") == null);
222222
}
223+
224+
[Fact]
225+
public void TrygetViewByName_works()
226+
{
227+
SQL(
228+
$"""
229+
create view dbo.TrygetViewByName_works as
230+
select x = 1;
231+
"""
232+
).ExecuteNonQuery(Connection);
233+
234+
var db = DatabaseDescription.LoadFromSchemaTables(Connection);
235+
var view = db.TryGetViewByName("dbo.TrygetViewByName_works").AssertNotNull();
236+
237+
PAssert.That(() => view.QualifiedName == "dbo.TrygetViewByName_works");
238+
PAssert.That(() => view.Columns.Single().ColumnName == "x");
239+
240+
PAssert.That(() => db.TryGetViewByName("dbo.DoesNotExist") == null);
241+
}
223242
}

0 commit comments

Comments
 (0)