aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-07 20:41:20 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commit78396f289ab50ce414bd8f65af8854ffb52fff48 (patch)
tree59f3a1ebb2a8e896ad21bdcf5736fc0328c84e76 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
parent1870bc78d4a2733246322c5540761da852afe713 (diff)
downloadcrupest-78396f289ab50ce414bd8f65af8854ffb52fff48.tar.gz
crupest-78396f289ab50ce414bd8f65af8854ffb52fff48.tar.bz2
crupest-78396f289ab50ce414bd8f65af8854ffb52fff48.zip
Develop secret api. v17
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs109
1 files changed, 32 insertions, 77 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
index 081071f..e60b202 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
@@ -1,113 +1,68 @@
-using System.Data;
+using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace CrupestApi.Commons.Crud;
-public delegate Task EntityPreSave(object? entity, ColumnInfo column, TableInfo table, IDbConnection connection);
-public delegate Task EntityPostGet(object? entity, ColumnInfo column, TableInfo table, IDbConnection connection);
-
public class ColumnInfo
{
- private Type ExtractRealTypeFromNullable(Type type)
- {
- if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
- {
- return type.GetGenericArguments()[0];
- }
-
- return type;
- }
+ private readonly AggregateColumnMetadata _metadata = new AggregateColumnMetadata();
- // A column with no property.
- public ColumnInfo(Type entityType, string sqlColumnName, bool isPrimaryKey, bool isAutoIncrement, ColumnTypeInfo typeInfo, ColumnIndexType indexType = ColumnIndexType.None, ColumnTypeRegistry? typeRegistry = null)
+ public ColumnInfo(Type entityType, IColumnMetadata metadata, Type clrType, IColumnTypeProvider typeProvider)
{
- if (typeRegistry is null)
- {
- typeRegistry = ColumnTypeRegistry.Instance;
- }
-
EntityType = entityType;
- PropertyName = sqlColumnName;
- PropertyType = typeof(int);
- PropertyRealType = typeof(int);
- SqlColumnName = sqlColumnName;
- ColumnTypeInfo = typeInfo;
- Nullable = false;
- IsPrimaryKey = isPrimaryKey;
- IsAutoIncrement = isAutoIncrement;
- TypeRegistry = typeRegistry;
- IndexType = indexType;
+ _metadata.Add(metadata);
+ ColumnType = typeProvider.Get(clrType);
}
- public ColumnInfo(Type entityType, string entityPropertyName, ColumnTypeRegistry? typeRegistry = null)
+ public ColumnInfo(PropertyInfo propertyInfo, IColumnTypeProvider typeProvider)
{
- if (typeRegistry is null)
+ EntityType = propertyInfo.DeclaringType!;
+ ColumnType = typeProvider.Get(propertyInfo.PropertyType);
+
+ var columnAttribute = propertyInfo.GetCustomAttribute<ColumnAttribute>();
+ if (columnAttribute is not null)
{
- typeRegistry = ColumnTypeRegistry.Instance;
+ _metadata.Add(columnAttribute);
}
+ }
- EntityType = entityType;
- PropertyName = entityPropertyName;
- PropertyInfo = entityType.GetProperty(entityPropertyName);
+ public Type EntityType { get; }
+ // If null, there is no corresponding property.
+ public PropertyInfo? PropertyInfo { get; } = null;
- if (PropertyInfo is null)
- throw new Exception("Public property with given name does not exist.");
+ public IColumnMetadata Metadata => _metadata;
- PropertyType = PropertyInfo.PropertyType;
- PropertyRealType = ExtractRealTypeFromNullable(PropertyType);
+ public IColumnTypeInfo ColumnType { get; }
- var columnAttribute = PropertyInfo.GetCustomAttribute<ColumnAttribute>();
- if (columnAttribute is null)
- {
- SqlColumnName = PropertyName;
- Nullable = true;
- IndexType = ColumnIndexType.None;
- DefaultEmptyForString = false;
- }
- else
+ public string ColumnName
+ {
+ get
{
- SqlColumnName = columnAttribute.DatabaseName ?? PropertyName;
- Nullable = !columnAttribute.NonNullable;
- IndexType = columnAttribute.IndexType;
- DefaultEmptyForString = columnAttribute.DefaultEmptyForString;
+ object? value = Metadata.GetValueOrDefault(ColumnMetadataKeys.ColumnName);
+ Debug.Assert(value is null || value is string);
+ return (string?)value ?? PropertyInfo?.Name ?? throw new Exception("Failed to get column name.");
}
-
- ColumnTypeInfo = typeRegistry.GetRequired(PropertyRealType);
- TypeRegistry = typeRegistry;
}
- public Type EntityType { get; }
- // If null, there is no corresponding property.
- public PropertyInfo? PropertyInfo { get; } = null;
- public string PropertyName { get; }
- public Type PropertyType { get; }
- public Type PropertyRealType { get; }
- public string SqlColumnName { get; }
- public ColumnTypeRegistry TypeRegistry { get; set; }
- public ColumnTypeInfo ColumnTypeInfo { get; }
- public bool Nullable { get; }
- public bool IsPrimaryKey { get; }
- public bool IsAutoIncrement { get; }
- public ColumnIndexType IndexType { get; }
-
- // TODO: Implement this behavior.
- public bool DefaultEmptyForString { get; }
+ public bool IsPrimaryKey => Metadata.GetValueOrDefault(ColumnMetadataKeys.IsPrimaryKey) is true;
+ public bool IsAutoIncrement => Metadata.GetValueOrDefault(ColumnMetadataKeys.IsAutoIncrement) is true;
+ public bool IsNotNull => IsPrimaryKey || Metadata.GetValueOrDefault(ColumnMetadataKeys.NotNull) is true;
- public string SqlType => ColumnTypeInfo.SqlType;
+ public ColumnIndexType Index => Metadata.GetValueOrDefault<ColumnIndexType?>(ColumnMetadataKeys.Index) ?? ColumnIndexType.None;
- public string GenerateCreateTableColumnString()
+ public string GenerateCreateTableColumnString(string? dbProviderId = null)
{
StringBuilder result = new StringBuilder();
- result.Append(SqlColumnName);
+ result.Append(ColumnName);
result.Append(' ');
- result.Append(SqlType);
+ result.Append(ColumnType.GetSqlTypeString(dbProviderId));
if (IsPrimaryKey)
{
result.Append(' ');
result.Append("PRIMARY KEY");
}
- else if (!Nullable)
+ else if (IsNotNull)
{
result.Append(' ');
result.Append(" NOT NULL");