From b2a7d7e0f667e8a276ef307a6988e42eee7feb5d Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 11 Dec 2022 12:42:21 +0800 Subject: Develop secret api. v28 --- .../CrupestApi.Commons/Crud/ColumnInfo.cs | 3 ++- .../CrupestApi/CrupestApi.Commons/Crud/ParamMap.cs | 2 +- .../CrupestApi.Commons/Crud/TableInfo.cs | 27 +++++++--------------- 3 files changed, 11 insertions(+), 21 deletions(-) (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons') diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs index 4a594bb..9a6b549 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs @@ -18,10 +18,11 @@ public class ColumnHooks /// Called after SELECT. Please use multicast if you want to customize it because there are many default behavior in it.Called before INSERT. Please use multicast if you want to customize it because there are many default behavior in it.Called before INSERT. Please use multicast if you want to customize it because there are many default behavior in it. public ColumnHookAction BeforeInsert; /// Called before UPDATE. Please use multicast if you want to customize it because there are many default behavior in it.Set value to null to delete the update item so it will not change. Set value to to update the column to NULL. public ColumnHookAction BeforeUpdate; } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ParamMap.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ParamMap.cs index fe8d2a0..841ba40 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ParamMap.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ParamMap.cs @@ -4,7 +4,7 @@ using System.Diagnostics; namespace CrupestApi.Commons.Crud; /// -/// Null value will be thrown. Please use . +/// is an optional column name related to the param. You may use it to do some column related things. Like use a more accurate conversion. /// public record ParamInfo(string Name, object? Value, string? ColumnName = null); diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index d2f48c6..61cd04f 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -367,12 +367,7 @@ CREATE TABLE {tableName}( var result = new DynamicParameters(); foreach (var param in parameters) { - if (param.Value is null) - { - continue; - } - - if (param.Value is DbNullValue) + if (param.Value is null || param.Value is DbNullValue) { result.Add(param.Name, null); continue; @@ -455,17 +450,14 @@ CREATE TABLE {tableName}( { InsertItem? item = insert.Items.FirstOrDefault(i => i.ColumnName == column.ColumnName); object? value = null; - if (item is null || item.Value is null) - { - column.Hooks.BeforeInsert(column, , ); - } if (item is null) { + column.Hooks.BeforeInsert(column, ref value, false); item = new InsertItem(column.ColumnName, value); - insert.Items.Add(item); } else { + column.Hooks.BeforeInsert(column, ref value, true); item.Value = value; } @@ -490,19 +482,16 @@ CREATE TABLE {tableName}( { var (sql, parameters) = GenerateUpdateSql(where, update); + var readUpdateClause = UpdateClause.Create(); + foreach (var column in ColumnInfos) { UpdateItem? item = update.Items.FirstOrDefault(i => i.ColumnName == column.ColumnName); var value = item?.Value; - column.Hooks.BeforeUpdate(column, ref value); - if (item is null) - { - if (value is not null) - update.Items.Add(new UpdateItem(column.ColumnName, value)); - } - else + column.Hooks.BeforeUpdate(column, ref value, item is null ? false : true); + if (value is not null) { - item.Value = value; + readUpdateClause.Add(column.ColumnName, value); } } -- cgit v1.2.3