public static Tuple <bool, List <string> > IsValidBasedOnSqlColumnAttributes(AdvanceMember member)// where T : Attribute { var tuple = new Tuple <bool, List <string> >(true, new List <string>() { }); if (member.SqlCustomAttritube.Ignore == true) { return(tuple); } if (member.SqlCustomAttritube.Nullable == false) { if (member.Value == null) { tuple.Item2.Add($"The field {member.Member.Name} is mark as a non-nullable field therefore it's required & it can't be null"); } } if (member.SqlCustomAttritube.PrimaryKey == true) { if (member.Value == null) { tuple.Item2.Add($"The field {member.Member.Name} is a primary key therefore it's required & it can't be null"); } else { if (member.Member.Type == typeof(DateTime) && (DateTime)member.Value == DateTime.MinValue) { tuple.Item2.Add($"The field {member.Member.Name} is a primary key therefore it's required & it can't be null"); } } } if (member.SqlCustomAttritube.MaxColumnSize.GetValueOrDefault(0) > 0) { var valueSize = 0; if (member.Value != null) { valueSize = member.Value.ToString().Length; } if (valueSize > member.SqlCustomAttritube.MaxColumnSize.GetValueOrDefault(0)) { tuple.Item2.Add($"The field {member.Member.Name} exceeds the maximum amount of characters ({member.SqlCustomAttritube.MaxColumnSize.GetValueOrDefault(0)})"); } } return(new Tuple <bool, List <string> >(tuple.Item2.Count <= 0, tuple.Item2)); }
public static string AdvanceTypeToSqlType(AdvanceMember advance, DataBaseType sqlType, bool hasMultipleKeys = false) { var str = TypeToSqlType(advance.Member.Type, sqlType); if (advance.Member.Type == typeof(string) && advance.SqlCustomAttritube.PrimaryKey == true) { str = str.Replace("(MAX)", "(900)"); /// SQL SERVER Doesn't allow varchar max to be primary key must be 900 bytes or less } var enumSql = System.Enum.GetValues(typeof(SqlColumnAttritubeMembers)).Cast <SqlColumnAttritubeMembers>().ToList(); var allowIdentity = true; enumSql.ForEach(delegate(SqlColumnAttritubeMembers members) { switch (members) { case SqlColumnAttritubeMembers.SetMaxColumnSize: if (advance.SqlCustomAttritube.MaxColumnSize != null) { // if (sqlType == DataBaseType.Sqlite) str = str.Replace(" (MAX)", $"({advance.SqlCustomAttritube.MaxColumnSize})"); } break; case SqlColumnAttritubeMembers.SetNullable: if (advance.SqlCustomAttritube.Nullable == false) { str += ($" NOT NULL "); } else if (advance.SqlCustomAttritube.Nullable == true) { str += ($" NULL "); } break; case SqlColumnAttritubeMembers.SetAutoIncrementBy: case SqlColumnAttritubeMembers.SetStartIncrementAt: if (sqlType == DataBaseType.Access95) { break; } if (sqlType == DataBaseType.Sqlite) { break; } if (allowIdentity) { if (advance.SqlCustomAttritube.AutoIncrementBy != null || advance.SqlCustomAttritube.StartIncrementAt != null) { str += $" IDENTITY({advance.SqlCustomAttritube.StartIncrementAt.GetValueOrDefault(1)},{advance.SqlCustomAttritube.AutoIncrementBy.GetValueOrDefault(1)})"; } } allowIdentity = false; break; case SqlColumnAttritubeMembers.SetUtcDateTime: break; case SqlColumnAttritubeMembers.SetPrimaryKey: if (sqlType == DataBaseType.Access95) { break; } if (advance.SqlCustomAttritube.PrimaryKey != null && advance.SqlCustomAttritube.PrimaryKey == true && !hasMultipleKeys) { str += ($" PRIMARY KEY "); } break; case SqlColumnAttritubeMembers.SetApiId: break; case SqlColumnAttritubeMembers.SetSyncTime: break; case SqlColumnAttritubeMembers.SetIgnore: break; case SqlColumnAttritubeMembers.MapTo: break; case SqlColumnAttritubeMembers.DefaultValue: break; case SqlColumnAttritubeMembers.TSQLDefaultValue: if (!string.IsNullOrEmpty(advance.SqlCustomAttritube.TSQLDefaultValue)) { str += ($" DEFAULT {advance.SqlCustomAttritube.TSQLDefaultValue} "); } break; case SqlColumnAttritubeMembers.SetxRefTableType: break; case SqlColumnAttritubeMembers.xRefTableSchema: break; case SqlColumnAttritubeMembers.xRefTableName: if (!string.IsNullOrEmpty(advance.SqlCustomAttritube.xRefTableName) && !string.IsNullOrEmpty(advance.SqlCustomAttritube.xRefJoinOnColumn)) { str += ($" FOREIGN KEY REFERENCES {advance.SqlCustomAttritube.xRefTableName}({advance.SqlCustomAttritube.xRefJoinOnColumn}) "); if (advance.SqlCustomAttritube.xRefOnDeleteCascade.GetValueOrDefault(false)) { str += ($" ON DELETE CASCADE "); } if (advance.SqlCustomAttritube.xRefOnUpdateCascade.GetValueOrDefault(false)) { str += ($" ON UPDATE CASCADE "); } } break; case SqlColumnAttritubeMembers.xRefJoinOnColumn: break; case SqlColumnAttritubeMembers.SetxRefOnUpdateCascade: break; case SqlColumnAttritubeMembers.SetxRefOnDeleteCascade: break; case SqlColumnAttritubeMembers.MappingIds: // WILL NEVER DO ANYTHING THIS IS FOR JOIN PURPOSE ONLY break; case SqlColumnAttritubeMembers.SerializableType: break; default: throw new ArgumentOutOfRangeException(nameof(members), members, null); } }); return(str); }