protected virtual List<string> GetSqlDbTypeParts(DiffColumn col, Type type) { List<string> parts = new List<string>(); var pair = CurrentSchema.Settings.GetSqlDbTypePair(type); if (pair.SqlDbType != col.SqlDbType) parts.Add("SqlDbType = SqlDbType." + col.SqlDbType); var defaultSize = CurrentSchema.Settings.GetSqlSize(null, pair.SqlDbType); if (defaultSize != null) { if (!(defaultSize == col.Precission || defaultSize == col.Length / DiffColumn.BytesPerChar(col.SqlDbType) || defaultSize == int.MaxValue && col.Length == -1)) parts.Add("Size = " + (col.Length == -1 ? "int.MaxValue" : col.Length != 0 ? (col.Length / DiffColumn.BytesPerChar(col.SqlDbType)).ToString() : col.Precission != 0 ? col.Precission.ToString() : "0")); } var defaultScale = CurrentSchema.Settings.GetSqlScale(null, col.SqlDbType); if (defaultScale != null) { if (!(col.Scale == defaultScale)) parts.Add("Scale = " + col.Scale); } if (col.Default != null) parts.Add("Default = \"" + CleanDefault(col.Default) + "\""); return parts; }
protected virtual string GetRelatedEntity(DiffTable table, DiffColumn col) { if (col.ForeignKey == null) return null; return GetEntityName(col.ForeignKey.TargetTable); }
protected virtual IEnumerable <string> GetFieldAttributes(DiffTable table, DiffColumn col, string relatedEntity, bool isMList) { List <string> attributes = new List <string>(); if (RequiresNotNullableAttribute(col, relatedEntity) && NotNullAttributeNecessary(table, col, isMList)) { attributes.Add("NotNullable"); } if (col.ForeignKey == null) { string sqlDbType = GetSqlTypeAttribute(table, col); if (sqlDbType != null) { attributes.Add(sqlDbType); } } if (RequiresColumnName(table, col)) { attributes.Add("ColumnName(\"" + col.Name + "\")"); } if (HasUniqueIndex(table, col)) { attributes.Add("UniqueIndex"); } return(attributes); }
protected virtual string GetBackColumnNameAttribute(DiffColumn backReference) { if (backReference.Name == "ParentID") return null; return "BackReferenceColumnName(\"{0}\")".FormatWith(backReference.Name); }
protected virtual string GetStringLengthValidator(DiffTable table, DiffColumn col, string relatedEntity) { if (GetValueType(col) != typeof(string)) { return(null); } var parts = new List <string>(); parts.Add("AllowNulls = " + col.Nullable.ToString().ToLower()); var min = GetMinStringLength(col); if (min != null) { parts.Add("Min = " + min); } if (col.Length != -1) { parts.Add("Max = " + col.Length / DiffColumn.BytesPerChar(col.SqlDbType)); } return("StringLengthValidator(" + parts.ToString(", ") + ")"); }
protected virtual string GetPrimaryKeyAttribute(DiffTable table) { DiffColumn primaryKey = GetPrimaryKeyColumn(table); if (primaryKey == null) return null; var def = CurrentSchema.Settings.DefaultPrimaryKeyAttribute; Type type = GetValueType(primaryKey); List<string> parts = new List<string>(); if (primaryKey.Name != def.Name) parts.Add("Name = \"" + primaryKey.Name + "\""); if (primaryKey.Identity != def.Identity) { parts.Add("Identity = " + primaryKey.Identity.ToString().ToLower()); parts.Add("IdentityBehaviour = " + primaryKey.Identity.ToString().ToLower()); } parts.AddRange(GetSqlDbTypeParts(primaryKey, type)); if (type != def.Type || parts.Any()) parts.Insert(0, "typeof(" + type.TypeName() + ")"); if(parts.Any()) return "PrimaryKey(" + parts.ToString(", ") + ")"; return null; }
protected virtual bool HasUniqueIndex(DiffTable table, DiffColumn col) { return(table.Indices.Values.Any(ix => ix.FilterDefinition == null && ix.Columns.Only()?.Let(ic => ic.ColumnName == col.Name && ic.IsIncluded == false) == true && ix.IsUnique && ix.Type == DiffIndexType.NonClustered)); }
protected virtual string DefaultColumnName(DiffTable table, DiffColumn col) { string fieldName = GetFieldName(table, col).FirstUpper(); if (col.ForeignKey == null) return fieldName; return fieldName + "ID"; }
protected virtual string GetRelatedEntity(DiffTable table, DiffColumn col) { if (col.ForeignKey == null) { return(null); } return(GetEntityName(Tables.GetOrThrow(col.ForeignKey.TargetTable))); }
protected virtual string GetSqlTypeAttribute(DiffTable table, DiffColumn col) { Type type = GetValueType(col); List<string> parts = GetSqlDbTypeParts(col, type); if (parts.Any()) return "SqlDbType(" + parts.ToString(", ") + ")"; return null; }
protected virtual string GetSqlTypeAttribute(DiffTable table, DiffColumn col) { Type type = GetValueType(col); List <string> parts = GetSqlDbTypeParts(col, type); if (parts.Any() && SqlTypeAttributeNecessary(parts, table, col)) { return("SqlDbType(" + parts.ToString(", ") + ")"); } return(null); }
protected virtual IEnumerable<string> GetPropertyAttributes(DiffTable table, DiffColumn col, string relatedEntity) { List<string> attributes = new List<string>(); if (HasNotNullableAttribute(col, relatedEntity) && GetValueType(col) != typeof(string)) attributes.Add("NotNullValidator"); string stringLengthValidator = GetStringLengthValidator(table, col, relatedEntity); if(stringLengthValidator != null) attributes.Add(stringLengthValidator); return attributes; }
protected virtual IEnumerable <string> GetPropertyAttributes(DiffTable table, DiffColumn col, string?relatedEntity) { List <string> attributes = new List <string>(); string?stringLengthValidator = GetStringLengthValidator(table, col, relatedEntity); if (stringLengthValidator != null) { attributes.Add(stringLengthValidator); } return(attributes); }
protected virtual List <string> GetSqlDbTypeParts(DiffColumn col, Type type) { List <string> parts = new List <string>(); var pair = CurrentSchema.Settings.GetSqlDbTypePair(type); if (pair.DbType.SqlServer != col.DbType.SqlServer) { parts.Add("SqlDbType = SqlDbType." + col.DbType.SqlServer); } var defaultSize = CurrentSchema.Settings.GetSqlSize(null, null, pair.DbType); if (defaultSize != null) { if (!(defaultSize == col.Length || defaultSize == int.MaxValue && col.Length == -1)) { parts.Add("Size = " + (col.Length == -1 ? "int.MaxValue" : col.Length != 0 ? col.Length.ToString() : col.Precision != 0 ? col.Precision.ToString() : "0")); } } var defaultPrecision = CurrentSchema.Settings.GetSqlPrecision(null, null, pair.DbType); if (defaultPrecision != null) { if (defaultPrecision != col.Precision) { parts.Add("Precision = " + (col.Precision != 0 ? col.Precision.ToString() : "0")); } } var defaultScale = CurrentSchema.Settings.GetSqlScale(null, null, col.DbType); if (defaultScale != null) { if (!(col.Scale == defaultScale)) { parts.Add("Scale = " + col.Scale); } } if (col.DefaultConstraint != null) { parts.Add("Default = \"" + CleanDefault(col.DefaultConstraint.Definition) + "\""); } return(parts); }
protected virtual string GetFieldName(DiffTable table, DiffColumn col) { string name = col.Name.Contains(' ') ? col.Name.ToPascal(false) : col.Name; if (this.GetRelatedEntity(table, col) != null) { if (name.Length > 2 && name.EndsWith("Id", StringComparison.InvariantCultureIgnoreCase)) name = name.RemoveEnd("Id".Length); if (name.Length > 2 && name.StartsWith("Id", StringComparison.InvariantCultureIgnoreCase)) name = name.RemoveStart("Id".Length); } return name.FirstLower(); }
protected virtual string GetFieldType(DiffTable table, DiffColumn col, string?relatedEntity) { var nullable = (col.Nullable ? "?" : ""); if (relatedEntity != null) { if (IsEnum(Tables.GetOrThrow(col.ForeignKey !.TargetTable))) { return(relatedEntity + nullable); } return((IsLite(table, col) ? "Lite<" + relatedEntity + ">" : relatedEntity) + nullable); } return(GetValueType(col).TypeName() + nullable); }
protected virtual string WriteField(string fileName, DiffTable table, DiffColumn col) { string relatedEntity = GetRelatedEntity(table, col); string type = GetFieldType(table, col, relatedEntity); string fieldName = GetFieldName(table, col); StringBuilder sb = new StringBuilder(); WriteAttributeTag(sb, GetFieldAttributes(table, col, relatedEntity, false)); WriteAttributeTag(sb, GetPropertyAttributes(table, col, relatedEntity)); sb.AppendLine("public {0} {1} {{ get; {2}set; }}".FormatWith(type, fieldName.FirstUpper(), IsReadonly(table, col) ? "private" : null)); return(sb.ToString()); }
protected virtual string GetFieldType(DiffTable table, DiffColumn col, string relatedEntity) { if (relatedEntity != null) { if (IsEnum(col.ForeignKey.TargetTable)) return col.Nullable ? relatedEntity + "?" : relatedEntity; return IsLite(table, col) ? "Lite<" + relatedEntity + ">" : relatedEntity; } var valueType = GetValueType(col); if (col.Nullable) return valueType.Nullify().TypeName(); return valueType.TypeName(); }
protected virtual string GetFieldName(DiffTable table, DiffColumn col) { string name = !IdentifierValidatorAttribute.PascalAscii.IsMatch(col.Name) || col.Name.Contains("_") ? col.Name.ToPascal(false, false) : col.Name; if (this.GetRelatedEntity(table, col) != null) { if (name.Length > 2 && name.EndsWith("Id", StringComparison.InvariantCultureIgnoreCase)) { name = name.RemoveEnd("Id".Length); } if (name.Length > 2 && name.StartsWith("Id", StringComparison.InvariantCultureIgnoreCase)) { name = name.RemoveStart("Id".Length); } } return(name.FirstLower()); }
protected virtual string GetFieldType(DiffTable table, DiffColumn col, string?relatedEntity) { if (relatedEntity != null) { if (IsEnum(Tables.GetOrThrow(col.ForeignKey !.TargetTable))) { return(col.Nullable ? relatedEntity + "?" : relatedEntity); } return(IsLite(table, col) ? "Lite<" + relatedEntity + ">" : relatedEntity); } var valueType = GetValueType(col); if (col.Nullable) { return(valueType.Nullify().TypeName()); } return(valueType.TypeName()); }
protected virtual Type GetValueType(DiffColumn col) { switch (col.SqlDbType) { case SqlDbType.BigInt: return typeof(long); case SqlDbType.Binary: return typeof(byte[]); case SqlDbType.Bit: return typeof(bool); case SqlDbType.Char: return typeof(char); case SqlDbType.Date: return typeof(DateTime); case SqlDbType.DateTime: return typeof(DateTime); case SqlDbType.DateTime2: return typeof(DateTime); case SqlDbType.DateTimeOffset: return typeof(DateTimeOffset); case SqlDbType.Decimal: return typeof(Decimal); case SqlDbType.Float: return typeof(double); case SqlDbType.Image: return typeof(byte[]); case SqlDbType.Int: return typeof(int); case SqlDbType.Money: return typeof(decimal); case SqlDbType.NChar: return typeof(string); case SqlDbType.NText: return typeof(string); case SqlDbType.NVarChar: return typeof(string); case SqlDbType.Real: return typeof(float); case SqlDbType.SmallDateTime: return typeof(DateTime); case SqlDbType.SmallInt: return typeof(short); case SqlDbType.SmallMoney: return typeof(decimal); case SqlDbType.Text: return typeof(string); case SqlDbType.Time: return typeof(TimeSpan); case SqlDbType.Timestamp: return typeof(TimeSpan); case SqlDbType.TinyInt: return typeof(byte); case SqlDbType.UniqueIdentifier: return typeof(Guid); case SqlDbType.VarBinary: return typeof(byte[]); case SqlDbType.VarChar: return typeof(string); case SqlDbType.Xml: return typeof(string); case SqlDbType.Udt: return Schema.Current.Settings.UdtSqlName .SingleOrDefaultEx(kvp => StringComparer.InvariantCultureIgnoreCase.Equals(kvp.Value, col.UserTypeName)) .Key; default: throw new NotImplementedException("Unknown translation for " + col.SqlDbType); } }
protected virtual IEnumerable<string> GetFieldAttributes(DiffTable table, DiffColumn col, string relatedEntity) { List<string> attributes = new List<string>(); if (HasNotNullableAttribute(col, relatedEntity)) attributes.Add("NotNullable"); if (col.ForeignKey == null) { string sqlDbType = GetSqlTypeAttribute(table, col); if (sqlDbType != null) attributes.Add(sqlDbType); } if (GetEmbeddedField(table, col) != null || col.Name != DefaultColumnName(table, col)) attributes.Add("ColumnName(\"" + col.Name + "\")"); if (HasUniqueIndex(table, col)) attributes.Add("UniqueIndex"); return attributes; }
protected virtual string?GetStringLengthValidator(DiffTable table, DiffColumn col, string?relatedEntity) { if (GetValueType(col) != typeof(string)) { return(null); } var parts = new List <string>(); var min = GetMinStringLength(col); if (min != null) { parts.Add("Min = " + min); } if (col.Length != -1) { parts.Add("Max = " + col.Length); } return("StringLengthValidator(" + parts.ToString(", ") + ")"); }
protected virtual bool IsReadonly(DiffTable table, DiffColumn col) { return false; }
protected virtual bool SqlTypeAttributeNecessary(List <string> parts, DiffTable table, DiffColumn col) { var part = parts.Only(); if (part != null && part.StartsWith("Size = ") && GetValueType(col) == typeof(string)) { return(false); } return(true); }
protected virtual bool IsLite(DiffTable table, DiffColumn col) { return true; }
protected virtual int?GetMinStringLength(DiffColumn col) { return(1); }
protected virtual bool NotNullAttributeNecessary(DiffTable table, DiffColumn col, bool isMList) { return(false); //NotNullValidator is enought }
protected virtual bool HasNotNullableAttribute(DiffColumn col, string relatedEntity) { return !col.Nullable && (relatedEntity != null || GetValueType(col).IsClass); }
protected virtual bool HasUniqueIndex(DiffTable table, DiffColumn col) { return table.Indices.Values.Any(a => a.FilterDefinition == null && a.Columns.Only() == col.Name && a.IsUnique && a.Type == DiffIndexType.NonClustered); }
protected virtual bool RequiresColumnName(DiffTable table, DiffColumn col) { return GetEmbeddedField(table, col) != null || col.Name != DefaultColumnName(table, col); }
protected virtual bool IsReadonly(DiffTable table, DiffColumn col) { return(false); }
protected virtual string WriteField(string fileName, DiffTable table, DiffColumn col) { string relatedEntity = GetRelatedEntity(table, col); string type = GetFieldType(table, col, relatedEntity); string fieldName = GetFieldName(table, col); StringBuilder sb = new StringBuilder(); WriteAttributeTag(sb, GetFieldAttributes(table, col, relatedEntity)); WriteAttributeTag(sb, GetPropertyAttributes(table, col, relatedEntity)); sb.AppendLine("public {0} {1} { get; {2}set; }".FormatWith(type, fieldName.FirstUpper(), IsReadonly(table, col) ? "private" : null)); return sb.ToString(); }
protected virtual bool RequiresNotNullableAttribute(DiffColumn col, string relatedEntity) { return(!col.Nullable && (relatedEntity != null || GetValueType(col).IsClass)); }
protected virtual string GetEmbeddedField(DiffTable table, DiffColumn col) { return null; }
public MListInfo(DiffColumn backReferenceColumn) { this.BackReferenceColumn = backReferenceColumn; }
protected virtual bool RequiresColumnName(DiffTable table, DiffColumn col) { return(GetEmbeddedField(table, col) != null || col.Name != DefaultColumnName(table, col)); }
protected virtual int? GetMinStringLength(DiffColumn col) { return 1; }
protected virtual string GetStringLengthValidator(DiffTable table, DiffColumn col, string relatedEntity) { if (GetValueType(col) != typeof(string)) return null; var parts = new List<string>(); parts.Add("AllowNulls = " + col.Nullable.ToString().ToLower()); var min = GetMinStringLength(col); if (min != null) parts.Add("Min = " + min); if (col.Length != -1) parts.Add("Max = " + col.Length / DiffColumn.BytesPerChar(col.SqlDbType)); return "StringLengthValidator(" + parts.ToString(", ") + ")"; }
protected virtual bool IsLite(DiffTable table, DiffColumn col) { return(true); }
protected virtual DiffColumn GetMListTrivialElementColumn(DiffTable table, DiffColumn parentColumn, DiffColumn orderColumn) { return table.Columns.Values.Where(c => c != parentColumn && c != orderColumn && !c.PrimaryKey).Only(); }