blob: 8b4607d22cec8f09e1d17dbabd06773397bdaf2f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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; }
}
|