aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-13 16:44:25 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:53 +0800
commit90aca5d4ca4aabdf7b1bfcbca1ddf1305bd2afab (patch)
treeb646eaa164b4b2fc5f712ceddbb92b2b67865295 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
parent72f0786810ec3afc7a7e42fdeb392717eeb1055b (diff)
downloadcrupest-90aca5d4ca4aabdf7b1bfcbca1ddf1305bd2afab.tar.gz
crupest-90aca5d4ca4aabdf7b1bfcbca1ddf1305bd2afab.tar.bz2
crupest-90aca5d4ca4aabdf7b1bfcbca1ddf1305bd2afab.zip
Develop secret api. v36
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs125
1 files changed, 11 insertions, 114 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
index 0ac6e7b..e91c777 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
@@ -4,62 +4,6 @@ using System.Text;
namespace CrupestApi.Commons.Crud;
-public class ColumnHooks
-{
- // value:
- // null => not specified
- // DbNullValue => specified as NULL
- // other => specified as value
- public delegate void ColumnHookAction(ColumnInfo column, ref object? value);
-
- public ColumnHooks(ColumnHookAction afterSelect, ColumnHookAction beforeInsert, ColumnHookAction beforeUpdate)
- {
- AfterSelect = afterSelect;
- BeforeInsert = beforeInsert;
- BeforeUpdate = beforeUpdate;
- }
-
- /// <summary>Called after SELECT. Please use multicast if you want to customize it because there are many default behavior in it.</summary>
- /// <remarks>
- /// Called after column type transformation.
- /// value(in):
- /// null => not found in SELECT result
- /// DbNullValue => database NULL
- /// others => database value
- /// value(out):
- /// null/DbNullValue => return null
- /// others => return as is
- /// </remarks>
- public ColumnHookAction AfterSelect;
-
- /// <summary>Called before INSERT. Please use multicast if you want to customize it because there are many default behavior in it.</summary>
- /// <remarks>
- /// Called before column type transformation.
- /// value(in):
- /// null => not specified by insert clause
- /// DbNullValue => specified as database NULL
- /// other => specified as other value
- /// value(out):
- /// null/DbNullValue => save database NULL
- /// other => save the value as is
- /// </remarks>
- public ColumnHookAction BeforeInsert;
-
- /// <summary>Called before UPDATE. Please use multicast if you want to customize it because there are many default behavior in it.</summary
- /// <remarks>
- /// Called before column type transformation.
- /// value(in):
- /// null => not specified by update clause
- /// DbNullValue => specified as database NULL
- /// other => specified as other value
- /// value(out):
- /// null => not update
- /// DbNullValue => update to database NULL
- /// other => update to the value
- /// </remarks>
- public ColumnHookAction BeforeUpdate;
-}
-
public class ColumnInfo
{
private readonly AggregateColumnMetadata _metadata = new AggregateColumnMetadata();
@@ -72,12 +16,6 @@ public class ColumnInfo
Table = table;
_metadata.Add(metadata);
ColumnType = typeProvider.Get(clrType);
-
- Hooks = new ColumnHooks(
- new ColumnHooks.ColumnHookAction(OnAfterSelect),
- new ColumnHooks.ColumnHookAction(OnBeforeInsert),
- new ColumnHooks.ColumnHookAction(OnBeforeUpdate)
- );
}
/// <summary>
@@ -94,16 +32,12 @@ public class ColumnInfo
{
_metadata.Add(columnAttribute);
}
-
- Hooks = new ColumnHooks(
- new ColumnHooks.ColumnHookAction(OnAfterSelect),
- new ColumnHooks.ColumnHookAction(OnBeforeInsert),
- new ColumnHooks.ColumnHookAction(OnBeforeUpdate)
- );
}
public TableInfo Table { get; }
+ public Type EntityType => Table.EntityType;
+
// If null, there is no corresponding property.
public PropertyInfo? PropertyInfo { get; } = null;
@@ -111,8 +45,6 @@ public class ColumnInfo
public IColumnTypeInfo ColumnType { get; }
- public ColumnHooks Hooks { 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;
@@ -163,35 +95,31 @@ public class ColumnInfo
}
}
- public MethodInfo ValidatorMethod
+ public MethodInfo? ValidatorMethod
{
get
{
- object? value = Metadata.GetValueOrDefault(ColumnMetadataKeys.Validator);
+ object? value = Metadata.GetValueOrDefault(ColumnMetadataKeys.DefaultValueGenerator);
Debug.Assert(value is null || value is string);
+ MethodInfo? result;
if (value is null)
{
- return GetType().GetMethod(nameof(DefaultValidator))!;
+ string methodName = ColumnName + "Validator";
+ result = Table.EntityType.GetMethod(methodName, BindingFlags.Static);
}
else
{
string methodName = (string)value;
- return Table.EntityType.GetMethod(methodName, BindingFlags.Static) ?? throw new Exception("The validator does not exist.");
+ result = Table.EntityType.GetMethod(methodName, BindingFlags.Static) ?? throw new Exception("The validator does not exist.");
}
+
+ return result;
}
}
public void InvokeValidator(object value)
{
- ValidatorMethod.Invoke(null, new object?[] { this, value });
- }
-
- public static void DefaultValidator(ColumnInfo column, object value)
- {
- if (column.IsNotNull && value is DbNullValue)
- {
- throw new Exception("The column can't be null.");
- }
+ ValidatorMethod?.Invoke(null, new object?[] { this, value });
}
public object? InvokeDefaultValueGenerator()
@@ -199,37 +127,6 @@ public class ColumnInfo
return DefaultValueGeneratorMethod?.Invoke(null, new object?[] { this });
}
- public static object? DefaultDefaultValueGenerator(ColumnInfo column)
- {
- return DbNullValue.Instance;
- }
-
- private void TryCoerceStringFromNullToEmpty(ref object? value)
- {
- if (ColumnType.ClrType == typeof(string) && (Metadata.GetValueOrDefault<bool?>(ColumnMetadataKeys.DefaultEmptyForString) is true) && value is DbNullValue)
- {
- value = "";
- }
- }
-
- protected void OnAfterSelect(ColumnInfo column, ref object? value)
- {
- TryCoerceStringFromNullToEmpty(ref value);
- }
-
- protected void OnBeforeInsert(ColumnInfo column, ref object? value)
- {
- TryCoerceStringFromNullToEmpty(ref value);
- }
-
- protected void OnBeforeUpdate(ColumnInfo column, ref object? value)
- {
- if (IsNoUpdate && value is not null)
- throw new Exception("The column can't be updated.");
-
- TryCoerceStringFromNullToEmpty(ref value);
- }
-
public string GenerateCreateTableColumnString(string? dbProviderId = null)
{
StringBuilder result = new StringBuilder();