private void ResetState()
 {
     _connection = null;
     _tableSelectionSet = null;
     _databaseModel = new DatabaseModel();
     _tables = new Dictionary<string, TableModel>(StringComparer.OrdinalIgnoreCase);
     _tableColumns = new Dictionary<string, ColumnModel>(StringComparer.OrdinalIgnoreCase);
 }
 public void Creates_entity_types()
 {
     var info = new DatabaseModel
     {
         Tables =
         {
             new TableModel
             {
                 Name = "tableWithSchema", SchemaName = "public",
                 Columns = { IdColumn }
             },
             new TableModel
             {
                 Name = "noSchema",
                 Columns = { IdColumn }
             },
             new TableModel
             {
                 Name = "notScaffoldable"
             }
         }
     };
     var model = _factory.Create(info);
     Assert.Collection(model.GetEntityTypes().OrderBy(t => t.Name).Cast<EntityType>(),
         table =>
             {
                 Assert.Equal("noSchema", table.Relational().TableName);
                 Assert.Null(table.Relational().Schema);
             },
         pgtable =>
             {
                 Assert.Equal("tableWithSchema", pgtable.Relational().TableName);
                 Assert.Equal("public", pgtable.Relational().Schema);
             }
         );
     Assert.NotEmpty(model.Scaffolding().EntityTypeErrors.Values);
 }
 public IModel Create(DatabaseModel databaseModel) => CreateFromDatabaseModel(databaseModel);
        public void Sequences()
        {
            var info = new DatabaseModel
            {
                Sequences =
                {
                    new SequenceModel { Name = "CountByThree", IncrementBy = 3 }
                }
            };

            var model = (Model)_factory.Create(info);

            Assert.Collection(model.Relational().Sequences, first =>
                {
                    Assert.NotNull(first);
                    Assert.Equal("CountByThree", first.Name);
                    Assert.Equal(3, first.IncrementBy);
                    Assert.Null(first.Schema);
                    Assert.Null(first.MaxValue);
                    Assert.Null(first.MinValue);
                    Assert.False(first.IsCyclic);
                });
        }
        public void Loads_column_types()
        {
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "Jobs",
                        Columns =
                        {
                            IdColumn,
                            new ColumnModel
                            {
                                Name = "occupation",
                                DataType = "string",
                                DefaultValue = "\"dev\""
                            },
                            new ColumnModel
                            {
                                Name = "salary",
                                DataType = "long",
                                IsNullable = true,
                                MaxLength = 100
                            },
                            new ColumnModel
                            {
                                Name = "modified",
                                DataType = "string",
                                IsNullable = false,
                                ValueGenerated = ValueGenerated.OnAddOrUpdate
                            },
                            new ColumnModel
                            {
                                Name = "created",
                                DataType = "string",
                                ValueGenerated = ValueGenerated.OnAdd
                            }
                        }
                    }
                }
            };

            var entityType = (EntityType)_factory.Create(info).FindEntityType("Jobs");

            Assert.Collection(entityType.GetProperties(),
                pk =>
                    {
                        Assert.Equal("Id", pk.Name);
                        Assert.Equal(typeof(long), pk.ClrType);
                    },
                col1 =>
                    {
                        Assert.Equal("created", col1.Relational().ColumnName);
                        Assert.Equal(ValueGenerated.OnAdd, col1.ValueGenerated);
                    },
                col2 =>
                    {
                        Assert.Equal("modified", col2.Relational().ColumnName);
                        Assert.Equal(ValueGenerated.OnAddOrUpdate, col2.ValueGenerated);
                    },
                col3 =>
                    {
                        Assert.Equal("occupation", col3.Relational().ColumnName);
                        Assert.Equal(typeof(string), col3.ClrType);
                        Assert.False(col3.IsColumnNullable());
                        Assert.Null(col3.GetMaxLength());
                        Assert.Equal("\"dev\"", col3.Relational().DefaultValueSql);
                    },
                col4 =>
                    {
                        Assert.Equal("salary", col4.Name);
                        Assert.Equal(typeof(long?), col4.ClrType);
                        Assert.True(col4.IsColumnNullable());
                        Assert.Equal(100, col4.GetMaxLength());
                        Assert.Null(col4.Relational().DefaultValue);
                    });
        }
        public void Unique_names()
        {
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "E F", Columns =
                        {
                            new ColumnModel { Name = "San itized", DataType = "long" },
                            new ColumnModel { Name = "San_itized", DataType = "long" }
                        }
                    },
                    new TableModel { Name = "E_F" }
                }
            };

            info.Tables.ElementAt(0).Columns.Add(new ColumnModel { Name = "Id", DataType = "long", PrimaryKeyOrdinal = 0, Table = info.Tables.ElementAt(0) });
            info.Tables.ElementAt(1).Columns.Add(new ColumnModel { Name = "Id", DataType = "long", PrimaryKeyOrdinal = 0, Table = info.Tables.ElementAt(1) });

            var model = _factory.Create(info);

            Assert.Collection(model.GetEntityTypes().Cast<EntityType>(),
                ef1 =>
                    {
                        Assert.Equal("E F", ef1.Relational().TableName);
                        Assert.Equal("E_F", ef1.Name);
                        Assert.Collection(ef1.GetProperties(),
                            id => { Assert.Equal("Id", id.Name); },
                            s1 =>
                                {
                                    Assert.Equal("San_itized", s1.Name);
                                    Assert.Equal("San itized", s1.Relational().ColumnName);
                                },
                            s2 =>
                                {
                                    Assert.Equal("San_itized1", s2.Name);
                                    Assert.Equal("San_itized", s2.Relational().ColumnName);
                                });
                    },
                ef2 =>
                    {
                        Assert.Equal("E_F", ef2.Relational().TableName);
                        Assert.Equal("E_F1", ef2.Name);
                        var id = Assert.Single(ef2.GetProperties());
                        Assert.Equal("Id", id.Name);
                        Assert.Equal("Id", id.Relational().ColumnName);
                    });
        }
        public void Indexes_and_alternate_keys()
        {
            var table = new TableModel
            {
                Name = "T",
                Columns =
                {
                    new ColumnModel { Name = "C1", DataType = "long", PrimaryKeyOrdinal = 1 },
                    new ColumnModel { Name = "C2", DataType = "long" },
                    new ColumnModel { Name = "C3", DataType = "long" }
                }
            };
            table.Indexes.Add(new IndexModel
            {
                Name = "IDX_C1",
                IndexColumns = { new IndexColumnModel { Column = table.Columns.ElementAt(0) } },
                IsUnique = false
            });
            table.Indexes.Add(new IndexModel
            {
                Name = "UNQ_C2",
                IndexColumns = { new IndexColumnModel { Column = table.Columns.ElementAt(1) } },
                IsUnique = true
            });
            table.Indexes.Add(new IndexModel
            {
                Name = "IDX_C2_C1",
                IndexColumns =
                {
                    new IndexColumnModel { Column = table.Columns.ElementAt(1) },
                    new IndexColumnModel { Column = table.Columns.ElementAt(0) }
                },
                IsUnique = false
            });
            table.Indexes.Add(new IndexModel
            {
                /*Name ="UNQ_C3_C1",*/
                IndexColumns =
                {
                    new IndexColumnModel { Column = table.Columns.ElementAt(2) },
                    new IndexColumnModel { Column = table.Columns.ElementAt(0) }
                },
                IsUnique = true
            });

            var info = new DatabaseModel { Tables = { table } };

            var entityType = (EntityType)_factory.Create(info).GetEntityTypes().Single();

            Assert.Collection(entityType.GetIndexes(),
                indexColumn1 =>
                    {
                        Assert.False(indexColumn1.IsUnique);
                        Assert.Equal("IDX_C1", indexColumn1.Relational().Name);
                        Assert.Same(entityType.FindProperty("C1"), indexColumn1.Properties.Single());
                    },
                uniqueColumn2 =>
                    {
                        Assert.True(uniqueColumn2.IsUnique);
                        Assert.Same(entityType.FindProperty("C2"), uniqueColumn2.Properties.Single());
                    },
                indexColumn2Column1 =>
                    {
                        Assert.False(indexColumn2Column1.IsUnique);
                        Assert.Equal(new[] { "C2", "C1" }, indexColumn2Column1.Properties.Select(c => c.Name).ToArray());
                    },
                uniqueColumn3Column1 =>
                    {
                        Assert.True(uniqueColumn3Column1.IsUnique);
                        Assert.Equal(new[] { "C3", "C1" }, uniqueColumn3Column1.Properties.Select(c => c.Name).ToArray());
                    }
                );

            Assert.Collection(entityType.GetKeys().Where(k => !k.IsPrimaryKey()),
                single =>
                    {
                        Assert.Equal("UNQ_C2", single.Relational().Name);
                        Assert.Same(entityType.FindProperty("C2"), single.Properties.Single());
                    },
                composite => { Assert.Equal(new[] { "C3", "C1" }, composite.Properties.Select(c => c.Name).ToArray()); });
        }
        public void Unmappable_column_type(string dataType)
        {
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "E",
                        Columns = { IdColumn }
                    }
                }
            };

            info.Tables.First().Columns.Add(new ColumnModel
            {
                Table = info.Tables.First(),
                Name = "Coli",
                DataType = dataType
            });

            Assert.Single(_factory.Create(info).FindEntityType("E").GetProperties());
            Assert.Contains(RelationalDesignStrings.CannotFindTypeMappingForColumn("E.Coli", dataType), _logger.FullLog);
        }
        public void Primary_key(string[] keyProps, int length)

        {
            var ordinal = 3;
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "PkTable",
                        Columns = keyProps.Select(k => new ColumnModel { PrimaryKeyOrdinal = ordinal++, Name = k, DataType = "long" }).ToList()
                    }
                }
            };
            var model = (EntityType)_factory.Create(info).GetEntityTypes().Single();

            Assert.Equal(keyProps, model.FindPrimaryKey().Properties.Select(p => p.Relational().ColumnName).ToArray());
        }
        public void Column_type_annotation(string dataType, string expectedColumnType)
        {
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "A",
                        Columns =
                        {
                            new ColumnModel
                            {
                                Name = "Col",
                                DataType = dataType,
                                PrimaryKeyOrdinal = 1
                            }
                        }
                    }
                }
            };

            var property = (Property)_factory.Create(info).FindEntityType("A").FindProperty("Col");

            Assert.Equal(expectedColumnType, property.Relational().ColumnType);
        }
        public void Column_ordinal_annotation()
        {
            var info = new DatabaseModel
            {
                Tables =
                {
                    new TableModel
                    {
                        Name = "A",
                        Columns =
                        {
                            new ColumnModel
                            {
                                Name = "Col1",
                                DataType = "string",
                                PrimaryKeyOrdinal = 1,
                                Ordinal = 1
                            },
                            new ColumnModel
                            {
                                Name = "Col2",
                                DataType = "string",
                                Ordinal = 2
                            },
                            new ColumnModel
                            {
                                Name = "Col3",
                                DataType = "string",
                                Ordinal = 3
                            }
                        }
                    }
                }
            };

            var entityTypeA = _factory.Create(info).FindEntityType("A");
            var property1 = (Property)entityTypeA.FindProperty("Col1");
            var property2 = (Property)entityTypeA.FindProperty("Col2");
            var property3 = (Property)entityTypeA.FindProperty("Col3");

            Assert.Equal(1, property1.Scaffolding().ColumnOrdinal);
            Assert.Equal(2, property2.Scaffolding().ColumnOrdinal);
            Assert.Equal(3, property3.Scaffolding().ColumnOrdinal);
        }
Пример #12
0
 public DatabaseSequence([NotNull] DatabaseModel database, [NotNull] string name)
 {
     Database = database;
     Name     = name;
 }
        public SqlServerDatabaseModelAnnotations([NotNull] DatabaseModel databaseModel)
        {
            Check.NotNull(databaseModel, nameof(databaseModel));

            _databaseModel = databaseModel;
        }
        public SqlServerDatabaseModelAnnotations([NotNull] DatabaseModel databaseModel)
        {
            Check.NotNull(databaseModel, nameof(databaseModel));

            _databaseModel = databaseModel;
        }
Пример #15
0
 public DatabaseView([NotNull] DatabaseModel database, [NotNull] string name)
     : base(database, name)
 {
 }
 public static SqlServerDatabaseModelAnnotations SqlServer([NotNull] this DatabaseModel databaseModel)
 => new SqlServerDatabaseModelAnnotations(databaseModel);