diff options
author | crupest <crupest@outlook.com> | 2022-12-07 20:41:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:52 +0800 |
commit | 78396f289ab50ce414bd8f65af8854ffb52fff48 (patch) | |
tree | 59f3a1ebb2a8e896ad21bdcf5736fc0328c84e76 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs | |
parent | 1870bc78d4a2733246322c5540761da852afe713 (diff) | |
download | crupest-78396f289ab50ce414bd8f65af8854ffb52fff48.tar.gz crupest-78396f289ab50ce414bd8f65af8854ffb52fff48.tar.bz2 crupest-78396f289ab50ce414bd8f65af8854ffb52fff48.zip |
Develop secret api. v17
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs | 115 |
1 files changed, 109 insertions, 6 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs index c31a13e..05ee269 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs @@ -1,8 +1,50 @@ namespace CrupestApi.Commons.Crud; +public static class ColumnMetadataKeys +{ + public const string ColumnName = nameof(ColumnAttribute.ColumnName); + public const string NotNull = nameof(ColumnAttribute.NotNull); + public const string IsPrimaryKey = nameof(ColumnAttribute.IsPrimaryKey); + public const string IsAutoIncrement = nameof(ColumnAttribute.IsAutoIncrement); + public const string Index = nameof(ColumnAttribute.Index); + public const string DefaultEmptyForString = nameof(ColumnAttribute.DefaultEmptyForString); +} + public interface IColumnMetadata { + bool TryGetValue(string key, out object? value); + + object? GetValueOrDefault(string key) + { + if (TryGetValue(key, out var value)) + { + return value; + } + else + { + return null; + } + } + T? GetValueOrDefault<T>(string key) + { + return (T?)GetValueOrDefault(key); + } + + object? this[string key] + { + get + { + if (TryGetValue(key, out var value)) + { + return value; + } + else + { + throw new KeyNotFoundException("Key not found."); + } + } + } } public enum ColumnIndexType @@ -16,19 +58,80 @@ public enum ColumnIndexType public class ColumnAttribute : Attribute, IColumnMetadata { // if null, use the property name. - public string? DatabaseName { get; set; } + public string? ColumnName { get; init; } // default false. - public bool NonNullable { get; set; } + public bool NotNull { get; init; } // default false - public bool IsPrimaryKey { get; set; } + public bool IsPrimaryKey { get; init; } // default false - public bool IsAutoIncrement { get; set; } + public bool IsAutoIncrement { get; init; } - public ColumnIndexType IndexType { get; set; } = ColumnIndexType.None; + // default None + public ColumnIndexType Index { get; init; } = ColumnIndexType.None; // Use empty string for default value of string type. - public bool DefaultEmptyForString { get; set; } + public bool DefaultEmptyForString { get; init; } + + public bool TryGetValue(string key, out object? value) + { + var property = GetType().GetProperty(key); + if (property is null) + { + value = null; + return false; + } + value = property.GetValue(this); + return true; + } +} + +public class AggregateColumnMetadata : IColumnMetadata +{ + private IDictionary<string, object?> _own = new Dictionary<string, object?>(); + private IList<IColumnMetadata> _children = new List<IColumnMetadata>(); + + public void Add(string key, object? value) + { + _own[key] = value; + } + + public void Remove(string key) + { + _own.Remove(key); + } + + public void Add(IColumnMetadata child) + { + _children.Add(child); + } + + public void Remove(IColumnMetadata child) + { + _children.Remove(child); + } + + public bool TryGetValue(string key, out object? value) + { + if (_own.ContainsKey(key)) + { + value = _own[key]; + return true; + } + + bool found = false; + value = null; + foreach (var child in _children) + { + if (child.TryGetValue(key, out var tempValue)) + { + value = tempValue; + found = true; + } + } + + return found; + } } |