protected static void Generic_Constructs_The_Correct_One2One_Relationships(Database db)
        {
            var table1 = db.Tables[4];
            var table2 = db.Tables[3];

            Assert.That(table1.Relationships, Is.Not.Null);
            Assert.That(table2.Relationships, Is.Not.Null);
            Assert.That(table1.Relationships, Has.Count(1));
            Assert.That(table2.Relationships, Has.Count(1));
            Assert.That(table1.Relationships[0], Is.Not.Null);
            Assert.That(table2.Relationships[0], Is.Not.Null);
            Assert.That(table1.Relationships[0], Is.SameAs(table2.Relationships[0]));

            var relationship = table1.Relationships[0];
            Assert.That(relationship, Is.Not.Null);
            Assert.That(relationship.PrimaryTable, Is.SameAs(table2));
            Assert.That(relationship.ForeignTable, Is.SameAs(table1));
            Assert.That(relationship.SourceCardinality, Is.EqualTo(Cardinality.One));
            Assert.That(relationship.TargetCardinality, Is.EqualTo(Cardinality.One));

            Assert.That(relationship.PrimaryKey, Is.Not.Null);
            Assert.That(relationship.PrimaryKey.Columns, Has.Count(3));
            Assert.That(relationship.ForeignKey.Columns, Has.Count(3));
            Assert.That(relationship.PrimaryKey.Keytype, Is.EqualTo(DatabaseKeyType.Primary));
            Assert.That(relationship.ForeignKey.Keytype, Is.EqualTo(DatabaseKeyType.Foreign));
        }
        protected void SetupBase(string resourcePath)
        {
            loader = new EntityLoader(MockRepository.GenerateStub<IFileController>());

            database = new Database("DB1");
            table1 = new Table("Table1");
            table1.AddColumn(new Column("ID") { Datatype = "int" });
            table1.AddColumn(new Column("BasicClass2_Index") { Datatype = "int" });
            database.AddEntity(table1);

            table2 = new Table("Table2");
            table2.AddColumn(new Column("ID") { Datatype = "int" });
            table2.AddColumn(new Column("BASIC_CLASS_1_ID"));
            database.AddEntity(table2);

            tableManyToMany = new Table("Class1Class2");
            tableManyToMany.AddColumn(new Column("Class1ID") { Datatype = "int" });
            tableManyToMany.AddColumn(new Column("Class2ID") { Datatype = "int" });
            database.AddTable(tableManyToMany);

            relationship = table1.CreateRelationshipTo(table2);
            relationship.PrimaryKey.AddColumn("ID");
            relationship.ForeignKey.AddColumn("BASIC_CLASS_1_ID");

            // Call we are testing
            mappingSet = loader.GetEntities(new[] { Path.Combine("Resources", resourcePath) }, database);
        }
 private static IColumn GetColumn()
 {
     IDatabase db = new Database("DB1");
     Table table = new Table("Table2");
     table.Database = db;
     Column column = new Column("Column0011");
     column.Parent = table;
     return column;
 }
        private static TimeSpan RunSerialisationTest(int numTables, int numColumns)
        {
            IDatabase db = new Database("Test Database", ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes.SQLServer2005);
            SetupDatabase(db, numTables, numColumns);

            DateTime start = DateTime.Now;

            db.Serialise(new DatabaseSerialisationScheme());

            return DateTime.Now - start;
        }
        public void Setup()
        {
            database = new Database("DB1");
            table = new Table("Table1");
            database.AddTable(table);

            reference = new ReferenceImpl(new Guid("11111111-1111-1111-1111-111111111111"));

            entitySet = MockRepository.GenerateMock<EntitySet>();
            entitySet.Stub(e => e.References).Return(new List<Reference> { reference }.AsReadOnly());
        }
        public void Should_Return_This()
        {
            const string expectedXML = @"<LogicalSchema Version=""1""><DatabaseName>Database1</DatabaseName></LogicalSchema>";

            Database db1 = new Database("Database1");

            string outputXml = db1.Serialise(new DatabaseSerialisationScheme());
            outputXml = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXml);

            Assert.That(outputXml, Is.EqualTo(expectedXML));
        }
Example #7
0
        public MappingSetImpl()
        {
            Database = new Database("", ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes.Unknown);
            EntitySet = new EntitySetImpl();

            EventAggregator = new EventAggregator(this);
            TablePrefixes = new List<string>();
            ColumnPrefixes = new List<string>();
            TableSuffixes = new List<string>();
            ColumnSuffixes = new List<string>();
        }
        public void It_Should_Deserialise_To_This()
        {
            var xml = "<MappingSet />";

            Database database = new Database("DB1");
            EntitySet entitySet = new EntitySetImpl();

            MappingSet set = new MappingSetDeserialisationScheme().DeserialiseMappingSet(xml.GetXmlDocRoot(), database, entitySet);

            Assert.That(set.EntitySet, Is.SameAs(entitySet));
            Assert.That(set.Database, Is.SameAs(database));
        }
        public void Both_Should_Validate_Correcty()
        {
            var table1 = new Table { Name = "Table1"};
            var table2 = new Table { Name = "Table2"};

            var database = new Database("DB");
            database.AddTable(table1);
            database.AddTable(table2);

            var failures = new List<ValidationFailure>();
            Assert.That(table1.ValidateObject(failures), Is.True);
            Assert.That(failures, Is.Empty);
        }
        public static Database TestDatabase()
        {
            Database db = new Database("Test Database");

            var t1 = new Table("Table1");
            db.AddTable(t1);
            t1.AddColumn(new Column("Column1") { InPrimaryKey = true, Datatype = "int", OrdinalPosition = 0, Size = 4});
            t1.AddColumn(new Column("Column2") { Datatype = "nvarchar", OrdinalPosition = 1, Size = 100 });
            t1.AddColumn(new Column("Column3") { Datatype = "datetime", OrdinalPosition = 2, Size = 8 });

            t1.AddIndex(new Index("PK_Table1") { IsUnique = true, Datatype = DatabaseIndexType.PrimaryKey}).AddColumn("Column1");
            t1.AddKey(new Key("PK_Table1", DatabaseKeyType.Primary)).AddColumn("Column1").AddColumn("Column2");

            return db;
        }
        public IDatabase Deserialise(string databaseXML)
        {
            // ReSharper disable PossibleNullReferenceException
            log.Info("Loading database XML");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(databaseXML);

            XmlElement root = doc.DocumentElement;
            string databaseName = root.SelectSingleNode("DatabaseName").InnerText;
            string databaseType = root.SelectSingleNode("DatabaseType").InnerText;
            DatabaseTypes dbType = (DatabaseTypes)Enum.Parse(typeof(DatabaseTypes), databaseType, true);
            log.InfoFormat("Processing database XML (DB Name = {0})", databaseName);
            Database database = new Database(databaseName, dbType);

            var connectionInfoNode = root.SelectSingleNode("ConnectionInformation");

            if (connectionInfoNode != null)
            {
                database.Loader = DeserialiseConnectionInformation(connectionInfoNode, databaseName);
                database.ConnectionInformation = database.Loader.DatabaseConnector.ConnectionInformation;
            }
            XmlNodeList tableNodes = root.SelectNodes("Table");
            XmlNodeList viewNodes = root.SelectNodes("View");
            XmlNodeList relationshipNodes = root.SelectNodes("Relationship");

            // The process:
            // 1) Deserialise all Tables. Don't do their columns/keys/indexes yet.
            // 2) Deserialise the Columns from each Table node. Add them to their parent Tables.
            // 3) Deserialise Indexes, add them to their parent Tables.
            // 4) Deserialise Keys.
            // 5) Deserialise Relationships

            DeserialiseTables(database, tableNodes);
            DeserialiseColumns(database, tableNodes);
            DeserialiseIndexes(database, tableNodes);
            DeserialiseKeys(database, tableNodes);

            DeserialiseViews(database, viewNodes);
            DeserialiseViewColumns(database, viewNodes);
            DeserialiseViewIndexes(database, viewNodes);
            DeserialiseViewKeys(database, viewNodes);

            DeserialiseRelationships(database, relationshipNodes);

            return database;

            // ReSharper restore PossibleNullReferenceException
        }
        private void buttonRunHBM2ModelTransform_Click(object sender, RoutedEventArgs args)
        {
            var loader = new EntityLoader(new FileController());
            var database = new Database("DB1", ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes.SQLServer2005);
            var table = new Table("TableName", "");
            table.AddColumn(new Column("ID"));
            table.AddColumn(new Column("Speed"));
            table.AddColumn(new Column("Name"));
            table.AddColumn(new Column("Speed"));
            table.AddColumn(new Column("Discriminator"));
            table.AddColumn(new Column("Car_Make"));
            table.AddColumn(new Column("Bike_Model"));

            database.AddTable(table);
            loader.GetEntities(new[] {"nhtest.xml"}, database);
        }
        public void Should_Return_This()
        {
            const string expectedXML = @"<LogicalSchema Version=""1""><DatabaseName>Database1</DatabaseName>" +
                        "<Table><Description /><Enabled>True</Enabled><IsUserDefined>False</IsUserDefined><Name>Table1</Name><UID>00000000-0000-0000-0000-000000000000</UID></Table>" +
                        "<Table><Description /><Enabled>True</Enabled><IsUserDefined>False</IsUserDefined><Name>Table2</Name><UID>00000000-0000-0000-0000-000000000000</UID></Table>" +
                        "</LogicalSchema>";

            Database db1 = new Database("Database1");
            db1.AddTable(new Table("Table1"));
            db1.AddTable(new Table("Table2"));

            string outputXml = db1.Serialise(new DatabaseSerialisationScheme());
            outputXml = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXml);

            Assert.That(outputXml, Is.EqualTo(expectedXML));
        }
        public void Setup()
        {
            database = new Database("Db1");
            entitySet = new EntitySetImpl();

            table = new Table("Table1");
            table.AddColumn(new Column("Column1"));
            entity1 = new EntityImpl("Entity1");
            entity1.AddProperty(new PropertyImpl("Property1"));
            entity2 = new EntityImpl("Entity2");

            entity2.AddProperty(new PropertyImpl("Property2"));
            var reference = entity1.CreateReferenceTo(entity2);
            reference.Identifier = new Guid("11111111-1111-1111-1111-111111111111");
            reference.End1Name = "end1";
            reference.End2Name = "end2";
            entitySet.AddReference(reference);

            database.AddTable(table);
            entitySet.AddEntity(entity1);
        }
        public void It_Should_Be_Reconstructed_Correctly()
        {
            const string xml = When_Serialising_Key_With_A_Referenced_Key.KeyWithReferencedKeyXml;
            XmlDocument document = new XmlDocument();
            document.LoadXml(xml);

            Database db = new Database("Database1");
            Table table1 = new Table("Table1");
            Table table2 = new Table("Table2");
            db.AddTable(table1);
            db.AddTable(table2);

            IKey key2 = new Key("ForeignKey") { Parent = table2 };
            table2.AddKey(key2);

            var scheme = new DatabaseDeserialisationScheme();
            IKey key = scheme.ProcessKeyNode(document.DocumentElement, table1, db);
            scheme.ProcessReferencedKey(key, document.DocumentElement, db);

            Assert.That(key.ReferencedKey, Is.SameAs(key2));
        }
        public void It_Should_Be_Reconstructed_Correctly()
        {
            IDatabase database = new Database("DB1");
            var table1 = new Table("Table1");
            var table2 = new Table("Table2");
            var key1 = new Key("K1", DatabaseKeyType.Primary);
            var key2 = new Key("ForeignKey", DatabaseKeyType.Foreign);

            table1.AddKey(key1);
            table2.AddKey(key2);

            database.AddTable(table1);
            database.AddTable(table2);

            Relationship rel = new DatabaseDeserialisationScheme().DeserialiseRelationship(FullRelationshipXml.GetXmlDocRoot(), database);

            Assert.That(rel.Identifier, Is.EqualTo(new Guid("11111111-1111-1111-1111-111111111111")));
            Assert.That(rel.Name, Is.EqualTo("Relation1"));
            Assert.That(rel.PrimaryTable, Is.SameAs(table1));
            Assert.That(rel.ForeignTable, Is.SameAs(table2));
            Assert.That(rel.PrimaryKey, Is.SameAs(key1));
            Assert.That(rel.ForeignKey, Is.SameAs(key2));
        }
        public void It_Should_Serialise_To_This()
        {
            XmlNode root = When_Serialising_A_Discriminator_With_One_Condition.FullDiscriminatorXml.GetXmlDocRoot();
            Entity entity = new EntityImpl("Entity1");

            var column = new Column("Column1");
            Table table = new Table("Table1");
            table.AddColumn(column);
            var database = new Database("db1");
            database.AddTable(table);

            var dis = new EntitySetDeserialisationScheme().DeserialiseDiscriminator(root, database);

            Assert.That(dis, Is.Not.Null);
            Assert.That(dis.RootGrouping, Is.Not.Null);
            Assert.That(dis.RootGrouping.ContainsConditions, Is.True);
            Assert.That(dis.RootGrouping.ContainsGroupings, Is.False);

            var condition = dis.RootGrouping.Conditions.ElementAt(0);

            Assert.That(condition.Column, Is.SameAs(column));
            Assert.That(condition.Operator, Is.SameAs(Operator.Equal));
            Assert.That(condition.ExpressionValue.Value, Is.EqualTo("5"));
        }
 private static IKey GetKey()
 {
     IDatabase db = new Database("DB1");
     Table table = new Table("Table2");
     table.Database = db;
     IKey key = new Key("KeyV");
     key.Parent = table;
     return key;
 }
        public void The_DatabaseName_Is_Wired_Up_Correctly()
        {
            IDatabaseForm form = MockRepository.GenerateMock<IDatabaseForm>();
            Database obj = new Database("Database1");
            obj.Loader = DatabaseLoaderFacade.GetSQLCELoader("asdfsdf.sdf");
            IMainPanel panel = MockRepository.GenerateMock<IMainPanel>();

            var presenter = new DatabasePresenter(panel, form);
            presenter.AttachToModel(obj);

            form.AssertWasCalled(a => a.SelectedDatabaseType = DatabaseTypes.SQLCE);
            form.AssertWasCalled(a => a.SetDatabaseFilename("asdfsdf.sdf"));
            form.AssertWasCalled(a => a.DatabaseHelper = Arg<IServerAndDatabaseHelper>.Is.NotNull);
        }
        public void Setup()
        {
            // Setup Database
            database = new Database("DB1");
            var table = new Table("User");
            table.AddColumn(new Column("Name"));
            table.AddColumn(new Column("AddressStreet"));
            table.AddColumn(new Column("AddressCity"));
            table.AddColumn(new Column("AddressCountry"));
            database.AddTable(table);

            // Setup Entities
            entitySet = new EntitySetImpl();
            Entity userEntity = new EntityImpl("User");
            userEntity.AddProperty(new PropertyImpl("Name"));

            // Create the Address type
            spec = new ComponentSpecificationImpl("Address");

            spec.AddProperty(new ComponentPropertyImpl("Street"));
            spec.AddProperty(new ComponentPropertyImpl("City"));
            spec.AddProperty(new ComponentPropertyImpl("Country"));

            // Create the Address component for the User entity.
            component = spec.CreateImplementedComponentFor(userEntity, "HomeAddress");

            entitySet.AddEntity(userEntity);
            entitySet.AddComponentSpecification(spec);

            // Setup the Mappings
            mappingSet = new MappingSetImpl(database, entitySet);
            componentMapping = new ComponentMappingImpl();
            mappingSet.AddMapping(componentMapping);

            componentMapping.AddPropertyAndColumn(component.Properties[0], table.Columns[1]);
            componentMapping.AddPropertyAndColumn(component.Properties[1], table.Columns[2]);
            componentMapping.AddPropertyAndColumn(component.Properties[2], table.Columns[3]);

            // Add the mapping between the Name property and the Name column in the database table.
            mappingSet.ChangeMappedColumnFor(userEntity.ConcreteProperties[0]).To(table.Columns[0]);
        }
Example #21
0
        protected static void Generic_Returns_Correct_Data(Database db, string databaseName, IDatabaseLoader loader)
        {
            Assert.That(db, Is.Not.Null);
            Assert.That(db.Tables, Is.Not.Null);
            Assert.That(db.Tables, Has.Count(1));
            Assert.That(db.Name, Is.EqualTo(databaseName));
            Assert.That(db.Loader, Is.SameAs(loader));

            var table = db.Tables[0];

            Assert.That(table, Is.Not.Null);
            Assert.That(table.Database, Is.SameAs(db));
            Assert.That(table.Name, Is.EqualTo("Table1"));
            Assert.That(table.Columns, Is.Not.Null);
            Assert.That(table.Columns, Has.Count(3));

            var column = table.Columns[0];

            Assert.That(column, Is.Not.Null);
            Assert.That(column.Parent, Is.SameAs(table));
            Assert.That(column.Name, Is.EqualTo("Column1"));
            Assert.That(column.Datatype, Is.EqualTo("int"));
            Assert.That(column.IsIdentity, Is.True, "Column1.IsIdentity should be true");
            Assert.That(column.InPrimaryKey, Is.True);
            Assert.That(column.IsNullable, Is.False);
            Assert.That(column.IsReadOnly, Is.False);
            Assert.That(column.IsUnique, Is.True);
            Assert.That(column.IsUserDefined, Is.False);
            Assert.That(column.OrdinalPosition, Is.EqualTo(1));
            Assert.That(column.Default, Is.EqualTo(""));

            column = table.Columns[1];

            Assert.That(column, Is.Not.Null);
            Assert.That(column.Parent, Is.SameAs(table));
            Assert.That(column.Name, Is.EqualTo("Column2"));
            Assert.That(column.Datatype, Is.EqualTo("nvarchar"));
            Assert.That(column.InPrimaryKey, Is.False);
            Assert.That(column.IsIdentity, Is.False);
            Assert.That(column.IsNullable, Is.True);
            Assert.That(column.IsReadOnly, Is.False);
            Assert.That(column.IsUnique, Is.False);
            Assert.That(column.IsUserDefined, Is.False);
            Assert.That(column.OrdinalPosition, Is.EqualTo(2));
            Assert.That(column.Default, Is.EqualTo(""));

            column = table.Columns[2];

            Assert.That(column, Is.Not.Null);
            Assert.That(column.Parent, Is.SameAs(table));
            Assert.That(column.Name, Is.EqualTo("Column3"));
            Assert.That(column.Datatype, Is.EqualTo("datetime"));
            Assert.That(column.InPrimaryKey, Is.False);
            Assert.That(column.IsIdentity, Is.False);
            Assert.That(column.IsNullable, Is.False);
            Assert.That(column.IsReadOnly, Is.False);
            Assert.That(column.IsUnique, Is.False);
            Assert.That(column.IsUserDefined, Is.False);
            Assert.That(column.OrdinalPosition, Is.EqualTo(3));
            Assert.That(column.Default, Is.EqualTo(""));

            IIndex index = table.Indexes[0];

            Assert.That(index, Is.Not.Null);
            Assert.That(index.Parent, Is.SameAs(table));
            Assert.That(index.Name, Is.EqualTo("PK_Table1"));
            Assert.That(index.Datatype, Is.EqualTo(DatabaseIndexType.PrimaryKey));
            Assert.That(index.IsClustered, Is.False);
            Assert.That(index.IsUnique, Is.True);
            Assert.That(index.IsUserDefined, Is.False);
            Assert.That(index.Columns, Is.Not.Null);
            Assert.That(index.Columns[0].Name, Is.EqualTo("Column1"));
            Assert.That(index.Columns[0].Parent, Is.EqualTo(table));

            index = table.Indexes[1];

            Assert.That(index, Is.Not.Null);
            Assert.That(index.Parent, Is.SameAs(table));
            Assert.That(index.Name, Is.EqualTo("UQ__Table1__000000000000000C"));
            Assert.That(index.Datatype, Is.EqualTo(DatabaseIndexType.Unique));
            Assert.That(index.IsClustered, Is.False);
            Assert.That(index.IsUnique, Is.True);
            Assert.That(index.IsUserDefined, Is.False);
            Assert.That(index.Columns, Is.Not.Null);
            Assert.That(index.Columns[0].Name, Is.EqualTo("Column1"));
            Assert.That(index.Columns[0].Parent, Is.EqualTo(table));
        }
        private MappingSet CreateMappingSet_OneTable()
        {
            Database database = new Database("DB1");
            var table = new Table("Table1");

            table.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true });
            table.AddColumn(new Column("Column2") { Datatype = "nvarchar", Size = 50 });

            database.AddTable(table);
            MappingProcessor processor = new MappingProcessor(new OneToOneEntityProcessor());
            return processor.CreateOneToOneMapping(database);
        }
        public void Both_Should_Not_Validate()
        {
            var table1 = new Table { Name = "Table"};
            var table2 = new Table { Name = "Table"};

            var database = new Database("DB");
            database.AddTable(table1);
            database.AddTable(table2);

            ValidateTable(table1);
            ValidateTable(table2);
        }
        public void SetUp()
        {
            loader = new EntityLoader(MockRepository.GenerateStub<IFileController>());

            database = new Database("DB1");
            table = new Table("Transport");
            table.AddColumn(new Column("ID") { Datatype = "int" });
            table.AddColumn(new Column("Discriminator") { Datatype = "char", Size = 1});
            table.AddColumn(new Column("Name") { Datatype = "varchar", Size = 100 });
            table.AddColumn(new Column("Bike_Code") { Datatype = "varchar", Size = 5 });
            database.AddEntity(table);

            // Call we are testing
            mappingSet = loader.GetEntities(new[] { Path.Combine("Resources", "TablePerClassHierarchy.hbm.xml") }, database);
        }
        public Database LoadDatabase(List<SchemaData> databaseObjectsToFetch, List<string> allowedSchemas)
        {
            try
            {
                connector.Open();
            }
            catch (Exception e)
            {
                throw new DatabaseLoaderException("Error opening database connection. See inner exception.", e);
            }
            try
            {
                var db = new Database(connector.DatabaseName, DatabaseTypes.MySQL) { Loader = this };
                db.ConnectionInformation = DatabaseConnector.ConnectionInformation;
                SetSchemaFilter(databaseObjectsToFetch);

                foreach (var table in GetTables(databaseObjectsToFetch))
                    db.AddTable(table);

                foreach (var view in GetViews(databaseObjectsToFetch))
                    db.AddView(view);

                GetForeignKeys(db);
                return db;
            }
            catch (Exception e)
            {
                throw new DatabaseLoaderException("Error opening database connection. See inner exception.", e);
            }
            finally
            {
                connector.Close();
            }
        }
        private MappingSet CreateMappingSet_TwoTables()
        {
            Database database = new Database("DB1");
            var table1 = new Table("Table1");
            table1.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true });

            var table2 = new Table("Table2");
            table2.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true });

            database.AddTable(table1);
            database.AddTable(table2);
            MappingProcessor processor = new MappingProcessor(new OneToOneEntityProcessor());
            return processor.CreateOneToOneMapping(database);
        }
 private static IIndex GetIndex()
 {
     IDatabase db = new Database("DB1");
     Table table = new Table("Table2");
     table.Database = db;
     Index index = new Index("Index1");
     index.Parent = table;
     return index;
 }
        public void SetUp()
        {
            Table1 = new Table("Table1");
            Table2 = new Table("Table2");
            Table3 = new Table("Table3");

            Database = new Database("DB1");
            Database.AddEntity(Table1);
            Database.AddEntity(Table2);
            Database.AddEntity(Table3);

            Relationship12 = Table1.CreateRelationshipTo(Table2);
            Relationship13 = Table1.CreateRelationshipTo(Table3);

            Set = new MappingSetImpl(Database, new EntitySetImpl());
        }
        public void Removed()
        {
            IDatabase db = new Database("DB1");
            Table table = new Table("Table2");
            table.Database = db;

            IMergeOperation<ITable> op = new TableRemovalOperation(table);

            StringAssert.Contains("DB1.Table2 Removed", op.ToString());
        }
        public void Addition()
        {
            IDatabase db = new Database("DB1");
            Table table = new Table("Table2");
            IMergeOperation<ITable> op = new TableAdditionOperation(db, table);

            Assert.That(op.ToString(), Is.EqualTo("DB1.Table2 Added"));
        }