public void TestGenerateScript()
        {
            var p = new MSSQLProvider(GetTestDB(), null);

            var sv = p.GetSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel();

            Assert.AreEqual(28, model.GetActiveEntities().Count());
            Assert.AreEqual(32, model.GetSourceFragments().Count());

            var msc = new ModelToSourceConnector(new SourceView(), model);

            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script));

            Assert.AreEqual(32, new Regex("CREATE TABLE ").Matches(script).Count);

            Console.WriteLine(script);
        }
        public void TestGenerateComplex()
        {
            SourceView sv = TestsSourceModel.TestsSourceModel.CreateComplexSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel();

            Assert.AreEqual(2, model.GetActiveEntities().Count());
            Assert.AreEqual(4, model.GetActiveRelations().Count());

            model.GetActiveRelations().First().Constraint = RelationConstraint.Unique;

            var msc = new ModelToSourceConnector(new SourceView(), model);

            var    p      = new MSSQLProvider(null, null);
            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script));
            Console.WriteLine(script);

            Assert.AreEqual(6, new Regex("CREATE TABLE ").Matches(script).Count);

            Assert.AreEqual(2, new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count);

            Assert.AreEqual(8, new Regex("FOREIGN KEY").Matches(script).Count);

            Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count);
        }
        public void TestGenerateScriptDropConstraint()
        {
            var p = new MSSQLProvider(GetTestDB(), null);

            var sv = p.GetSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false);

            Assert.AreEqual(28, model.GetActiveEntities().Count());
            Assert.AreEqual(32, model.GetSourceFragments().Count());

            EntityPropertyDefinition prop = model.GetActiveEntities().SelectMany(item => item.GetProperties().OfType <EntityPropertyDefinition>()).First();
            SourceConstraint         c    = new SourceConstraint(SourceConstraint.UniqueConstraintTypeName, "xxx");

            c.SourceFields.AddRange(prop.SourceFields.Cast <SourceFieldDefinition>());
            prop.SourceFragment.Constraints.Add(c);

            var msc = new ModelToSourceConnector(p.GetSourceView(), model);
            var tbl = msc.SourceView.GetSourceFragments().Single(item => item.Selector == prop.SourceFragment.Selector &&
                                                                 item.Name == prop.SourceFragment.Name);

            tbl.Constraints.Add(new SourceConstraint(SourceConstraint.UniqueConstraintTypeName, "xxx"));

            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script), script);

            Assert.AreEqual(1, new Regex("DROP CONSTRAINT").Matches(script).Count);
        }
        public void TestGenerateScriptHierachy()
        {
            var p = new MSSQLProvider(GetTestDB(), null);

            var sv = p.GetSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false);

            Assert.AreEqual(28, model.GetActiveEntities().Count());
            Assert.AreEqual(32, model.GetSourceFragments().Count());

            var msc = new ModelToSourceConnector(new SourceView(), model);

            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script));
            Console.WriteLine(script);

            Assert.AreEqual(sv.GetSourceFragments().Count(), new Regex("CREATE TABLE ").Matches(script).Count);
            IEnumerable <SourceConstraint> pks = sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName));

            Assert.AreEqual(pks.Count(), new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count);
            Assert.AreEqual(2, new Regex("UNIQUE NONCLUSTERED").Matches(script).Count);
            Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count);
            Assert.AreEqual(sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)).Count(), new Regex("FOREIGN KEY").Matches(script).Count);

            msc = new ModelToSourceConnector(sv, model);

            script = msc.GenerateSourceScript(p, false);

            Assert.IsTrue(string.IsNullOrEmpty(script), script);

            RelationDefinitionBase r = model.GetActiveRelations().First(item => item.Constraint == RelationConstraint.PrimaryKey);

            r.Constraint = RelationConstraint.Unique;
            r.SourceFragment.Constraints.Single(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName).ConstraintType = SourceConstraint.UniqueConstraintTypeName;

            msc = new ModelToSourceConnector(sv, model);

            script = msc.GenerateSourceScript(p, false);

            Assert.IsTrue(string.IsNullOrEmpty(script), script);
        }
Example #5
0
        public void TestAlter()
        {
            var p = new MSSQLProvider(GetTestDB(), null);

            var sv = p.GetSourceView();

            var model = new WXMLModel();

            var smc = new SourceToModelConnector(sv, model);

            smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false);

            Assert.AreEqual(28, model.GetActiveEntities().Count());
            Assert.AreEqual(32, model.GetSourceFragments().Count());

            var msc = new ModelToSourceConnector(new SourceView(), model);

            foreach (SourceFragmentDefinition sf in sv.GetSourceFragments().ToArray())
            {
                foreach (SourceFieldDefinition field in sv.GetSourceFields(sf).Where(item => !item.IsFK))
                {
                    msc.SourceView.SourceFields.Add(new SourceFieldDefinition(new SourceFragmentDefinition(field.SourceFragment.Identifier,
                                                                                                           field.SourceFragment.Name, field.SourceFragment.Selector), field.SourceFieldExpression, field.SourceType, field.SourceTypeSize, field.IsNullable, field.IsAutoIncrement, field.DefaultValue));
                    break;
                }
            }

            string script = msc.GenerateSourceScript(p, false);

            Assert.IsFalse(string.IsNullOrEmpty(script));
            Console.WriteLine(script);

            Assert.AreEqual(4, new Regex("CREATE TABLE ").Matches(script).Count);
            Assert.AreEqual(26, new Regex("ALTER TABLE ").Matches(script.Remove(script.IndexOf("--Creating primary keys"))).Count);
            IEnumerable <SourceConstraint> pks = sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName));

            Assert.AreEqual(pks.Count(), new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count);
            Assert.AreEqual(2, new Regex("UNIQUE NONCLUSTERED").Matches(script).Count);
            Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count);
            Assert.AreEqual(sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)).Count(), new Regex("FOREIGN KEY").Matches(script).Count);
        }