Пример #1
0
 private static void GenerateRelScript(SelfRelationTarget rt, StringBuilder script, bool unicodeStrings, RelationDefinitionBase rel)
 {
     for (int i = 0; i < rt.FieldName.Length; i++)
     {
         script.Append(rt.FieldName[i]).Append(" ");
         if (rel is SelfRelationDefinition)
         {
             SelfRelationDefinition   r  = rel as SelfRelationDefinition;
             ScalarPropertyDefinition sp = r.Properties.Skip(i).First();
             script.Append(GetType(sp.SourceField, sp.PropertyType, sp.Attributes, unicodeStrings)).Append(" NOT NULL");
         }
         else if (rel is RelationDefinition)
         {
             LinkTarget lt = rt as LinkTarget;
             ScalarPropertyDefinition sp = lt.Properties.Skip(i).First();
             script.Append(GetType(sp.SourceField, sp.PropertyType, sp.Attributes, unicodeStrings)).Append(" NOT NULL");
         }
         else
         {
             throw new NotSupportedException(rel.GetType().ToString());
         }
     }
 }
 public static CodeExpression GetPropertyReferenceExpression(WXMLCodeDomGeneratorSettings settings, ScalarPropertyDefinition propertyDesc, bool addNamespace)
 {
     return(new CodePropertyReferenceExpression(GetEntityClassReferenceExpression(settings, propertyDesc.Entity, addNamespace), propertyDesc.Name));
 }
 public CodeEntityPropertyField(WXMLCodeDomGeneratorSettings settings, ScalarPropertyDefinition property)
 {
     Type       = property.PropertyType.ToCodeType(settings);
     Name       = new WXMLCodeDomGeneratorNameHelper(settings).GetPrivateMemberName(property.Name);
     Attributes = WXMLCodeDomGenerator.GetMemberAttribute(property.FieldAccessLevel);
 }
Пример #4
0
        protected PropertyDefinition AppendColumn(EntityDefinition e, SourceFieldDefinition c,
                                                  out bool created, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            created = false;
            //var x = e.OwnProperties.OfType<ScalarPropertyDefinition>().Where(pd =>
            //    pd.SourceFieldExpression.Trim(']', '[') == c.SourceFieldExpression.Trim('[',']')
            //);

            var nsfn = c.SourceFieldExpression.Trim('[', ']');

            if (!caseSensitive)
            {
                nsfn = nsfn.ToLower();
            }

            var x = e.OwnProperties.Where(pd =>
            {
                ScalarPropertyDefinition sp = pd as ScalarPropertyDefinition;
                if (sp != null)
                {
                    var spsf = sp.SourceFieldExpression.Trim(']', '[');
                    if (!caseSensitive)
                    {
                        spsf = spsf.ToLower();
                    }

                    return(spsf == nsfn);
                }
                else
                {
                    EntityPropertyDefinition ep = pd as EntityPropertyDefinition;
                    if (ep != null)
                    {
                        return(ep.SourceFields.Any(item => {
                            var spsf = item.SourceFieldExpression.Trim(']', '[');
                            if (!caseSensitive)
                            {
                                spsf = spsf.ToLower();
                            }
                            return spsf == nsfn;
                        }));
                    }
                }
                return(false);
            }
                                          );

            PropertyDefinition       pe  = null;
            ScalarPropertyDefinition pes = x.OfType <ScalarPropertyDefinition>().SingleOrDefault();

            if (pes != null)
            {
                pe = pes;
            }
            else
            {
                pe = x.OfType <EntityPropertyDefinition>().SingleOrDefault();
            }

            GetSourceFragment(c.SourceFragment);

            if (pe == null)
            {
                Field2DbRelations attrs = c.GetAttributes();
                string            name  = GetName(c.SourceFieldExpression);

                if (!SourceView.GetSourceFields(c.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(name, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase)))
                {
                    name = Trim(name, transforRawNamesToReadableForm);
                }

                if (capitalizeNames)
                {
                    name = Capitalize(name);
                }

                if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && c.IsNullable)
                {
                    throw new WXMLException(string.Format("Column {0}.{1} cannot be nullable since it's a primary key", c.SourceFragment, c.SourceFieldExpression));
                }

                var t = GetClrType(c.SourceType, c.IsNullable, ref attrs);

                pe = new ScalarPropertyDefinition(e, name,
                                                  name, attrs, "Auto generated from column " + c.SourceFieldExpression, t, c,
                                                  AccessLevel.Private, AccessLevel.Public
                                                  );

                e.AddProperty(pe);
                created = true;
            }
            else
            {
                Field2DbRelations attrs = c.GetAttributes();
                if (attrs == Field2DbRelations.None)
                {
                    attrs = pe.Attributes;
                }

                ScalarPropertyDefinition sp = pe as ScalarPropertyDefinition;
                if (sp != null)
                {
                    if (!pe.PropertyType.IsUserType && (attrs & Field2DbRelations.PK) != Field2DbRelations.PK &&
                        sp.SourceType != c.SourceType)
                    {
                        pe.PropertyType = GetClrType(c.SourceType, c.IsNullable, ref attrs);
                    }

                    sp.SourceField = c;
                }

                if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && e.BaseEntity != null && e.BaseEntity.GetPkProperties().Any())
                {
                    attrs = attrs & ~Field2DbRelations.PK;
                }

                if (attrs == Field2DbRelations.None)
                {
                    attrs = pe.Attributes;
                }

                pe.Attributes = attrs;
            }
            return(pe);
        }
Пример #5
0
        protected EntityPropertyDefinition AppendFK(EntityDefinition e, SourceFragmentDefinition sf,
                                                    SourceConstraint fk, relation1to1 rb, out bool created,
                                                    bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            created = false;
            var rels = SourceView.GetFKRelations(fk);
            SourceFragmentDefinition m  = rels.First().PKField.SourceFragment;
            EntityDefinition         re = e;

            if (sf != m)
            {
                re = GetEntity(m, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
            }
            string         rid = "t" + re.Name;
            TypeDefinition td  = Model.GetType(rid, false);

            if (td == null)
            {
                td = new TypeDefinition(rid, re);
                Model.AddType(td);
            }

            string propAlias = td.Entity.Name;

            if (rels.Count() == 1)
            {
                propAlias = Trim(GetName(rels.First().FKField.SourceFieldExpression), true);
            }

            if (capitalizeNames)
            {
                propAlias = Capitalize(propAlias);
            }

            string propName = propAlias;

            EntityPropertyDefinition ep = null;

            //try
            //{
            ep = (EntityPropertyDefinition)e.OwnProperties
                 .SingleOrDefault(item => item.Identifier == propAlias);

            if (ep == null)
            {
                ep = e.OwnProperties.OfType <EntityPropertyDefinition>().SingleOrDefault(item => fk.SourceFields.All(sf2 =>
                                                                                                                     item.SourceFields.Any(sff => sf2.SourceFieldExpression.Trim('[', ']') == sff.SourceFieldExpression.Trim('[', ']'))
                                                                                                                     ));
            }

            if (ep == null)
            {
                var one2one = fk.SourceFields.All(sss => sss.IsPK);

                if (one2one && e.BaseEntity == re)
                {
                    foreach (var sfd_ in fk.SourceFields)
                    {
                        bool x;
                        var  xx = AppendColumn(e, sfd_, out x, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
                        RaiseOnPropertyCreated(xx, x);
                    }
                    return(null);
                }

                int cnt = e.OwnProperties.Count(p => p.Name == propName);
                if (cnt > 0)
                {
                    propName  = propName + cnt;
                    propAlias = propAlias + cnt;
                }

                SourceFragmentDefinition sfd = GetSourceFragment(sf);

                ep = new EntityPropertyDefinition(propName, propAlias,
                                                  Field2DbRelations.None, "Auto generated from constraint " + fk.ConstraintName,
                                                  AccessLevel.Private, AccessLevel.Public, td, sfd, e);

                e.AddProperty(ep);
                created = true;

                foreach (SourceReferences rel in rels)
                {
                    SourceFieldDefinition fld = SourceView.GetSourceFields(sf).Single(item => item.SourceFieldExpression == rel.FKField.SourceFieldExpression);

                    ScalarPropertyDefinition pk = re.GetPkProperties().SingleOrDefault(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression);
                    if (pk == null)
                    {
                        if (rel.PKConstraint.ConstraintType != SourceConstraint.UniqueConstraintTypeName)
                        {
                            throw new WXMLException(string.Format("Cannot find pk for constraint {0}", rel.PKConstraint.ConstraintName));
                        }

                        pk = re.GetProperties().OfType <ScalarPropertyDefinition>().Single(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression);
                    }

                    ep.AddSourceField(pk.PropertyAlias,
                                      fld.SourceFieldExpression, null, fld.SourceType, fld.SourceTypeSize, fld.IsNullable, fld.DefaultValue
                                      );
                }
            }
            else
            {
                if (ep.Description == "Auto generated from constraint " + fk.ConstraintName)
                {
                    ep.PropertyType = td;
                }
            }

            foreach (SourceFieldDefinition pkField in SourceView.GetFKRelations(fk)
                     .Select(item => item.FKField)
                     .Where(item => item.IsPK))
            {
                string pkPropAlias = GetName(pkField.SourceFieldExpression);

                if (!SourceView.GetSourceFields(pkField.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(pkPropAlias, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase)))
                {
                    pkPropAlias = Trim(pkPropAlias, transforRawNamesToReadableForm);
                }

                //string pkPropAlias = Trim(GetName(pkField.SourceFieldExpression), transforRawNamesToReadableForm);
                if (capitalizeNames)
                {
                    pkPropAlias = Capitalize(pkPropAlias);
                }

                string             pkPropName = pkPropAlias;
                PropertyDefinition pe         = e.OwnProperties
                                                .SingleOrDefault(pd => pd.Identifier == pkPropAlias);
                Field2DbRelations attrs  = pkField.GetAttributes();
                TypeDefinition    pkType = GetClrType(pkField.SourceType, pkField.IsNullable, ref attrs);
                bool pkCreated           = pe == null;
                if (pkCreated)
                {
                    int cnt = e.OwnProperties.Count(p => p.Name == pkPropName);
                    if (cnt > 0)
                    {
                        pkPropName = pkPropName + cnt;
                        //pkPropAlias = pkPropAlias + cnt;
                    }

                    pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs,
                                                      "Auto generated from column " + pkField.SourceFieldExpression,
                                                      pkType, pkField, AccessLevel.Private, AccessLevel.Public);

                    e.AddProperty(pe);
                }
                else
                {
l1:
                    if (pe is ScalarPropertyDefinition)
                    {
                        pe.Attributes  |= attrs;
                        pe.PropertyType = pkType;
                        ((ScalarPropertyDefinition)pe).SourceField = pkField;
                    }
                    else
                    {
                        int cnt = e.OwnProperties.Count(p => p.Identifier == pkPropAlias);
                        if (cnt > 0)
                        {
                            if (e.OwnProperties.Any(item => item.Identifier == GetName(pkField.SourceFieldExpression)))
                            {
                                pkPropAlias = pkPropAlias + cnt;
                            }
                            else
                            {
                                pkPropAlias = GetName(pkField.SourceFieldExpression);
                            }
                        }
                        pkPropName = pkPropAlias;

                        pe = e.OwnProperties.SingleOrDefault(item => item.PropertyAlias == pkPropAlias);
                        if (pe != null)
                        {
                            if (pe is EntityPropertyDefinition)
                            {
                                throw new WXMLParserException(string.Format("Property {0} expected to be of type ScalarPropertyDefinition", pe.Identifier));
                            }
                            goto l1;
                        }

                        pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs,
                                                          "Auto generated from column " + pkField.SourceFieldExpression,
                                                          pkType, pkField, AccessLevel.Private, AccessLevel.Public);

                        e.AddProperty(pe);
                    }
                }
                RaiseOnPropertyCreated(pe, pkCreated);
            }
            //}
            //catch
            //{
            //    int i = 10;
            //}
            return(ep);
        }
Пример #6
0
        public void ApplySourceViewToModel(bool dropProperties, relation1to1 rb,
                                           bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            _tables2skip   = new List <SourceFragmentDefinition>();
            _entities2skip = new Dictionary <string, EntityDefinition>();

            foreach (SourceFragmentDefinition sf in SourceView.GetSourceFragments())
            {
                if (_tables2skip.Contains(sf))
                {
                    continue;
                }

                if (sf.Constraints.Count(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 2 &&
                    SourceView.GetSourceFields(sf).All(clm => clm.IsFK))
                {
                    continue;
                }

                GetEntity(sf, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
            }

            //Dictionary<string, EntityDefinition> dic = Process1to1Relations(columns, defferedCols, odef, escape, notFound, rb);

            ProcessMany2Many();

            if (dropProperties)
            {
                foreach (EntityDefinition ed in Model.GetActiveEntities())
                {
                    foreach (PropertyDefinition pd in ed.GetActiveProperties()
                             .Where(item =>
                                    !string.IsNullOrEmpty(item.Description) &&
                                    item.Description.StartsWith("Auto generated from ", StringComparison.Ordinal)
                                    ).ToArray()
                             )
                    {
                        var sourceFields = SourceView.GetSourceFields(pd.SourceFragment);

                        if (pd is ScalarPropertyDefinition)
                        {
                            ScalarPropertyDefinition p = pd as ScalarPropertyDefinition;
                            if (!sourceFields
                                .Any(item => (
                                         caseSensitive?Trim(
                                             (capitalizeNames ? Capitalize(item.SourceFieldExpression) : item.SourceFieldExpression),
                                             transforRawNamesToReadableForm
                                             ):item.SourceFieldExpression.ToLower()
                                         ) == (
                                         caseSensitive?p.SourceFieldExpression:p.SourceFieldExpression.ToLower()
                                         )))
                            {
                                ed.RemoveProperty(pd);
                                RaiseOnPropertyRemoved(pd);
                            }
                        }
                        else if (pd is EntityPropertyDefinition)
                        {
                            EntityPropertyDefinition p = pd as EntityPropertyDefinition;
                            foreach (EntityPropertyDefinition.SourceField field in p.SourceFields.ToArray())
                            {
                                if (!sourceFields.Any(item =>
                                                      (caseSensitive?Trim(capitalizeNames ? Capitalize(item.SourceFieldExpression)
                                        : item.SourceFieldExpression, transforRawNamesToReadableForm):item.SourceFieldExpression.ToLower()) == (caseSensitive?field.SourceFieldExpression:field.SourceFieldExpression.ToLower())))
                                {
                                    p.RemoveSourceField(field);
                                }
                            }
                            if (p.SourceFields.Count() == 0)
                            {
                                ed.RemoveProperty(pd);
                                RaiseOnPropertyRemoved(pd);
                            }
                        }
                        else
                        {
                            throw new NotSupportedException(pd.GetType().ToString());
                        }
                    }
                }
            }

            ProcessOne2Many();
        }
Пример #7
0
        public void TestAlterProperty()
        {
            using (Stream stream = Resources.GetXmlDocumentStream("suppressed"))
            {
                Assert.IsNotNull(stream);

                WXMLModel model = WXMLModel.LoadFromXml(new XmlTextReader(stream));

                Assert.IsNotNull(model);

                EntityDefinition entity = model.GetActiveEntities().Single(item => item.Identifier == "e1");

                Assert.IsNotNull(entity);

                Assert.AreEqual(2, entity.GetActiveProperties().Count());

                var p = entity.GetActiveProperties().SingleOrDefault(item => item.PropertyAlias == "Prop1");
                Assert.IsNotNull(p);
                Assert.IsFalse(p.FromBase);
                Assert.AreEqual("Prop1", p.Name);

                WXMLModel newModel = new WXMLModel();

                ScalarPropertyDefinition oldProp = ((ScalarPropertyDefinition)p).Clone();

                Assert.IsFalse(oldProp.FromBase);

                TypeDefinition newType = new TypeDefinition("tInt16", typeof(short));

                newModel.AddType(newType);

                EntityDefinition newEntity = new EntityDefinition(entity.Identifier, entity.Name, entity.Namespace, entity.Description, newModel);

                //newModel.AddEntity(newEntity);

                ScalarPropertyDefinition newProp = new ScalarPropertyDefinition(newEntity, "Prop2")
                {
                    PropertyAlias = "Prop1",
                    PropertyType  = newType
                };

                newEntity.AddProperty(newProp);

                model.Merge(Normalize(newModel));

                Assert.AreEqual(2, entity.GetActiveProperties().Count());

                ScalarPropertyDefinition renewProp = (ScalarPropertyDefinition)entity.GetActiveProperties()
                                                     .Single(item => item.PropertyAlias == "Prop1");

                Assert.AreEqual("Prop2", renewProp.Name);

                Assert.AreEqual(oldProp.DefferedLoadGroup, renewProp.DefferedLoadGroup);
                Assert.AreEqual(oldProp.AvailableFrom, renewProp.AvailableFrom);
                Assert.AreEqual(oldProp.AvailableTo, renewProp.AvailableTo);
                Assert.AreEqual(oldProp.Description, renewProp.Description);
                Assert.AreEqual(oldProp.Disabled, renewProp.Disabled);
                Assert.AreEqual(oldProp.EnablePropertyChanged, renewProp.EnablePropertyChanged);
                Assert.AreEqual(oldProp.FieldAccessLevel, renewProp.FieldAccessLevel);
                Assert.AreEqual(oldProp.FromBase, renewProp.FromBase);
                Assert.AreEqual(oldProp.Group, renewProp.Group);
                Assert.AreEqual(oldProp.IsSuppressed, renewProp.IsSuppressed);
                Assert.AreEqual(oldProp.Obsolete, renewProp.Obsolete);
                Assert.AreEqual(oldProp.ObsoleteDescripton, renewProp.ObsoleteDescripton);
                Assert.AreEqual(oldProp.PropertyAccessLevel, renewProp.PropertyAccessLevel);
                Assert.AreEqual(oldProp.PropertyAlias, renewProp.PropertyAlias);
                Assert.AreEqual(oldProp.SourceFragment, renewProp.SourceFragment);

                Assert.AreEqual(oldProp.SourceType, renewProp.SourceType);
                Assert.AreEqual(oldProp.IsNullable, renewProp.IsNullable);
                Assert.AreEqual(oldProp.SourceTypeSize, renewProp.SourceTypeSize);
                Assert.AreEqual(oldProp.SourceFieldAlias, renewProp.SourceFieldAlias);
                Assert.AreEqual(oldProp.SourceFieldExpression, renewProp.SourceFieldExpression);
            }
        }
Пример #8
0
        public void TestFillProperties()
        {
            Worm_CodeGen_Core_OrmXmlParserAccessor parser;

            using (XmlReader rdr = XmlReader.Create(GetSampleFileStream()))
            {
                object privateParser = Worm_CodeGen_Core_OrmXmlParserAccessor.CreatePrivate(rdr);
                parser = new Worm_CodeGen_Core_OrmXmlParserAccessor(privateParser);
                parser.Read();
            }

            parser.FillSourceFragments();
            parser.FindEntities();
            parser.FillImports();
            parser.FillTypes();


            WXMLModel ormObjectDef = parser.Model;

            EntityDefinition entity = ormObjectDef.GetEntities()
                                      .Single(match => match.Identifier == "eArtist" && match.Name == "Artist");

            parser.FillProperties(entity);

            Assert.AreEqual <int>(8, entity.OwnProperties.Count());

            ScalarPropertyDefinition prop = (ScalarPropertyDefinition)entity.GetProperty("ID");

            Assert.IsNotNull(prop);
            //Assert.AreEqual<int>(1, prop.Attributes.Length, "Attributes is undefined");
            Assert.AreEqual <string>("PK", prop.Attributes.ToString(), "Attributes is not correct defined");
            Assert.IsNotNull(prop.SourceFragment, "Table is undefined");
            Assert.AreEqual <string>("tblArtists", prop.SourceFragment.Identifier, "Table.Identifier is undefined");
            Assert.AreEqual <string>("id", prop.SourceFieldExpression, "FieldName is undefined");
            Assert.AreEqual <string>("System.Int32", prop.PropertyType.GetTypeName(null), "PropertyTypeString is undefined");
            Assert.AreEqual <string>("Property ID Description", prop.Description, "Description is undefined");
            Assert.AreEqual <AccessLevel>(AccessLevel.Private, prop.FieldAccessLevel, "FieldAccessLevel");
            Assert.AreEqual <AccessLevel>(AccessLevel.Public, prop.PropertyAccessLevel, "PropertyAccessLevel");
            Assert.AreEqual <string>(prop.Name, prop.PropertyAlias, "PropertyAlias");

            prop = (ScalarPropertyDefinition)entity.GetProperty("Title");
            Assert.IsNotNull(prop);
            //Assert.AreEqual<int>(0, prop.Attributes.Length, "Attributes is undefined");
            Assert.IsNotNull(prop.SourceFragment, "Table is undefined");
            Assert.AreEqual <string>("tblArtists", prop.SourceFragment.Identifier, "Table.Identifier is undefined");
            Assert.AreEqual <string>("name", prop.SourceFieldExpression, "FieldName is undefined");
            Assert.AreEqual <string>("System.String", prop.PropertyType.GetTypeName(null), "PropertyTypeString is undefined");
            Assert.AreEqual <string>("Property Title Description", prop.Description, "Description");
            Assert.AreEqual <AccessLevel>(AccessLevel.Private, prop.FieldAccessLevel, "FieldAccessLevel");
            Assert.AreEqual <AccessLevel>(AccessLevel.Assembly, prop.PropertyAccessLevel, "PropertyAccessLevel");
            Assert.AreEqual <string>(prop.Name, prop.PropertyAlias, "PropertyAlias");

            prop = (ScalarPropertyDefinition)entity.GetProperty("DisplayTitle");
            Assert.IsNull(prop);
            prop = (ScalarPropertyDefinition)entity.GetProperty("DisplayName");
            Assert.IsNotNull(prop);
            Assert.AreEqual <string>("DisplayTitle", prop.Name, "Name is undefined");
            //Assert.AreEqual<int>(0, prop.Attributes.Length, "Attributes is undefined");
            Assert.IsNotNull(prop.SourceFragment, "Table is undefined");
            Assert.AreEqual <string>("tblArtists", prop.SourceFragment.Identifier, "Table.Identifier is undefined");
            Assert.AreEqual <string>("display_name", prop.SourceFieldExpression, "FieldName is undefined");
            Assert.AreEqual <string>("System.String", prop.PropertyType.GetTypeName(null), "PropertyTypeString is undefined");
            Assert.AreEqual <string>("Property Title Description", prop.Description, "Property Title Description");
            Assert.AreEqual <AccessLevel>(AccessLevel.Family, prop.FieldAccessLevel, "FieldAccessLevel");
            Assert.AreEqual <AccessLevel>(AccessLevel.Family, prop.PropertyAccessLevel, "PropertyAccessLevel");
            Assert.AreEqual <string>("DisplayName", prop.PropertyAlias, "PropertyAlias");

            prop = (ScalarPropertyDefinition)entity.GetProperty("Fact");

            //Assert.AreEqual<int>(1, prop.Attributes.Length, "Attributes.Factory absent");
            Assert.AreEqual <string>("Factory", prop.Attributes.ToString(), "Attributes.Factory invalid");

            prop = (ScalarPropertyDefinition)entity.GetProperty("TestInsDef");

            //Assert.AreEqual<int>(1, prop.Attributes.Length, "Attributes.Factory absent");
            Assert.AreEqual <string>("InsertDefault", prop.Attributes.ToString(), "Attributes.InsertDefault invalid");

            prop = (ScalarPropertyDefinition)entity.GetProperty("TestNullabe");

            Assert.AreEqual <Type>(typeof(int?), prop.PropertyType.ClrType);
            Assert.IsFalse(prop.Disabled, "Disabled false");

            prop = (ScalarPropertyDefinition)entity.GetProperty("TestDisabled");
            Assert.IsTrue(prop.Disabled, "Disabled true");
        }
Пример #9
0
        private void FillEntityProperties(PropertyDefinition rp, XmlNode propertiesNode)
        {
            XmlElement propertyElement = null;

            if (rp is ScalarPropertyDefinition)
            {
                propertyElement = CreateElement("Property");
            }
            else if (rp is EntityPropertyDefinition)
            {
                propertyElement = CreateElement("EntityProperty");
            }
            else if (rp is CustomPropertyDefinition)
            {
                propertyElement = CreateElement("CustomProperty");
            }
            else
            {
                throw new NotSupportedException(rp.GetType().ToString());
            }

            if (rp.PropertyAccessLevel == AccessLevel.Private &&
                rp.Interfaces.Any())
            {
                string[] ss = rp.Name.Split(':');
                if (ss.Length > 1)
                {
                    propertyElement.SetAttribute("name", ss[1]);
                }
                else
                {
                    propertyElement.SetAttribute("name", rp.Name);
                }
            }
            else
            {
                propertyElement.SetAttribute("name", rp.Name);
            }

            if (rp.Attributes != Field2DbRelations.None)
            {
                propertyElement.SetAttribute("attributes", rp.Attributes.ToString().Replace(",", ""));
            }

            if (rp.SourceFragment != null)
            {
                propertyElement.SetAttribute("table", rp.SourceFragment.Identifier);
            }

            if (rp.PropertyType != null)
            {
                if (rp is ScalarPropertyDefinition || rp is CustomPropertyDefinition)
                {
                    propertyElement.SetAttribute("type", rp.PropertyType.Identifier);
                }
                else if (rp is EntityPropertyDefinition)
                {
                    propertyElement.SetAttribute("referencedEntity", rp.PropertyType.Entity.Identifier);
                }
                else
                {
                    throw new NotSupportedException(rp.GetType().ToString());
                }
            }

            if (!string.IsNullOrEmpty(rp.Description))
            {
                propertyElement.SetAttribute("description", rp.Description);
            }

            if (rp.FieldAccessLevel != AccessLevel.Private && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("classfieldAccessLevel", rp.FieldAccessLevel.ToString());
            }

            if (rp.PropertyAccessLevel != AccessLevel.Public)
            {
                propertyElement.SetAttribute("propertyAccessLevel", rp.PropertyAccessLevel.ToString());
            }

            if (rp.PropertyAlias != rp.Name)
            {
                propertyElement.SetAttribute("propertyAlias", rp.PropertyAlias);
            }

            if (rp.Disabled)
            {
                propertyElement.SetAttribute("disabled", XmlConvert.ToString(true));
            }

            if (rp.Obsolete != ObsoleteType.None)
            {
                propertyElement.SetAttribute("obsolete", rp.Obsolete.ToString());
            }

            if (!string.IsNullOrEmpty(rp.ObsoleteDescripton))
            {
                propertyElement.SetAttribute("obsoleteDescription", rp.ObsoleteDescripton);
            }

            if (rp.EnablePropertyChanged && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("enablePropertyChanged", XmlConvert.ToString(rp.EnablePropertyChanged));
            }

            if (rp.Action != MergeAction.Merge)
            {
                propertyElement.SetAttribute("action", rp.Action.ToString());
            }

            if (!string.IsNullOrEmpty(rp.DefferedLoadGroup) && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("defferedLoadGroup", rp.DefferedLoadGroup);
            }

            if (!rp.GenerateAttribute && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("generateAttribute", XmlConvert.ToString(rp.GenerateAttribute));
            }

            if (!string.IsNullOrEmpty(rp.AvailableFrom) && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("availableFrom", rp.AvailableFrom);
            }

            if (!string.IsNullOrEmpty(rp.AvailableTo) && !(rp is CustomPropertyDefinition))
            {
                propertyElement.SetAttribute("availableTo", rp.AvailableTo);
            }

            if (!string.IsNullOrEmpty(rp.Feature))
            {
                propertyElement.SetAttribute("feature", rp.Feature);
            }

            if (rp.Interfaces.Any())
            {
                var implElement = CreateElement("implement");
                propertyElement.AppendChild(implElement);

                foreach (var interfaceId in rp.Interfaces)
                {
                    var intElement = CreateElement("interface");
                    implElement.AppendChild(intElement);
                    intElement.SetAttribute("ref", interfaceId.Ref);
                    if (!string.IsNullOrEmpty(interfaceId.Prop))
                    {
                        intElement.SetAttribute("property", interfaceId.Prop);
                    }
                }
            }

            if (rp is ScalarPropertyDefinition)
            {
                ScalarPropertyDefinition property = rp as ScalarPropertyDefinition;

                if (!string.IsNullOrEmpty(property.SourceFieldAlias))
                {
                    propertyElement.SetAttribute("fieldAlias", property.SourceFieldAlias);
                }

                if (property.SourceField != null)
                {
                    propertyElement.SetAttribute("fieldName", property.SourceFieldExpression);

                    if (!string.IsNullOrEmpty(property.SourceType))
                    {
                        propertyElement.SetAttribute("fieldTypeName", property.SourceType);
                    }

                    if (property.SourceTypeSize.HasValue)
                    {
                        propertyElement.SetAttribute("fieldTypeSize", XmlConvert.ToString(property.SourceTypeSize.Value));
                    }

                    if (!property.IsNullable)
                    {
                        propertyElement.SetAttribute("fieldNullable", XmlConvert.ToString(property.IsNullable));
                    }

                    if (!string.IsNullOrEmpty(property.SourceField.DefaultValue))
                    {
                        propertyElement.SetAttribute("fieldDefault", property.SourceField.DefaultValue);
                    }
                }
            }
            else if (rp is EntityPropertyDefinition)
            {
                EntityPropertyDefinition property = rp as EntityPropertyDefinition;
                foreach (EntityPropertyDefinition.SourceField field in property.SourceFields)
                {
                    XmlElement fields = CreateElement("field");

                    fields.SetAttribute("relatedProperty", field.PropertyAlias);

                    if (!string.IsNullOrEmpty(field.SourceFieldExpression))
                    {
                        fields.SetAttribute("fieldName", field.SourceFieldExpression);
                    }

                    if (!string.IsNullOrEmpty(field.SourceType))
                    {
                        fields.SetAttribute("fieldTypeName", field.SourceType);
                    }

                    if (field.SourceTypeSize.HasValue)
                    {
                        fields.SetAttribute("fieldTypeSize", XmlConvert.ToString(field.SourceTypeSize.Value));
                    }

                    if (!field.IsNullable)
                    {
                        fields.SetAttribute("fieldNullable", XmlConvert.ToString(field.IsNullable));
                    }

                    if (!string.IsNullOrEmpty(field.SourceFieldAlias))
                    {
                        fields.SetAttribute("fieldAlias", field.SourceFieldAlias);
                    }

                    if (!string.IsNullOrEmpty(field.DefaultValue))
                    {
                        fields.SetAttribute("fieldDefault", field.DefaultValue);
                    }

                    propertyElement.AppendChild(fields);
                }
            }
            else if (rp is CustomPropertyDefinition)
            {
                CustomPropertyDefinition property = rp as CustomPropertyDefinition;
                XmlElement get = CreateElement("Get");
                FillBody(get, property.GetBody);
                propertyElement.AppendChild(get);

                if (property.SetBody != null)
                {
                    XmlElement set = CreateElement("Set");
                    FillBody(set, property.SetBody);
                    propertyElement.AppendChild(set);
                }
            }
            else
            {
                throw new NotSupportedException(rp.GetType().ToString());
            }

            if (rp.Extensions.Count > 0)
            {
                foreach (var extension in rp.Extensions)
                {
                    FillExtension(propertyElement, extension);
                }
            }

            propertiesNode.AppendChild(propertyElement);
        }
        private void CreateUniqueConstraints(StringBuilder script, ISourceProvider provider)
        {
            var uks = Model.GetActiveEntities().SelectMany(e => e.GetActiveProperties())
                      .Where(item => item.SourceFragment != null &&
                             item.SourceFragment.Constraints.Any(cns =>
                                                                 cns.ConstraintType == SourceConstraint.UniqueConstraintTypeName));

            if (uks.Count() == 0)
            {
                return;
            }
            bool hdr = false;

            foreach (SourceFragmentDefinition s in uks.Select(item => item.SourceFragment).Distinct())
            {
                SourceFragmentDefinition sf = s;

                var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item =>
                                                                               item.Name == sf.Name && item.Selector == sf.Selector);

                const bool    isPK  = false;
                List <string> names = new List <string>();
                foreach (SourceConstraint constraint in s.Constraints.Where(item =>
                                                                            item.ConstraintType == SourceConstraint.UniqueConstraintTypeName))
                {
                    if (targetSF == null || !targetSF.Constraints.Any(item =>
                                                                      constraint.ConstraintType == item.ConstraintType &&
                                                                      constraint.SourceFields.All(fld =>
                                                                                                  item.SourceFields.Any(sfld => sfld.SourceFieldExpression == fld.SourceFieldExpression))))
                    {
                        var tablePKs = uks.Where(item => item.SourceFragment == sf);
                        List <PropDefinition> tableProps = new List <PropDefinition>();
                        foreach (PropertyDefinition prop in tablePKs)
                        {
                            if (prop is ScalarPropertyDefinition)
                            {
                                ScalarPropertyDefinition p = prop as ScalarPropertyDefinition;
                                if (constraint.SourceFields.Any(item => item.SourceFieldExpression == p.SourceFieldExpression))
                                {
                                    tableProps.Add(new PropDefinition {
                                        Attr = prop.Attributes, Field = p.SourceField, PropType = prop.PropertyType
                                    });
                                }
                            }
                            else
                            {
                                EntityPropertyDefinition p = prop as EntityPropertyDefinition;
                                foreach (EntityPropertyDefinition.SourceField field in p.SourceFields)
                                {
                                    if (constraint.SourceFields.Any(item => item.SourceFieldExpression == field.SourceFieldExpression))
                                    {
                                        tableProps.Add(new PropDefinition {
                                            Attr     = Field2DbRelations.None, Field = field,
                                            PropType = prop.PropertyType.Entity.GetProperties().Single(pr => pr.PropertyAlias == field.PropertyAlias).PropertyType
                                        }
                                                       );
                                    }
                                }
                            }
                        }
                        string constraintName = "UK_" + sf.Name.Trim(']', '[');
                        if (names.Contains(constraintName))
                        {
                            constraintName += "_" + names.Count(item => item.StartsWith(constraintName));
                        }

                        if (!hdr)
                        {
                            script.AppendLine("--Creating unique constraints");
                            hdr = true;
                        }

                        provider.GenerateCreatePKScript(tableProps, constraintName, script, isPK,
                                                        tablePKs.First().Entity.GetPkProperties().Count() == 0);

                        names.Add(constraintName);
                    }
                }
            }
        }
Пример #11
0
        private void MergeEntities(WXMLModel mergeWith)
        {
            foreach (EntityDefinition newEntity in mergeWith.GetEntities())
            {
                string newEntityIdentifier = newEntity.Identifier;

                EntityDefinition entity = GetEntities().SingleOrDefault(item => item.Identifier == newEntityIdentifier);
                if (entity != null)
                {
                    if (!string.IsNullOrEmpty(newEntity.Name))
                    {
                        entity.Name = newEntity.Name;
                    }

                    entity.Namespace          = newEntity.Namespace;
                    entity.BaseEntity         = newEntity.BaseEntity;
                    entity.Behaviour          = newEntity.Behaviour;
                    entity.CacheCheckRequired = newEntity.CacheCheckRequired;
                    entity.Description        = newEntity.Description;
                    entity.Disabled           = newEntity.Disabled;
                    entity.InheritsBaseTables = newEntity.InheritsBaseTables;
                    entity.AutoInterface      = newEntity.AutoInterface;
                    entity.UseGenerics        = newEntity.UseGenerics;
                    entity.FamilyName         = newEntity.FamilyName;
                    foreach (var @interface in newEntity.Interfaces)
                    {
                        if (!entity.Interfaces.ContainsKey(@interface.Key))
                        {
                            entity.Interfaces.Add(@interface.Key, @interface.Value);
                        }
                    }

                    entity.ClearSourceFragments();
                    foreach (SourceFragmentRefDefinition newsf in newEntity.OwnSourceFragments)
                    {
                        //string newsfId = newsf.Identifier;
                        //SourceFragmentRefDefinition sf =
                        //    entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId);

                        //foreach (PropertyDefinition rp in entity.GetProperties()
                        //    .Where(item=>item.SourceFragment == sf))
                        //{
                        //    if (rp is ScalarPropertyDefinition)
                        //    {
                        //        ScalarPropertyDefinition property = rp as ScalarPropertyDefinition;
                        //        property.SourceField.SourceFragment = newsf;
                        //    }
                        //    else if (rp is EntityPropertyDefinition)
                        //    {
                        //        EntityPropertyDefinition property = rp as EntityPropertyDefinition;
                        //        property.SourceFragment = newsf;
                        //    }
                        //}

                        //entity.RemoveSourceFragment(sf);
                        entity.AddSourceFragment(newsf);
                    }
                    //List<SourceFragmentRefDefinition> torem = new List<SourceFragmentRefDefinition>();
                    //foreach (SourceFragmentRefDefinition newsf in newEntity.GetSourceFragments())
                    //{
                    //    string newsfId = newsf.Identifier;
                    //    SourceFragmentRefDefinition sf =
                    //        entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId);

                    //    if (sf != null)
                    //    {
                    //        if (newsf.Action == MergeAction.Delete)
                    //        {
                    //            entity.RemoveSourceFragment(sf);
                    //            torem.Add(newsf);
                    //        }
                    //        else if (newsf.AnchorTable != null)
                    //        {
                    //            sf.AnchorTable = newsf.AnchorTable;
                    //            sf.JoinType = newsf.JoinType;
                    //            if (newsf.Conditions.Count > 0)
                    //            {
                    //                foreach (SourceFragmentRefDefinition.Condition c in newsf.Conditions)
                    //                {
                    //                    SourceFragmentRefDefinition.Condition ec =
                    //                        sf.Conditions.SingleOrDefault(item =>
                    //                            item.LeftColumn == c.LeftColumn &&
                    //                            item.RightColumn == c.RightColumn
                    //                        );
                    //                    if (ec != null)
                    //                    {
                    //                        if (c.Action == MergeAction.Delete)
                    //                            sf.Conditions.Remove(ec);
                    //                    }
                    //                    else
                    //                        sf.Conditions.Add(c);
                    //                }
                    //            }
                    //        }
                    //    }
                    //    else
                    //        entity.AddSourceFragment(newsf);
                    //}

                    //foreach (SourceFragmentRefDefinition newsf in torem)
                    //{
                    //    newEntity.RemoveSourceFragment(newsf);
                    //}

                    foreach (PropertyDefinition newProperty in newEntity.GetProperties())
                    {
                        string newPropertyName = newProperty.PropertyAlias;

                        PropertyDefinition rp =
                            entity.GetProperties().SingleOrDefault(item => item.PropertyAlias == newPropertyName);

                        if (rp != null)
                        {
                            if (newProperty.Action == MergeAction.Delete)
                            {
                                entity.RemoveProperty(rp);
                            }
                            else
                            {
                                rp.Name = MergeString(rp, newProperty, item => item.Name);
                                rp.ObsoleteDescripton = MergeString(rp, newProperty, item => item.ObsoleteDescripton);
                                rp.DefferedLoadGroup  = MergeString(rp, newProperty, item => item.DefferedLoadGroup);
                                rp.GenerateAttribute  = newProperty.GenerateAttribute;
                                rp.AvailableFrom      = MergeString(rp, newProperty, item => item.AvailableFrom);
                                rp.AvailableTo        = MergeString(rp, newProperty, item => item.AvailableTo);
                                rp.Description        = MergeString(rp, newProperty, item => item.Description);
                                if (newProperty.Attributes != Field2DbRelations.None)
                                {
                                    rp.Attributes = newProperty.Attributes;
                                }
                                rp.Feature               = MergeString(rp, newProperty, item => item.Feature);
                                rp.Group                 = newProperty.Group ?? rp.Group;
                                rp.PropertyType          = newProperty.PropertyType ?? rp.PropertyType;
                                rp.Disabled              = newProperty.Disabled;
                                rp.EnablePropertyChanged = newProperty.EnablePropertyChanged;
                                rp.SourceFragment        = newProperty.SourceFragment ?? rp.SourceFragment;

                                if (newProperty.FieldAccessLevel != default(AccessLevel))
                                {
                                    rp.FieldAccessLevel = newProperty.FieldAccessLevel;
                                }

                                if (newProperty.PropertyAccessLevel != default(AccessLevel))
                                {
                                    rp.PropertyAccessLevel = newProperty.PropertyAccessLevel;
                                }

                                if (newProperty.Obsolete != default(ObsoleteType))
                                {
                                    rp.Obsolete = newProperty.Obsolete;
                                }

                                if (rp.GetType() != newProperty.GetType())
                                {
                                    PropertyDefinition newProp = null;
                                    if (rp is EntityPropertyDefinition && newProperty is ScalarPropertyDefinition)
                                    {
                                        newProp = new ScalarPropertyDefinition(entity, newPropertyName);
                                        rp.CopyTo(newProp);
                                    }
                                    else if (rp is ScalarPropertyDefinition && newProperty is EntityPropertyDefinition)
                                    {
                                        newProp = new EntityPropertyDefinition(rp as ScalarPropertyDefinition);
                                    }
                                    entity.RemoveProperty(rp);
                                    entity.AddProperty(newProp);
                                    rp = newProp;
                                }

                                if (rp is ScalarPropertyDefinition)
                                {
                                    ScalarPropertyDefinition property = rp as ScalarPropertyDefinition;
                                    property.SourceField.SourceType            = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceType);
                                    property.SourceFieldAlias                  = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldAlias);
                                    property.SourceField.SourceFieldExpression = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldExpression);
                                    property.SourceField.IsNullable            = ((ScalarPropertyDefinition)newProperty).IsNullable;
                                    property.SourceField.SourceTypeSize        = ((ScalarPropertyDefinition)newProperty).SourceTypeSize ?? property.SourceTypeSize;
                                }
                            }
                        }
                        else
                        {
                            entity.AddProperty(newProperty);
                        }
                    }

                    MergeExtensions(entity.Extensions, newEntity.Extensions);
                }
                else
                {
                    AddEntity(newEntity);
                }
            }
        }
Пример #12
0
        public override void GenerateCreateScript(IEnumerable <PropertyDefinition> props, StringBuilder script,
                                                  bool unicodeStrings)
        {
            SourceFragmentDefinition sf = props.First().SourceFragment;

            script.AppendFormat("CREATE TABLE {0}.{1}(", sf.Selector, sf.Name);
            List <PropDefinition> propList = new List <PropDefinition>();

            foreach (PropertyDefinition prop in props)
            {
                if (prop is ScalarPropertyDefinition)
                {
                    ScalarPropertyDefinition sp = prop as ScalarPropertyDefinition;
                    //script.Append(sp.SourceFieldExpression).Append(" ").Append(GetType(sp.SourceField, sp.PropertyType, sp.Attributes, unicodeStrings));

                    //if (sp.SourceField.IsAutoIncrement)
                    //    script.Append(" IDENTITY");

                    //script.Append(sp.IsNullable ? " NULL" : " NOT NULL");

                    //if (!string.IsNullOrEmpty(sp.SourceField.DefaultValue))
                    //    script.AppendFormat(" DEFAULT({0})", sp.SourceField.DefaultValue);

                    //script.Append(", ");
                    propList.Add(new PropDefinition {
                        PropType = prop.PropertyType, Attr = prop.Attributes, Field = sp.SourceField
                    });
                }
                else if (prop is EntityPropertyDefinition)
                {
                    EntityPropertyDefinition ep = prop as EntityPropertyDefinition;

                    foreach (EntityPropertyDefinition.SourceField sp in ep.SourceFields)
                    {
                        if (!props.OfType <ScalarPropertyDefinition>().Any(item => item.SourceFieldExpression == sp.SourceFieldExpression))
                        {
                            var pk = ep.PropertyType.Entity.GetPkProperties().SingleOrDefault(item => item.PropertyAlias == sp.PropertyAlias);
                            if (pk == null)
                            {
                                pk = ep.PropertyType.Entity.GetProperties().OfType <ScalarPropertyDefinition>()
                                     .Single(item => !item.Disabled && item.SourceField.Constraints.Any(cns => cns.ConstraintType == SourceConstraint.UniqueConstraintTypeName));
                            }

                            //script.Append(sp.SourceFieldExpression).Append(" ").Append(
                            //    GetType(sp, pk.PropertyType, Field2DbRelations.None, unicodeStrings));

                            //script.Append(sp.IsNullable ? " NULL" : " NOT NULL");

                            //if (!string.IsNullOrEmpty(sp.DefaultValue))
                            //    script.AppendFormat(" DEFAULT({0})", sp.DefaultValue);

                            //script.Append(", ");
                            propList.Add(new PropDefinition {
                                PropType = pk.PropertyType, Attr = Field2DbRelations.None, Field = sp
                            });
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException(prop.GetType().ToString());
                }
            }
            GenerateColumns(propList, script, unicodeStrings);

            script.Length -= 2;
            script.AppendLine(");");
            script.AppendLine();
        }