Esempio n. 1
0
        private IEnumerable <CreateTableStatement> GetCreateTableStatements()
        {
            foreach (var entitySet in edmModel.Container.EntitySets)
            {
                ICollection <AssociationType> associationTypes =
                    edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entitySet.Name).ToList();

                IList <AssociationTypeWrapper> associationTypeWrappers = new List <AssociationTypeWrapper>();
                foreach (var associationType in associationTypes)
                {
                    string fromTable = edmModel.Container.GetEntitySetByName(associationType.Constraint.FromRole.Name, true).Table;
                    string toTable   = edmModel.Container.GetEntitySetByName(associationType.Constraint.ToRole.Name, true).Table;

                    string fromTableName = TableNameCreator.CreateTableName(fromTable);
                    string toTableName   = TableNameCreator.CreateTableName(toTable);

                    associationTypeWrappers.Add(new AssociationTypeWrapper
                    {
                        AssociationType = associationType,
                        FromTableName   = fromTableName,
                        ToTableName     = toTableName
                    });
                }

                var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypeWrappers);
                yield return(tableStatementBuilder.BuildStatement());
            }
        }
        public CreateTableStatement BuildStatement()
        {
            var keyMembers = entitySet.ElementType.KeyMembers.Cast <EdmProperty>().ToArray();

            // Only create a CompositePrimaryKeyStatement if there is a composite primary key.
            // If there is just one key member this is handled using a constraint.
            CompositePrimaryKeyStatement compositePrimaryKeyStatement = null;

            if (keyMembers.Length > 1)
            {
                compositePrimaryKeyStatement = new CompositePrimaryKeyStatementBuilder(keyMembers).BuildStatement();
            }

            var simpleColumnCollection = new ColumnStatementCollectionBuilder(entitySet.ElementType.Properties, keyMembers).BuildStatement();
            var foreignKeyCollection   = new ForeignKeyStatementBuilder(associationTypeContainer.GetAssociationTypes(entitySet.Name)).BuildStatement();

            var columnStatements = new List <IStatement>();

            columnStatements.AddRange(simpleColumnCollection);
            columnStatements.AddIfNotNull(compositePrimaryKeyStatement);
            columnStatements.AddRange(foreignKeyCollection);

            return(new CreateTableStatement
            {
                TableName = TableNameCreator.CreateTableName(entitySet.Table),
                ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
            });
        }
Esempio n. 3
0
        public CreateTableStatement BuildStatement()
        {
            var simpleColumnCollection = new ColumnStatementCollectionBuilder(entitySet.ElementType.Properties).BuildStatement();
            var primaryKeyStatement    = new PrimaryKeyStatementBuilder(entitySet.ElementType.KeyMembers).BuildStatement();
            var foreignKeyCollection   = new ForeignKeyStatementBuilder(associationTypes).BuildStatement();

            var columnStatements = new List <IStatement>();

            columnStatements.AddRange(simpleColumnCollection);
            columnStatements.Add(primaryKeyStatement);
            columnStatements.AddRange(foreignKeyCollection);

            return(new CreateTableStatement
            {
                TableName = TableNameCreator.CreateTableName(entitySet.Table),
                ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
            });
        }
        public static string GetTableName(this EntityType entityType)
        {
            MetadataProperty metadataProperty;

            if (!entityType.MetadataProperties.TryGetValue("TableName", false, out metadataProperty))
            {
                return(entityType.Name);
            }

            if (metadataProperty.Value.GetType().Name != "DatabaseName")
            {
                return(entityType.Name);
            }

            object metadataPropertyValue     = metadataProperty.Value;
            Type   metadataPropertyValueType = metadataProperty.Value.GetType();

            // The type DatabaseName is internal. So we need reflection...
            // GetValue() overload with one value was introduces in .net 4.5 so use the overload with two parameters.
            var name = (string)metadataPropertyValueType.GetProperty("Name").GetValue(metadataPropertyValue, null);

            return(TableNameCreator.CreateTableName(name));
        }
        public SqliteAssociationType(AssociationType associationType, EntityContainer container)
        {
            FromRoleEntitySetName = associationType.Constraint.FromRole.Name;
            ToRoleEntitySetName   = associationType.Constraint.ToRole.Name;

            string fromTable = container.GetEntitySetByName(FromRoleEntitySetName, true).Table;
            string toTable;

            if (IsSelfReferencing(associationType))
            {
                toTable             = fromTable;
                ToRoleEntitySetName = FromRoleEntitySetName;
            }
            else
            {
                toTable = container.GetEntitySetByName(ToRoleEntitySetName, true).Table;
            }

            FromTableName     = TableNameCreator.CreateTableName(fromTable);
            ToTableName       = TableNameCreator.CreateTableName(toTable);
            ForeignKey        = associationType.Constraint.ToProperties.Select(x => x.Name);
            ForeignPrimaryKey = associationType.Constraint.FromProperties.Select(x => x.Name);
            CascadeDelete     = associationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade;
        }
        public CreateIndexStatementCollection BuildStatement()
        {
            IDictionary <string, CreateIndexStatement> createIndexStatments = new Dictionary <string, CreateIndexStatement>();

            foreach (var edmProperty in entitySet.ElementType.Properties)
            {
                var indexAnnotations = edmProperty.MetadataProperties
                                       .Select(x => x.Value)
                                       .OfType <IndexAnnotation>();

                foreach (var index in indexAnnotations.SelectMany(ia => ia.Indexes))
                {
                    CreateIndexStatement createIndexStatement;
                    string indexName = GetIndexName(index, edmProperty);
                    if (!createIndexStatments.TryGetValue(indexName, out createIndexStatement))
                    {
                        createIndexStatement = new CreateIndexStatement
                        {
                            IsUnique = index.IsUnique,
                            Name     = indexName,
                            Table    = TableNameCreator.CreateTableName(entitySet.Table),
                            Columns  = new Collection <CreateIndexStatement.IndexColumn>()
                        };
                        createIndexStatments.Add(indexName, createIndexStatement);
                    }

                    createIndexStatement.Columns.Add(new CreateIndexStatement.IndexColumn
                    {
                        Name  = edmProperty.Name,
                        Order = index.Order
                    });
                }
            }

            return(new CreateIndexStatementCollection(createIndexStatments.Values));
        }
        public void CreateTableNameTest()
        {
            string result = TableNameCreator.CreateTableName("Test");

            Assert.AreEqual("\"Test\"", result);
        }