Exemplo n.º 1
0
        private void WireSets()
        {
            foreach (string tableToWire in FkForTables.Keys)
            {
                logger.Debug(string.Format("{0} working on:", GetType().Name) + tableToWire);
                Dictionary <string, int> multicolls = new Dictionary <string, int>();
                var kinfo = FkForTables[tableToWire];
                foreach (var key in kinfo.Keys)
                {
                    var            kdef   = kinfo[key];
                    ITableMetadata ptable = GetPrimaryTable(kdef);
                    ITableMetadata ctable = GetForeignTable(kdef);

                    if (null != ptable)
                    {
                        var collectingClass = context.Model.GetClassFromTableName(ptable.Name);
                        var collectedClass  = context.Model.GetClassFromTableName(ctable.Name);
                        if (null == collectedClass)
                        {
                            logger.Warn(string.Format("Not included table {0} could not be used in a collection", ctable.Name));
                        }
                        if (null == collectingClass)
                        {
                            logger.Warn(string.Format("Target table for a collection of {0} is not included", ptable.Name));
                        }
                        if (null != collectedClass && null != collectingClass)
                        {
                            if (!multicolls.ContainsKey(collectedClass.name))
                            {
                                multicolls[collectedClass.name] = 0;
                            }
                            set s = new set();
                            s.inverse = true;
                            context.Model.AddCollectionToEntity(collectingClass.name, s);
                            s.table = collectedClass.table;
                            s.name  = context.NamingStrategy.GetNameForCollection(collectedClass.name, multicolls[collectedClass.name]);
                            s.key   = GenerateCollectionKey(kdef);

                            var pkctable = GetKeyColumns(ctable);
                            if (pkctable.Length > 0) // collected object is an entity
                            {
                                s.Item = new onetomany()
                                {
                                    @class = context.Model.GetClassFromTableName(ctable.Name).name
                                };
                            }
                            else
                            {
                                var        notanentity = context.Model.GetClassFromTableName(ctable.Name);
                                property[] props       = context.Model.GetPropertyOfEntity(notanentity.name);
                                if (props.Length == 1)
                                {
                                    //use an element
                                    element e = new element();
                                    e.column    = props[0].column ?? props[0].name;
                                    e.type1     = props[0].type1;
                                    e.type      = props[0].type;
                                    e.precision = props[0].precision;
                                    e.length    = props[0].length;
                                    s.Item      = e;
                                }
                                else
                                {
                                    //use a composite element
                                    compositeelement ce = new compositeelement();
                                    ce.@class = context.NamingStrategy.GetClassNameForCollectionComponent(ctable.Name);
                                    ce.Items  = context.Model.GetPropertyOfEntity(notanentity.name);
                                    s.Item    = ce;
                                }
                                context.Model.RemoveEntity(notanentity.name);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void CheckIfCanBeMap(set set, @class entity)
 {
     if (set.Item is onetomany)
     {
         var collectedEntity = context.Model.GetClassFromEntityName((set.Item as onetomany).@class);
         if (collectedEntity.Item is compositeid)
         {
             var           cid       = collectedEntity.Item as compositeid;
             List <string> setColumn = new List <string>();
             if (!string.IsNullOrEmpty(set.key.column1))
             {
                 setColumn.Add(set.key.column1);
             }
             if (null != set.key.column)
             {
                 setColumn.AddRange(set.key.column.Select(q => q.name));
             }
             List <string> collectedKeyColumns = new List <string>();
             collectedKeyColumns.AddRange(cid.Items.OfType <keyproperty>().Select(q => q.column1 ?? q.name));
             List <string> nonOverlappingColumns;
             if (CheckOverlapping(setColumn, collectedKeyColumns, out nonOverlappingColumns))
             {
                 setToRemove.Add(set);
                 entitiesToRemove.Add(collectedEntity.name);
                 map map = new map();
                 map.name  = set.name;
                 map.table = set.table;
                 map.key   = set.key;
                 var meta = context.GetTableMetaData(collectedEntity.schema, collectedEntity.table ?? collectedEntity.name);
                 if (nonOverlappingColumns.Count == 1)
                 {
                     map.Item = new index()
                     {
                         column1 = nonOverlappingColumns[0], type = typeConverter.GetNHType(meta.GetColumnMetadata(nonOverlappingColumns[0]))
                     };
                 }
                 else
                 {
                     compositeindex ci = new compositeindex();
                     ci.@class = context.NamingStrategy.GetClassNameForComponentKey(map.name ?? map.table);
                     ci.Items  = nonOverlappingColumns.Select(q => new keyproperty()
                     {
                         name      = context.NamingStrategy.GetPropertyNameFromColumnName(q)
                         , column1 = context.NamingStrategy.GetPropertyNameFromColumnName(q) != q ? q : null
                         , type1   = typeConverter.GetNHType(meta.GetColumnMetadata(q))
                         , length  = meta.GetColumnMetadata(q).ColumnSize == 0 ? null:meta.GetColumnMetadata(q).ColumnSize.ToString()
                     }).ToArray();
                     map.Item = ci;
                 }
                 property[] props = collectedEntity.Items.OfType <property>().ToArray();
                 if (props.Length == 1)
                 {
                     //use an element
                     element e = new element();
                     e.column    = props[0].column ?? props[0].name;
                     e.type1     = props[0].type1;
                     e.type      = props[0].type;
                     e.precision = props[0].precision;
                     e.length    = props[0].length;
                     map.Item1   = e;
                 }
                 else
                 {
                     //use a composite element
                     compositeelement ce = new compositeelement();
                     ce.@class = context.NamingStrategy.GetClassNameForCollectionComponent(collectedEntity.table ?? collectedEntity.name);
                     ce.Items  = context.Model.GetPropertyOfEntity(collectedEntity.name);
                     map.Item1 = ce;
                 }
                 context.Model.AddCollectionToEntity(entity.name, map);
             }
         }
     }
 }
Exemplo n.º 3
0
        private void WireSets()
        {
            foreach (string tableToWire in FkForTables.Keys)
            {
                logger.Debug(string.Format("{0} working on:", GetType().Name) + tableToWire);
                Dictionary<string,int> multicolls = new Dictionary<string,int>();
                var kinfo = FkForTables[tableToWire];
                foreach (var key in kinfo.Keys)
                {
                    var kdef = kinfo[key];
                    ITableMetadata ptable = GetPrimaryTable(kdef);
                    ITableMetadata ctable = GetForeignTable(kdef);

                    if (null != ptable)
                    {
                        var collectingClass = context.Model.GetClassFromTableName(ptable.Name);
                        var collectedClass = context.Model.GetClassFromTableName(ctable.Name);
                        if (null == collectedClass)
                            logger.Warn(string.Format("Not included table {0} could not be used in a collection", ctable.Name));
                        if (null == collectingClass)
                            logger.Warn(string.Format("Target table for a collection of {0} is not included", ptable.Name));
                        if (null != collectedClass && null != collectingClass)
                        {

                            if (!multicolls.ContainsKey(collectedClass.name))
                                multicolls[collectedClass.name] = 0;
                            set s = new set();
                            s.inverse = true;
                            context.Model.AddCollectionToEntity(collectingClass.name, s);
                            s.table = collectedClass.table;
                            s.name = context.NamingStrategy.GetNameForCollection(collectedClass.name, multicolls[collectedClass.name]);
                            s.key = GenerateCollectionKey(kdef);

                            var pkctable = GetKeyColumns(ctable);
                            if (pkctable.Length > 0) // collected object is an entity
                            {
                                s.Item = new onetomany() { @class = context.Model.GetClassFromTableName(ctable.Name).name };
                            }
                            else
                            {
                                var notanentity = context.Model.GetClassFromTableName(ctable.Name);
                                property[] props = context.Model.GetPropertyOfEntity(notanentity.name);
                                if (props.Length == 1)
                                {
                                    //use an element
                                    element e = new element();
                                    e.column = props[0].column ?? props[0].name;
                                    e.type1 = props[0].type1;
                                    e.type = props[0].type;
                                    e.precision = props[0].precision;
                                    e.length = props[0].length;
                                    s.Item = e;
                                }
                                else
                                {
                                    //use a composite element
                                    compositeelement ce = new compositeelement();
                                    ce.@class = context.NamingStrategy.GetClassNameForCollectionComponent(ctable.Name);
                                    ce.Items = context.Model.GetPropertyOfEntity(notanentity.name);
                                    s.Item = ce;
                                }
                                context.Model.RemoveEntity(notanentity.name);
                            }
                        }
                    }
                }
            }
        }
 private void CheckIfCanBeMap(set set, @class entity)
 {
     if (set.Item is onetomany)
     {
         var collectedEntity = context.Model.GetClassFromEntityName((set.Item as onetomany).@class);
         if (collectedEntity.Item is compositeid)
         {
             var cid = collectedEntity.Item as compositeid;
             List<string> setColumn = new List<string>();
             if (!string.IsNullOrEmpty(set.key.column1))
                 setColumn.Add(set.key.column1);
             if (null != set.key.column)
                 setColumn.AddRange(set.key.column.Select(q => q.name));
             List<string> collectedKeyColumns = new List<string>();
             collectedKeyColumns.AddRange(cid.Items.OfType<keyproperty>().Select(q => q.column1 ?? q.name));
             List<string> nonOverlappingColumns;
             if (CheckOverlapping(setColumn, collectedKeyColumns, out nonOverlappingColumns))
             {
                 setToRemove.Add(set);
                 entitiesToRemove.Add(collectedEntity.name);
                 map map = new map();
                 map.name = set.name;
                 map.table = set.table;
                 map.key = set.key;
                 var meta = context.GetTableMetaData(collectedEntity.schema, collectedEntity.table ?? collectedEntity.name);
                 if (nonOverlappingColumns.Count == 1)
                 {
                     map.Item = new index() { column1 = nonOverlappingColumns[0], type = typeConverter.GetNHType(meta.GetColumnMetadata(nonOverlappingColumns[0])) };
                 }
                 else
                 {
                     compositeindex ci = new compositeindex();
                     ci.@class = context.NamingStrategy.GetClassNameForComponentKey(map.name??map.table);
                     ci.Items = nonOverlappingColumns.Select(q => new keyproperty() {
                             name = context.NamingStrategy.GetPropertyNameFromColumnName(q)
                            ,column1 = context.NamingStrategy.GetPropertyNameFromColumnName(q) != q ? q : null
                            ,type1=typeConverter.GetNHType(meta.GetColumnMetadata(q))
                            ,length = meta.GetColumnMetadata(q).ColumnSize == 0 ? null:meta.GetColumnMetadata(q).ColumnSize.ToString()
                     }).ToArray();
                     map.Item = ci;
                 }
                 property[] props = collectedEntity.Items.OfType<property>().ToArray();
                 if (props.Length == 1)
                 {
                     //use an element
                     element e = new element();
                     e.column = props[0].column ?? props[0].name;
                     e.type1 = props[0].type1;
                     e.type = props[0].type;
                     e.precision = props[0].precision;
                     e.length = props[0].length;
                     map.Item1 = e;
                 }
                 else
                 {
                     //use a composite element
                     compositeelement ce = new compositeelement();
                     ce.@class = context.NamingStrategy.GetClassNameForCollectionComponent(collectedEntity.table??collectedEntity.name);
                     ce.Items = context.Model.GetPropertyOfEntity(collectedEntity.name);
                     map.Item1 = ce;
                 }
                 context.Model.AddCollectionToEntity(entity.name, map);
             }
         }
     }
 }