private static SqlPreCommandSimple UpdateByFkChange(string tn, DiffColumn difCol, IColumn tabCol, Func <ObjectName, ObjectName> changeName) { if (difCol.ForeignKey == null || tabCol.ReferenceTable == null || tabCol.AvoidForeignKey) { return(null); } ObjectName oldFk = changeName(difCol.ForeignKey.TargetTable); if (oldFk.Equals(tabCol.ReferenceTable.Name)) { return(null); } AliasGenerator ag = new AliasGenerator(); return(new SqlPreCommandSimple( @"UPDATE {2} SET {0} = -- get {5} id from {4}.Id FROM {1} {2} JOIN {3} {4} ON {2}.{0} = {4}.Id".FormatWith(tabCol.Name, tn, ag.NextTableAlias(tn), oldFk, ag.NextTableAlias(oldFk.Name), tabCol.ReferenceTable.Name.Name))); }
private static string GenerateColumnCode(DiffColumn c) { var type = CodeGeneration.CodeGenerator.Entities.GetValueType(c); if (c.Nullable) { type = type.Nullify(); } StringBuilder sb = new StringBuilder(); if (c.Identity) { sb.AppendLine("[ViewPrimaryKey]"); } sb.AppendLine($"public {type.TypeName()} {c.Name};"); return(sb.ToString()); }
public static string CreateOldColumn(DiffColumn c) { string fullType = GetColumnType(c); var generatedAlways = c.GeneratedAlwaysType != GeneratedAlwaysType.None ? $"GENERATED ALWAYS AS ROW {(c.GeneratedAlwaysType == GeneratedAlwaysType.AsRowStart ? "START" : "END")} HIDDEN" : null; var defaultConstraint = c.DefaultConstraint != null ? $"CONSTRAINT {c.DefaultConstraint.Name} DEFAULT " + c.DefaultConstraint.Definition : null; return($" ".Combine( c.Name.SqlEscape(), fullType, c.Identity ? "IDENTITY " : null, generatedAlways, c.Collation != null ? ("COLLATE " + c.Collation) : null, c.Nullable ? "NULL" : "NOT NULL", defaultConstraint )); }
private static SqlPreCommandSimple UpdateByFkChange(string tn, DiffColumn difCol, IColumn tabCol, Func<ObjectName, ObjectName> changeName) { if (difCol.ForeignKey == null || tabCol.ReferenceTable == null || tabCol.AvoidForeignKey) return null; ObjectName oldFk = changeName(difCol.ForeignKey.TargetTable); if (oldFk.Equals(tabCol.ReferenceTable.Name)) return null; AliasGenerator ag = new AliasGenerator(); return new SqlPreCommandSimple( @"UPDATE {2} SET {0} = -- get {5} id from {4}.Id FROM {1} {2} JOIN {3} {4} ON {2}.{0} = {4}.Id".FormatWith(tabCol.Name, tn, ag.NextTableAlias(tn), oldFk, ag.NextTableAlias(oldFk.Name), tabCol.ReferenceTable.Name.Name)); }
public static string GetColumnType(DiffColumn c) { return(c.SqlDbType == SqlDbType.Udt ? c.UserTypeName ! : c.SqlDbType.ToString().ToUpper()) /*+ GetSizeScale(Math.Max(c.Length, c.Precision), c.Scale)*/; }
public static SqlPreCommand AlterTableAddOldColumn(ITable table, DiffColumn column) { return(new SqlPreCommandSimple("ALTER TABLE {0} ADD {1}".FormatWith(table.Name, CreateOldColumn(column)))); }