private void LoadConstraints()
        {
            Constraints = ConstraintInfo.Read(Processor, TableMeta);
            foreach (ConstraintInfo constraint in Constraints)
            {
                List <string> columns = new List <string>();
                if (Indexes.Any(x => x.Name == constraint.IndexName))
                {
                    columns = Indexes.First(x => x.Name == constraint.IndexName).Columns;
                }

                foreach (ColumnDefinition column in Definition.Columns)
                {
                    if (columns.Contains(column.Name))
                    {
                        if (constraint.IsPrimaryKey)
                        {
                            column.IsPrimaryKey   = true;
                            column.PrimaryKeyName = constraint.Name;
                            RemoveIndex(constraint.Name);
                        }

                        if (constraint.IsNotNull)
                        {
                            column.IsNullable = false;
                        }

                        if (constraint.IsUnique)
                        {
                            column.IsUnique = true;
                        }
                    }
                }

                if (constraint.IsForeignKey)
                {
                    ForeignKeyDefinition fkDef = new ForeignKeyDefinition()
                    {
                        Name           = constraint.Name,
                        ForeignTable   = TableMeta.Name,
                        ForeignColumns = columns,
                        PrimaryTable   = constraint.ForeignIndex.TableName,
                        PrimaryColumns = constraint.ForeignIndex.Columns,
                        OnUpdate       = constraint.UpdateRule,
                        OnDelete       = constraint.DeleteRule
                    };

                    RemoveIndex(constraint.Name);

                    Definition.ForeignKeys.Add(fkDef);
                }
            }
        }
Exemplo n.º 2
0
        public DocumentTypeBuilder <T> DefineIndex(Func <IndexKeysDefinitionBuilder <T>, IndexKeysDefinition <T> > builder, Action <CreateIndexOptions <T> > options)
        {
            IndexKeysDefinitionBuilder <T> indexBuilder = Builders <T> .IndexKeys;
            var indexOptionsBuilder = new CreateIndexOptions <T>();
            var definition          = builder(indexBuilder);

            options(indexOptionsBuilder);
            if (Indexes.Any(c => c.Item2.Name == indexOptionsBuilder.Name))
            {
                throw new ArgumentException($"this '{indexOptionsBuilder.Name}' index name exists.");
            }
            Indexes.Add(Tuple.Create(definition, indexOptionsBuilder));
            _apply(this);
            return(this);
        }
        /// <summary>
        /// Serialize the data type description to XML
        /// </summary>
        /// <returns>Serialized data type descriptor</returns>
        public XElement ToXml()
        {
            var element = new XElement("DataTypeDescriptor",
                                       new XAttribute("dataTypeId", this.DataTypeId),
                                       new XAttribute("name", this.Name),
                                       new XAttribute("namespace", this.Namespace),
                                       this.Title != null ? new XAttribute("title", this.Title) : null,
                                       new XAttribute("isCodeGenerated", this.IsCodeGenerated),
                                       new XAttribute("cachable", this.Cachable),
                                       new XAttribute("searchable", this.Searchable),
                                       this.LabelFieldName != null ? new XAttribute("labelFieldName", this.LabelFieldName) : null,
                                       !string.IsNullOrEmpty(this.InternalUrlPrefix) ? new XAttribute("internalUrlPrefix", this.InternalUrlPrefix) : null,
                                       this.TypeManagerTypeName != null ? new XAttribute("typeManagerTypeName", this.TypeManagerTypeName) : null,
                                       !string.IsNullOrEmpty(this.BuildNewHandlerTypeName) ? new XAttribute("buildNewHandlerTypeName", this.BuildNewHandlerTypeName) : null);


            element.Add(new[]
            {
                new XElement("DataAssociations",
                             DataAssociations.Select(da => da.ToXml())),
                new XElement("DataScopes",
                             DataScopes.Select(dsi => new XElement("DataScopeIdentifier", new XAttribute("name", dsi)))),
                new XElement("KeyPropertyNames",
                             KeyPropertyNames.Select(name => new XElement("KeyPropertyName", new XAttribute("name", name)))),
                VersionKeyPropertyNames.Any()
                    ? new XElement("VersionKeyPropertyNames",
                                   VersionKeyPropertyNames.Select(name => new XElement("VersionKeyPropertyName", new XAttribute("name", name))))
                    : null,
                new XElement("SuperInterfaces",
                             SuperInterfaces.Select(su => new XElement("SuperInterface", new XAttribute("type", TypeManager.SerializeType(su))))),
                new XElement("Fields", Fields.Select(f => f.ToXml()))
            });

            if (Indexes.Any())
            {
                element.Add(new XElement("Indexes", Indexes.Select(i => i.ToXml())));
            }

            return(element);
        }
Exemplo n.º 4
0
 public bool ContainsIndex(string name) => Indexes.Any(index => index.Name.Equals(name));
 public bool HasClusteredIndex()
 {
     return(Indexes.Any(x => x.IsClustered));
 }