private void InitLogicalDeleted() { var statusProperty = SqlProperties.FirstOrDefault(x => x.PropertyInfo.GetCustomAttribute <StatusAttribute>() != null); foreach (var property in AllProperties) { var joinAttr = property.GetCustomAttribute <JoinAttributeBase>(); if (joinAttr?.TableName == null) { continue; } //var deleted = joinProperty.JoinPropertyInfo.PropertyType.GetCustomAttribute<DeletedAttribute>(); var deleteAttr = property.PropertyType.GetProperties().FirstOrDefault(x => x.GetCustomAttribute <DeletedAttribute>() != null); if (deleteAttr == null) { continue; } if (JoinsLogicalDelete == null) { JoinsLogicalDelete = new Dictionary <string, PropertyInfo>(); } JoinsLogicalDelete.Add(joinAttr.TableName, deleteAttr); } if (statusProperty == null) { return; } StatusPropertyName = statusProperty.ColumnName; if (statusProperty.PropertyInfo.PropertyType == typeof(bool)) { LogicalDelete = true; LogicalDeleteValue = 1; // true } else if (statusProperty.PropertyInfo.PropertyType.IsEnum) { var deleteOption = statusProperty.PropertyInfo.PropertyType.GetFields().FirstOrDefault(f => f.GetCustomAttribute <DeletedAttribute>() != null); if (deleteOption == null) { return; } var enumValue = Enum.Parse(statusProperty.PropertyInfo.PropertyType, deleteOption.Name); LogicalDeleteValue = Convert.ChangeType(enumValue, Enum.GetUnderlyingType(statusProperty.PropertyInfo.PropertyType)); LogicalDelete = true; } }
private void AppendJoinQuery(JoinAttributeBase attrJoin, StringBuilder joinBuilder, string tableName) { var joinString = attrJoin.ToString(); var joinAs = Provider == SqlProvider.Oracle ? " " : " AS "; if (attrJoin is CrossJoinAttribute) { joinBuilder.Append(attrJoin.TableAlias == string.Empty ? $"{joinString} {attrJoin.TableName} " : $"{joinString} {attrJoin.TableName}{joinAs}{attrJoin.TableAlias} "); } else if (attrJoin.TableName != null) { var customFilter = string.Empty; if (JoinsLogicalDelete != null && JoinsLogicalDelete.TryGetValue(attrJoin.TableName, out var deleteAttr)) { var colAttr = deleteAttr.GetCustomAttribute <ColumnAttribute>(); var colName = colAttr == null ? deleteAttr.Name : colAttr.Name; object deleteValue = 1; if (deleteAttr.PropertyType.IsEnum) { var deleteOption = deleteAttr.PropertyType.GetFields().FirstOrDefault(f => f.GetCustomAttribute <DeletedAttribute>() != null); if (deleteOption != null) { var enumValue = Enum.Parse(deleteAttr.PropertyType, deleteOption.Name); deleteValue = Convert.ChangeType(enumValue, Enum.GetUnderlyingType(deleteAttr.PropertyType)); } } customFilter = attrJoin.TableAlias == string.Empty ? $"AND {attrJoin.TableName}.{colName} != {deleteValue} " : $"AND {attrJoin.TableAlias}.{colName} != {deleteValue} "; } joinBuilder.Append(attrJoin.TableAlias == string.Empty ? $"{joinString} {attrJoin.TableName} ON {tableName}.{attrJoin.Key} = {attrJoin.TableName}.{attrJoin.ExternalKey} {customFilter}" : $"{joinString} {attrJoin.TableName}{joinAs}{attrJoin.TableAlias} ON {tableName}.{attrJoin.Key} = {attrJoin.TableAlias}.{attrJoin.ExternalKey} {customFilter}"); } }