aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-05 13:50:52 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commit9f7162f12944cc722dba9e91f4d30aa312142d49 (patch)
treeb6e68cfecdfcdb25b240277d667d26eb1d1b6806 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
parent2d63ac857ed1082f6f7d365674aa734c582a99dd (diff)
downloadcrupest-9f7162f12944cc722dba9e91f4d30aa312142d49.tar.gz
crupest-9f7162f12944cc722dba9e91f4d30aa312142d49.tar.bz2
crupest-9f7162f12944cc722dba9e91f4d30aa312142d49.zip
Develop secret api. v7
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs69
1 files changed, 69 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
new file mode 100644
index 0000000..8b4607d
--- /dev/null
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
@@ -0,0 +1,69 @@
+using System.Reflection;
+
+namespace CrupestApi.Commons.Crud;
+
+public class ColumnInfo
+{
+ private Type ExtractRealTypeFromNullable(Type type)
+ {
+ if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
+ {
+ return type.GetGenericArguments()[0];
+ }
+
+ return type;
+ }
+
+ // A column with no property.
+ public ColumnInfo(Type entityType, string sqlColumnName, bool isPrimaryKey, bool isAutoIncrement, IColumnTypeInfo typeInfo)
+ {
+ EntityType = entityType;
+ PropertyName = null;
+ PropertyType = typeof(int);
+ PropertyRealType = typeof(int);
+ SqlColumnName = sqlColumnName;
+ ColumnTypeInfo = typeInfo;
+ Nullable = false;
+ IsPrimaryKey = isPrimaryKey;
+ IsAutoIncrement = isAutoIncrement;
+ }
+
+ public ColumnInfo(Type entityType, string entityPropertyName)
+ {
+ EntityType = entityType;
+ PropertyName = entityPropertyName;
+
+ var property = entityType.GetProperty(entityPropertyName);
+
+ if (property is null)
+ throw new Exception("Public property with given name does not exist.");
+
+ PropertyType = property.PropertyType;
+ PropertyRealType = ExtractRealTypeFromNullable(PropertyType);
+
+ var columnAttribute = property.GetCustomAttribute<ColumnAttribute>();
+ if (columnAttribute is null)
+ {
+ SqlColumnName = PropertyName;
+ Nullable = true;
+ }
+ else
+ {
+ SqlColumnName = columnAttribute.DatabaseName ?? PropertyName;
+ Nullable = !columnAttribute.NonNullable;
+ }
+ ColumnTypeInfo = ColumnTypeInfoRegistry.Singleton.GetRequiredByDataType(PropertyRealType);
+
+ }
+
+ public Type EntityType { get; }
+ // If null, there is no corresponding property.
+ public string? PropertyName { get; }
+ public Type PropertyType { get; }
+ public Type PropertyRealType { get; }
+ public string SqlColumnName { get; }
+ public IColumnTypeInfo ColumnTypeInfo { get; }
+ public bool Nullable { get; }
+ public bool IsPrimaryKey { get; }
+ public bool IsAutoIncrement { get; }
+} \ No newline at end of file