diff options
Diffstat (limited to 'docker')
3 files changed, 71 insertions, 52 deletions
| diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs index 9a6b549..37ae971 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs @@ -6,7 +6,11 @@ namespace CrupestApi.Commons.Crud;  public class ColumnHooks  { -    public delegate void ColumnHookAction(ColumnInfo column, ref object? value, bool specified); +    // 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)      { @@ -131,46 +135,50 @@ public class ColumnInfo      private void TryCoerceStringFromNullToEmpty(ref object? value)      { -        if (ColumnType.ClrType == typeof(string) && (Metadata.GetValueOrDefault<bool?>(ColumnMetadataKeys.DefaultEmptyForString) is true) && (value is null || value is DbNullValue)) +        if (ColumnType.ClrType == typeof(string) && (Metadata.GetValueOrDefault<bool?>(ColumnMetadataKeys.DefaultEmptyForString) is true) && value is DbNullValue)          {              value = "";          }      } -    protected void OnAfterSelect(ColumnInfo column, ref object? value, bool specified) +    protected void OnAfterSelect(ColumnInfo column, ref object? value)      {          TryCoerceStringFromNullToEmpty(ref value);      } -    protected void OnBeforeInsert(ColumnInfo column, ref object? value, bool specified) +    protected void OnBeforeInsert(ColumnInfo column, ref object? value)      {          if (column.IsClientGenerate && value is not null)          { -            throw new UserException($"'{column.ColumnName}' can't be set manually. It is auto generated."); +            throw new Exception($"'{column.ColumnName}' can't be set manually. It is auto generated.");          } -        DefaultValueGeneratorMethod?.Invoke(null, new object[] { }); +        var defaultValueGenerator = DefaultValueGeneratorMethod; +        if (defaultValueGenerator is not null && value is null) +        { +            value = defaultValueGenerator.Invoke(null, null); +        } -        OnBeforeSet(column, ref value); -    } +        TryCoerceStringFromNullToEmpty(ref value); -    protected void OnBeforeUpdate(ColumnInfo column, ref object? value, bool specified) -    { -        if (column.IsNoUpdate) +        if (IsNotNull && (value is null || value is DbNullValue))          { -            throw new UserException($"'{column.ColumnName}' is not updatable."); +            throw new Exception($"'{column.ColumnName}' can't be null.");          } - -        OnBeforeSet(column, ref value);      } -    protected void OnBeforeSet(ColumnInfo column, ref object? value) +    protected void OnBeforeUpdate(ColumnInfo column, ref object? value)      { +        if (column.IsNoUpdate && value is not null) +        { +            throw new Exception($"'{column.ColumnName}' is not updatable."); +        } +          TryCoerceStringFromNullToEmpty(ref value); -        if ((value is null || value is DbNullValue) && column.IsNotNull) +        if (IsNotNull && value is DbNullValue)          { -            throw new UserException($"{column.ColumnName} can't be null."); +            throw new Exception($"'{column.ColumnName}' can't be null.");          }      } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 53424a4..f0af62a 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -60,12 +60,6 @@ public class CrudService<TEntity> : IDisposable where TEntity : class          return _table.Insert(_dbConnection, insertClause);      } -    // Return the key. TODO: Continue here. -    public object Insert(TEntity entity) -    { -        return _table.Insert(_dbConnection, ); -    } -      public int Update(IUpdateClause updateClause, IWhereClause? filter)      {          return _table.Update(_dbConnection, filter, updateClause); @@ -90,10 +84,4 @@ public class CrudService<TEntity> : IDisposable where TEntity : class      {          return SelectAsJson(WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)).Single();      } - -    public object InsertFromJson(JsonDocument? json) -    { -        // TODO: Implement this. -        throw new NotImplementedException(); -    }  } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index 64ea505..58a4396 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -425,19 +425,24 @@ CREATE TABLE {tableName}(                  var dynamicProperty = dynamicType.GetProperty(column.ColumnName);                  if (dynamicProperty is null)                  { -                    column.Hooks.AfterSelect(column, ref value, false); +                    column.Hooks.AfterSelect(column, ref value);                  }                  else                  {                      value = dynamicProperty.GetValue(d); -                    if (value is not null) +                    if (value is null || value is DbNullValue) +                        value = DbNullValue.Instance; +                    else                          value = column.ColumnType.ConvertFromDatabase(value); -                    column.Hooks.AfterSelect(column, ref value, true); +                    column.Hooks.AfterSelect(column, ref value);                  } -                if (dynamicProperty is not null) +                if (dynamicProperty is not null && value is not null)                  { -                    dynamicProperty.SetValue(d, value); +                    if (value is DbNullValue) +                        dynamicProperty.SetValue(d, null); +                    else +                        dynamicProperty.SetValue(d, value);                  }              } @@ -490,22 +495,34 @@ CREATE TABLE {tableName}(      public object Insert(IDbConnection dbConnection, IInsertClause insert)      {          object? key = null; + +        var realInsert = InsertClause.Create(); +          foreach (var column in ColumnInfos)          {              InsertItem? item = insert.Items.SingleOrDefault(i => i.ColumnName == column.ColumnName); -            object? value = null;              if (item is null)              { -                column.Hooks.BeforeInsert(column, ref value, false); -                item = new InsertItem(column.ColumnName, value); +                object? value = null; +                column.Hooks.BeforeInsert(column, ref value); +                if (value is not null) +                    if (value is DbNullValue) +                        realInsert.Add(column.ColumnName, null); +                    else +                        realInsert.Add(column.ColumnName, value);              }              else              { -                column.Hooks.BeforeInsert(column, ref value, true); -                item.Value = value; +                object? value = item.Value ?? DbNullValue.Instance; +                column.Hooks.BeforeInsert(column, ref value); +                if (value is not null) +                    if (value is DbNullValue) +                        realInsert.Add(column.ColumnName, null); +                    else +                        realInsert.Add(column.ColumnName, value);              } -            if (item.ColumnName == KeyColumn.ColumnName) +            if (item?.ColumnName == KeyColumn.ColumnName)              {                  key = item.Value;              } @@ -530,19 +547,25 @@ CREATE TABLE {tableName}(          {              UpdateItem? item = update.Items.FirstOrDefault(i => i.ColumnName == column.ColumnName); -            object? value = item?.Value; -            column.Hooks.BeforeUpdate(column, ref value, false); - -            if (value is not null) +            if (item is null)              { -                if (value is DbNullValue) -                { -                    realUpdate.Add(column.ColumnName, null); -                } -                else -                { -                    realUpdate.Add(column.ColumnName, value); -                } +                object? value = null; +                column.Hooks.BeforeUpdate(column, ref value); +                if (value is not null) +                    if (value is DbNullValue) +                        realUpdate.Add(column.ColumnName, null); +                    else +                        realUpdate.Add(column.ColumnName, value); +            } +            else +            { +                object? value = item.Value ?? DbNullValue.Instance; +                column.Hooks.BeforeUpdate(column, ref value); +                if (value is not null) +                    if (value is DbNullValue) +                        realUpdate.Add(column.ColumnName, null); +                    else +                        realUpdate.Add(column.ColumnName, value);              }          } | 
