|
1 |
| -using System.Data; |
| 1 | +using System.Collections.Frozen; |
| 2 | +using System.Data; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Reflection; |
2 | 6 | using Bit.Core.Entities;
|
3 | 7 | using Bit.Core.Models.Data;
|
4 | 8 | using Dapper;
|
|
7 | 11 |
|
8 | 12 | namespace Bit.Infrastructure.Dapper;
|
9 | 13 |
|
| 14 | +/// <summary> |
| 15 | +/// Provides a way to build a <see cref="DataTable"/> based on the properties of <see cref="T"/>. |
| 16 | +/// </summary> |
| 17 | +/// <typeparam name="T"></typeparam> |
| 18 | +public class DataTableBuilder<T> |
| 19 | +{ |
| 20 | + private readonly FrozenDictionary<string, (Type Type, Func<T, object?> Getter)> _columnBuilders; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Creates a new instance of <see cref="DataTableBuilder{T}"/>. |
| 24 | + /// </summary> |
| 25 | + /// <example> |
| 26 | + /// <code> |
| 27 | + /// new DataTableBuilder<MyObject>( |
| 28 | + /// [ |
| 29 | + /// i => i.Id, |
| 30 | + /// i => i.Name, |
| 31 | + /// ] |
| 32 | + /// ); |
| 33 | + /// </code> |
| 34 | + /// </example> |
| 35 | + /// <param name="columnExpressions"></param> |
| 36 | + /// <exception cref="ArgumentException"></exception> |
| 37 | + public DataTableBuilder(Expression<Func<T, object?>>[] columnExpressions) |
| 38 | + { |
| 39 | + ArgumentNullException.ThrowIfNull(columnExpressions); |
| 40 | + ArgumentOutOfRangeException.ThrowIfZero(columnExpressions.Length); |
| 41 | + |
| 42 | + var columnBuilders = new Dictionary<string, (Type Type, Func<T, object?>)>(columnExpressions.Length); |
| 43 | + |
| 44 | + for (var i = 0; i < columnExpressions.Length; i++) |
| 45 | + { |
| 46 | + var columnExpression = columnExpressions[i]; |
| 47 | + |
| 48 | + if (!TryGetPropertyInfo(columnExpression, out var propertyInfo)) |
| 49 | + { |
| 50 | + throw new ArgumentException($"Could not determine the property info from the given expression '{columnExpression}'."); |
| 51 | + } |
| 52 | + |
| 53 | + // Unwrap possible Nullable<T> |
| 54 | + var type = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType; |
| 55 | + |
| 56 | + // This needs to be after unwrapping the `Nullable` since enums can be nullable |
| 57 | + if (type.IsEnum) |
| 58 | + { |
| 59 | + // Get the backing type of the enum |
| 60 | + type = Enum.GetUnderlyingType(type); |
| 61 | + } |
| 62 | + |
| 63 | + if (!columnBuilders.TryAdd(propertyInfo.Name, (type, columnExpression.Compile()))) |
| 64 | + { |
| 65 | + throw new ArgumentException($"Property with name '{propertyInfo.Name}' was already added, properties can only be added once."); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + _columnBuilders = columnBuilders.ToFrozenDictionary(); |
| 70 | + } |
| 71 | + |
| 72 | + private static bool TryGetPropertyInfo(Expression<Func<T, object?>> columnExpression, [MaybeNullWhen(false)] out PropertyInfo property) |
| 73 | + { |
| 74 | + property = null; |
| 75 | + |
| 76 | + // Reference type properties |
| 77 | + // i => i.Data |
| 78 | + if (columnExpression.Body is MemberExpression { Member: PropertyInfo referencePropertyInfo }) |
| 79 | + { |
| 80 | + property = referencePropertyInfo; |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + // Value type properties will implicitly box into the object so |
| 85 | + // we need to look past the Convert expression |
| 86 | + // i => (System.Object?)i.Id |
| 87 | + if ( |
| 88 | + columnExpression.Body is UnaryExpression |
| 89 | + { |
| 90 | + NodeType: ExpressionType.Convert, |
| 91 | + Operand: MemberExpression { Member: PropertyInfo valuePropertyInfo }, |
| 92 | + } |
| 93 | + ) |
| 94 | + { |
| 95 | + // This could be an implicit cast from the property into our return type object? |
| 96 | + property = valuePropertyInfo; |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + // Other possible expression bodies here |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + public DataTable Build(IEnumerable<T> source) |
| 105 | + { |
| 106 | + ArgumentNullException.ThrowIfNull(source); |
| 107 | + |
| 108 | + var table = new DataTable(); |
| 109 | + |
| 110 | + foreach (var (name, (type, _)) in _columnBuilders) |
| 111 | + { |
| 112 | + table.Columns.Add(new DataColumn(name, type)); |
| 113 | + } |
| 114 | + |
| 115 | + foreach (var entity in source) |
| 116 | + { |
| 117 | + var row = table.NewRow(); |
| 118 | + |
| 119 | + foreach (var (name, (_, getter)) in _columnBuilders) |
| 120 | + { |
| 121 | + var value = getter(entity); |
| 122 | + if (value is null) |
| 123 | + { |
| 124 | + row[name] = DBNull.Value; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + row[name] = value; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + table.Rows.Add(row); |
| 133 | + } |
| 134 | + |
| 135 | + return table; |
| 136 | + } |
| 137 | +} |
| 138 | + |
10 | 139 | public static class DapperHelpers
|
11 | 140 | {
|
| 141 | + private static readonly DataTableBuilder<OrganizationSponsorship> _organizationSponsorshipTableBuilder = new( |
| 142 | + [ |
| 143 | + os => os.Id, |
| 144 | + os => os.SponsoringOrganizationId, |
| 145 | + os => os.SponsoringOrganizationUserId, |
| 146 | + os => os.SponsoredOrganizationId, |
| 147 | + os => os.FriendlyName, |
| 148 | + os => os.OfferedToEmail, |
| 149 | + os => os.PlanSponsorshipType, |
| 150 | + os => os.LastSyncDate, |
| 151 | + os => os.ValidUntil, |
| 152 | + os => os.ToDelete, |
| 153 | + ] |
| 154 | + ); |
| 155 | + |
12 | 156 | public static DataTable ToGuidIdArrayTVP(this IEnumerable<Guid> ids)
|
13 | 157 | {
|
14 | 158 | return ids.ToArrayTVP("GuidId");
|
@@ -63,24 +207,9 @@ public static DataTable ToArrayTVP(this IEnumerable<CollectionAccessSelection> v
|
63 | 207 |
|
64 | 208 | public static DataTable ToTvp(this IEnumerable<OrganizationSponsorship> organizationSponsorships)
|
65 | 209 | {
|
66 |
| - var table = new DataTable(); |
| 210 | + var table = _organizationSponsorshipTableBuilder.Build(organizationSponsorships ?? []); |
67 | 211 | table.SetTypeName("[dbo].[OrganizationSponsorshipType]");
|
68 |
| - |
69 |
| - var columnData = new List<(string name, Type type, Func<OrganizationSponsorship, object?> getter)> |
70 |
| - { |
71 |
| - (nameof(OrganizationSponsorship.Id), typeof(Guid), ou => ou.Id), |
72 |
| - (nameof(OrganizationSponsorship.SponsoringOrganizationId), typeof(Guid), ou => ou.SponsoringOrganizationId), |
73 |
| - (nameof(OrganizationSponsorship.SponsoringOrganizationUserId), typeof(Guid), ou => ou.SponsoringOrganizationUserId), |
74 |
| - (nameof(OrganizationSponsorship.SponsoredOrganizationId), typeof(Guid), ou => ou.SponsoredOrganizationId), |
75 |
| - (nameof(OrganizationSponsorship.FriendlyName), typeof(string), ou => ou.FriendlyName), |
76 |
| - (nameof(OrganizationSponsorship.OfferedToEmail), typeof(string), ou => ou.OfferedToEmail), |
77 |
| - (nameof(OrganizationSponsorship.PlanSponsorshipType), typeof(byte), ou => ou.PlanSponsorshipType), |
78 |
| - (nameof(OrganizationSponsorship.LastSyncDate), typeof(DateTime), ou => ou.LastSyncDate), |
79 |
| - (nameof(OrganizationSponsorship.ValidUntil), typeof(DateTime), ou => ou.ValidUntil), |
80 |
| - (nameof(OrganizationSponsorship.ToDelete), typeof(bool), ou => ou.ToDelete), |
81 |
| - }; |
82 |
| - |
83 |
| - return organizationSponsorships.BuildTable(table, columnData); |
| 212 | + return table; |
84 | 213 | }
|
85 | 214 |
|
86 | 215 | public static DataTable BuildTable<T>(this IEnumerable<T> entities, DataTable table,
|
|
0 commit comments