Esempio n. 1
0
        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;
        }
Esempio n. 2
0
        protected AtomMemberInfo GetStrongTypeMember(AtomMemberInfo member)
        {
            if (member.HasReference && member.Reference.IsReferenceToHiddenPrimaryKey)
            {
                return member.Reference.TargetAtomAlternateKey;
            }

            return member;
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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)));
        }
Esempio n. 5
0
        private string GetMemberConstraint(AtomMemberInfo value)
        {
            if (value.IsPrimary ||
                value.IsAltKey ||
                !value.Optional)
            {
                return "NOT NULL";
            }

            return "NULL";
        }
Esempio n. 6
0
        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]";
        }
Esempio n. 7
0
        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}");
        }
Esempio n. 8
0
        protected bool IsQueryableColumn(AtomMemberInfo arg)
        {
            if (arg.IsPrimary &&
                Atom.AdditionalInfo.ShouldHidePrimaryKey())
            {
                return false;
            }

            if (IsKey(arg))
            {
                return true;
            }

            return true;
        }
Esempio n. 9
0
        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;
        }
Esempio n. 10
0
        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;
        }
Esempio n. 11
0
        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;
        }
Esempio n. 12
0
        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;
        }
Esempio n. 13
0
        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;
        }
Esempio n. 14
0
 private string GetLookupColumnName(AtomMemberInfo key)
 {
     return "id";
 }
Esempio n. 15
0
 public BatchListSproc(AtomModel atom, AtomMemberInfo byKey = null)
     : base(atom)
 {
     _byKey = byKey;
 }
Esempio n. 16
0
 private string ValidateDefault(AtomMemberInfo value)
 {
     return value.MemberType.ValidateDefault(value.DefaultValue);
 }
Esempio n. 17
0
 private string GetMemberType(AtomMemberInfo value)
 {
     return value.MemberType.Accept(new SqlDescriptionVisitor());
 }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
        private string GetConstraintModifier(AtomMemberInfo member)
        {
            if (member.IsPrimary && !(member.MemberType is MemberGuid))
            {
                return "CLUSTERED";
            }

            return "NONCLUSTERED";
        }
Esempio n. 20
0
 private string BuildIndexColumnDef(AtomMemberInfo member)
 {
     return $"[{member.Name}] {GetIndexColumnDeclaration(member)}".Trim();
 }
Esempio n. 21
0
        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;
        }
Esempio n. 22
0
 public GetAllSproc(AtomModel atom, AtomMemberInfo byKey = null)
     : base(atom)
 {
     _byKey = byKey;
 }
Esempio n. 23
0
        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;
        }
Esempio n. 24
0
        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;
        }
Esempio n. 25
0
        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;
        }
Esempio n. 26
0
 public CSharpMemberNameFinder(AtomMemberInfo member)
     : this(AliasedAtomMemberInfo.FromAtomMemberInfo(member))
 {
 }
Esempio n. 27
0
 public GetBySproc(AtomModel atom, AtomMemberInfo byKey, bool isUnique)
     : base(atom)
 {
     _byKey = byKey;
     _isUnique = isUnique;
 }
Esempio n. 28
0
 public CSharpStrongTypeNameFinder(AtomMemberInfo member)
 {
     _member = member;
 }