private string ForeignKeyConstraint(AtomMemberInfo member) { var foreignTable = member.Reference.Name; var foreignKeyColumnName = string.IsNullOrWhiteSpace(member.Reference.Member) ? member.Name : member.Reference.Member; // fix naming convention if we have multiple columns pointing to the same foreign key // the name goes from FK_Table_FkKey to FK_Table_ColumnName if ( member.Atom.Members.Any( i => i.HasReference && i.Reference.Member == member.Reference.Member && i.Reference.Name == member.Reference.Name && i.Name != member.Name)) { foreignKeyColumnName = member.Name; } string Schema = _table.AdditionalInfo.Schema, TableName = _table.Name, ForeignTable = foreignTable, ForeignKeyName = $"FK_{_table.Name}_{foreignTable}_{foreignKeyColumnName}", ForeignColumn = member.Reference.Member ?? member.Name, MemberName = member.Name, schema = member.Atom.AdditionalInfo.Schema; string fk = $@"ALTER TABLE [{schema}].[{TableName}] ADD CONSTRAINT [{ForeignKeyName}] FOREIGN KEY ([{MemberName}]) REFERENCES [{Schema}].[{ForeignTable}] ([{ForeignColumn}]) GO"; return fk; }
protected AtomMemberInfo GetStrongTypeMember(AtomMemberInfo member) { if (member.HasReference && member.Reference.IsReferenceToHiddenPrimaryKey) { return member.Reference.TargetAtomAlternateKey; } return member; }
private string Generate(AtomMemberInfo member) { var type = GetMemberType(member); var constraint = GetMemberConstraint(member); var defaultValue = GetDefaultValue(member.Name, member); return $@"[{member.Name}] {type} {constraint} {defaultValue}".Trim(); }
protected bool CanBeStronger(AtomMemberInfo member) { if (!Config.Entities.StrongTypes || member.HasFlag(MemberFlags.Hidden)) { return false; } return member.IsAltKey || member.IsPrimary || (member.HasReference && (member.Reference.IsReferenceToHiddenPrimaryKey || CanBeStronger(member.Reference.TargetMember))); }
private string GetMemberConstraint(AtomMemberInfo value) { if (value.IsPrimary || value.IsAltKey || !value.Optional) { return "NOT NULL"; } return "NULL"; }
private string BuildIndex(AtomMemberInfo member) { string IndexType = (member.Unique ? "UNIQUE " : "") + "NONCLUSTERED", TableName = _data.Name, Columns = member.Name, Schema = _data.AdditionalInfo.Schema, IndexPrefix = member.Unique ? "AK" : "IX", SpacedColumns = BuildIndexColumnDef(member); return $@"CREATE {IndexType} INDEX [{IndexPrefix}_{TableName}_{Columns}] ON [{Schema}].[{TableName}] ({SpacedColumns}) ON [PRIMARY]"; }
private string GetTableType(AtomMemberInfo key) { if (key.MemberType is MemberGuid) { return Constants.GuidTableType; } if (key.MemberType is MemberLong) { return Constants.BigIntTableType; } throw new Exception($"Custom table type didn't exist for column {key.Name} with type {key.Type}"); }
protected bool IsQueryableColumn(AtomMemberInfo arg) { if (arg.IsPrimary && Atom.AdditionalInfo.ShouldHidePrimaryKey()) { return false; } if (IsKey(arg)) { return true; } return true; }
private string GetUpdateValues(AtomMemberInfo member) { if (member.HasFlag(MemberFlags.SoftDeleteTracking)) { return member.Name + " = 1"; } if (member.Atom.AdditionalInfo.Temporal.HasTemporal.GetValueOrDefault() && member.HasFlag(MemberFlags.LastModifiedDateTimetracking)) { return member.Name + " = @NOW"; } return null; }
private string AlternateKeyConstraints(AtomMemberInfo member) { string Schema = _table.AdditionalInfo.Schema, TableName = _table.Name, ConstraintName = GetConstraintName(member), ConstraintType = GetConstraintType(member), MemberName = member.Name, schema = member.Atom.AdditionalInfo.Schema; string ak = $@"ALTER TABLE [{Schema}].[{TableName}] ADD CONSTRAINT [{ConstraintName}] {ConstraintType} ([{MemberName}]) ON [PRIMARY] GO"; ; return ak; }
protected string GetMemberType(AtomMemberInfo member) { if (CanBeStronger(member)) { var referencingTypeName = new CSharpStrongTypeNameFinder(member).TypeName(); if (member.Optional) { return referencingTypeName + "?"; } return referencingTypeName; } string typeName = member.MemberType.Accept(new CSharpDefaultTypeFinder(member.Optional)); return typeName; }
private string GetDefaultValue(string name, AtomMemberInfo value) { // prevent duplicate primary keys getting the Identity Value if (!_primaryKeySet && value.IsPrimary && value.MemberType is MemberLong && !_atomModel.IsLookup) { _primaryKeySet = true; return "IDENTITY(1,1)"; } if (!string.IsNullOrEmpty(value.DefaultValue)) { return $"CONSTRAINT [DF_{_atomModel.Name}_{name}] DEFAULT ({ValidateDefault(value)})"; } return string.Empty; }
private string GetUpdateValues(AtomMemberInfo member) { if (member.IsPrimary || member.IsAltKey) { return null; } if (member.Atom.AdditionalInfo.Temporal.HasTemporal.GetValueOrDefault()) { if (member.HasFlag(MemberFlags.LastModifiedDateTimetracking)) { return member.Name + " = @NOW"; } if (member.HasFlag(MemberFlags.CreatedDateTimeTracking)) { return null; } } return member.Name + " = @" + member.Name; }
private string GetLookupColumnName(AtomMemberInfo key) { return "id"; }
public BatchListSproc(AtomModel atom, AtomMemberInfo byKey = null) : base(atom) { _byKey = byKey; }
private string ValidateDefault(AtomMemberInfo value) { return value.MemberType.ValidateDefault(value.DefaultValue); }
private string GetMemberType(AtomMemberInfo value) { return value.MemberType.Accept(new SqlDescriptionVisitor()); }
private string GetIndexColumnDeclaration(AtomMemberInfo member) { if (!member.SortDirection.HasValue) { return string.Empty; } switch (member.SortDirection.Value) { case AtomMemberSortDirection.Asc: return string.Empty; case AtomMemberSortDirection.Desc: return "DESC"; } string message = "Unknown index column declaration"; Log.Error($"{message} {{@Member}}", member); throw new Exception(message); }
private string GetConstraintModifier(AtomMemberInfo member) { if (member.IsPrimary && !(member.MemberType is MemberGuid)) { return "CLUSTERED"; } return "NONCLUSTERED"; }
private string BuildIndexColumnDef(AtomMemberInfo member) { return $"[{member.Name}] {GetIndexColumnDeclaration(member)}".Trim(); }
private string StrongTypeCastingType(AtomMemberInfo member) { if (Config.Entities.StrongTypes && CanBeStronger(member)) { var type = member.MemberType; if (member.HasReference && member.Reference.IsReferenceToHiddenPrimaryKey) { type = member.Reference.TargetAtomAlternateKey.MemberType; } return "(" + new CSharpDefaultTypeFinder(member.Optional).Visit(type) + ")"; } return string.Empty; }
public GetAllSproc(AtomModel atom, AtomMemberInfo byKey = null) : base(atom) { _byKey = byKey; }
private string GetConstraintType(AtomMemberInfo member) { if (member.IsPrimary) { return "PRIMARY KEY " + GetConstraintModifier(member); } if ((member.IsAltKey || member.Unique)) { return "UNIQUE " + GetConstraintModifier(member); } return string.Empty; }
private string GetConstraintName(AtomMemberInfo member) { if (member.IsPrimary) { return "PK_" + _table.Name; } if (member.IsAltKey || member.Unique) { return $"AK_{_table.Name}_{member.Name}"; } return string.Empty; }
private bool IsListableField(AtomMemberInfo arg) { if (arg.IsPrimary && Atom.AdditionalInfo.ShouldHidePrimaryKey()) { return false; } if (IsKey(arg)) { return true; } if (IsHiddenTemporal(arg)) { return false; } return true; }
public CSharpMemberNameFinder(AtomMemberInfo member) : this(AliasedAtomMemberInfo.FromAtomMemberInfo(member)) { }
public GetBySproc(AtomModel atom, AtomMemberInfo byKey, bool isUnique) : base(atom) { _byKey = byKey; _isUnique = isUnique; }
public CSharpStrongTypeNameFinder(AtomMemberInfo member) { _member = member; }