Exemplo n.º 1
0
        private void ProcessManyToOneReference(
			Entity entity,
			DirectedReference directedReference,
			Action<object> addItem,
			string cascade,
			string collectionCascade,
			string lazy,
			string orderByClause,
			bool inverse)
        {
            if (directedReference.FromEndEnabled == false) return;

            ITable referenceMappedTable = directedReference.Reference.MappedTable();
            DirectedRelationship directedRelationship = null;

            if (referenceMappedTable == null)
                directedRelationship = GetDirectedMappedRelationship(entity, directedReference.Reference);

            if (directedReference.FromEndCardinality == Cardinality.One)
            {
                fetchMode fetchMode;
                bool insert;
                bool update;

                if (directedReference.Entity1IsFromEnd)
                {
                    fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd1FetchMode().ToString(), true);
                    insert = directedReference.Reference.GetReferenceEnd1Insert();
                    update = directedReference.Reference.GetReferenceEnd1Update();
                }
                else
                {
                    fetchMode = (fetchMode)Enum.Parse(typeof(fetchMode), directedReference.Reference.GetReferenceEnd2FetchMode().ToString(), true);
                    insert = directedReference.Reference.GetReferenceEnd2Insert();
                    update = directedReference.Reference.GetReferenceEnd2Update();
                }
                manytoone manyToOneNode;

                if (referenceMappedTable == null)
                    manyToOneNode = CreateManyToOneNode(directedReference, directedRelationship, cascade);
                else
                    manyToOneNode = CreateManyToOneNode(directedReference, referenceMappedTable);

                manyToOneNode.fetch = fetchMode;
                manyToOneNode.fetchSpecified = true;
                manyToOneNode.insert = insert;
                manyToOneNode.update = update;

                addItem(manyToOneNode);
            }
            else
            {
                key keyNode = new key();

                if (referenceMappedTable == null &&
                    directedRelationship.ToKey.Columns.Count > 1)
                {
                    foreach (var columnNode in GetColumnNodes(directedRelationship.ToKey.Columns))
                        keyNode.AddColumn(columnNode);
                }
                else if (referenceMappedTable != null)
                {
                    ITable toPrimaryMappedTable = EntityMapper.GetPrimaryTable(directedReference.ToEntity);
                    var toColumnsInPrimaryKey = referenceMappedTable.Relationships.First(t => t.PrimaryTable == toPrimaryMappedTable || t.ForeignTable == toPrimaryMappedTable).ForeignKey.Columns;

                    foreach (var columnNode in GetColumnNodes(toColumnsInPrimaryKey))
                        keyNode.AddColumn(columnNode);
                }
                else
                    keyNode.column1 = directedRelationship.ToKey.Columns[0].Name.BackTick();

                onetomany oneToManyNode = new onetomany();
                oneToManyNode.@class = directedReference.ToEntity.Name;

                collectionFetchMode collFetchMode;

                if (directedReference.Entity1IsFromEnd)
                    collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd1CollectionFetchMode().ToString(), true);
                else
                    collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd2CollectionFetchMode().ToString(), true);

                AssociationType type = NHCollections.GetAssociationType(directedReference);

                switch (type)
                {
                    case AssociationType.None:
                        Log.WarnFormat("No association type was set on reference {0} for the end {1}. This is usually an error.", directedReference.Reference.Name, directedReference.Entity1IsFromEnd ? "1" : "2");
                        return;
                    case AssociationType.Set:
                        var set = CreateSetNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                        set.Item = oneToManyNode;

                        if (orderByClause.Length > 0)
                            set.orderby = orderByClause;

                        addItem(set);
                        break;
                    case AssociationType.Map:
                        var mapNode = CreateMapNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                        mapNode.Item = new index
                        {
                            column1 = NHCollections.GetIndexColumnName(directedReference),
                            type = NHCollections.GetIndexColumnTypeName(directedReference, EntityMapper.GetPrimaryTable(directedReference.ToEntity))
                        };
                        mapNode.Item1 = oneToManyNode;

                        if (orderByClause.Length > 0)
                            mapNode.orderby = orderByClause;

                        addItem(mapNode);
                        break;
                    case AssociationType.Bag:
                        var bag = CreateBagNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                        bag.Item = oneToManyNode;

                        if (orderByClause.Length > 0)
                            bag.orderby = orderByClause;

                        addItem(bag);
                        break;
                    case AssociationType.List:
                        list listNode = CreateListNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                        listNode.Item = new index { column1 = NHCollections.GetIndexColumnName(directedReference) };
                        listNode.Item1 = oneToManyNode;

                        if (orderByClause.Length > 0)
                            listNode.orderby = orderByClause;

                        addItem(listNode);
                        break;
                    case AssociationType.IDBag:
                        idbag idbagNode = CreateIdBagNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                        idbagNode.collectionid = new collectionid
                        {
                            column1 = NHCollections.GetIndexColumnName(directedReference),
                            generator = new generator { @class = "sequence" },
                            type = NHCollections.GetIndexColumnTypeName(directedReference, EntityMapper.GetPrimaryTable(directedReference.ToEntity))
                        };

                        addItem(idbagNode);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("AssociationType not handled yet: " + type.ToString());
                }
            }
        }
Exemplo n.º 2
0
        private void ProcessManyToManyReference(
			Entity entity,
			DirectedReference directedReference,
			Action<object> addItem,
			Dictionary<ITable, int> manyToManySameTableProcessingCounts,
			string collectionCascade,
			string lazy,
			string orderByClause,
			bool inverse)
        {
            if (directedReference.FromEndEnabled == false) return;

            ITable referenceMappedTable = directedReference.Reference.MappedTable();

            ITable fromPrimaryMappedTable = EntityMapper.GetPrimaryTable(entity);
            ITable toPrimaryMappedTable = EntityMapper.GetPrimaryTable(directedReference.ToEntity);
            Cardinality cardinalityPrimary;
            Cardinality cardinalityForeign;
            IKey mainKey;
            IKey associationKey;
            ITable associationTable = entity.GetAssociationTable(directedReference.ToEntity, out cardinalityPrimary, out cardinalityForeign, out mainKey, out associationKey);

            key keyNode = new key();
            List<IColumn> fromInPrimaryKey = new List<IColumn>();
            List<IColumn> toInPrimaryKey = new List<IColumn>();

            if (fromPrimaryMappedTable == toPrimaryMappedTable)
            {
                // This many-to-many relationship is to the same table
                if (manyToManySameTableProcessingCounts.ContainsKey(toPrimaryMappedTable))
                {
                    int index = manyToManySameTableProcessingCounts[toPrimaryMappedTable];
                    index++;
                    fromInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable).ElementAt(index).PrimaryKey.Columns);
                    toInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable).ElementAt(index).ForeignKey.Columns);
                    manyToManySameTableProcessingCounts[toPrimaryMappedTable] = index;
                }
                else
                {
                    fromInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable).ElementAt(0).PrimaryKey.Columns);
                    toInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable).ElementAt(0).ForeignKey.Columns);
                    manyToManySameTableProcessingCounts.Add(toPrimaryMappedTable, 0);
                }
            }
            else
            {
                foreach (var coll in referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable).Select(r => r.ForeignKey.Columns))
                    foreach (var c in coll)
                        fromInPrimaryKey.Add(c);

                foreach (var coll in referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable).Select(r => r.ForeignKey.Columns))
                    foreach (var c in coll)
                        toInPrimaryKey.Add(c);
            }

            if (fromInPrimaryKey.Count() == 1)
                keyNode.column1 = fromInPrimaryKey.First().Name.BackTick();
            else
                foreach (var columnNode in GetColumnNodes(fromInPrimaryKey))
                    keyNode.AddColumn(columnNode);

            manytomany manyToManyNode = new manytomany();
            manyToManyNode.@class = directedReference.ToEntity.Name;

            if (toInPrimaryKey.Count() == 1)
                manyToManyNode.column = toInPrimaryKey.First().Name.BackTick();
            else
                foreach (var columnNode in GetColumnNodes(toInPrimaryKey))
                    keyNode.AddColumn(columnNode);

            collectionFetchMode collFetchMode;

            if (directedReference.Entity1IsFromEnd)
                collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd1CollectionFetchMode().ToString(), true);
            else
                collFetchMode = (collectionFetchMode)Enum.Parse(typeof(collectionFetchMode), directedReference.Reference.GetReferenceEnd2CollectionFetchMode().ToString(), true);

            AssociationType type = NHCollections.GetAssociationType(directedReference);

            switch (type)
            {
                case AssociationType.None:
                    Log.WarnFormat("No association type was set on reference {0} for the end {1}. This is usually an error.", directedReference.Reference.Name, directedReference.Entity1IsFromEnd ? "One" : "Two");
                    return;
                case AssociationType.Set:
                    set setNode = CreateSetNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                    setNode.table = referenceMappedTable.Name.BackTick();
                    setNode.Item = manyToManyNode;

                    if (orderByClause.Length > 0)
                        setNode.orderby = orderByClause;

                    addItem(setNode);
                    break;
                case AssociationType.Bag:
                    bag bagNode = CreateBagNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                    bagNode.table = referenceMappedTable.Name.BackTick();
                    bagNode.Item = manyToManyNode;

                    if (orderByClause.Length > 0)
                        bagNode.orderby = orderByClause;

                    addItem(bagNode);
                    break;
                case AssociationType.Map:
                    map mapNode = CreateMapNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                    mapNode.table = referenceMappedTable.Name.BackTick();
                    mapNode.Item = new index
                                        {
                                            column1 = NHCollections.GetIndexColumnName(directedReference),
                                            type = NHCollections.GetIndexColumnTypeName(directedReference, toPrimaryMappedTable /*fromPrimaryMappedTable*/)
                                        };
                    mapNode.Item1 = manyToManyNode;

                    if (orderByClause.Length > 0)
                        mapNode.orderby = orderByClause;

                    addItem(mapNode);
                    break;
                case AssociationType.List:
                    list listNode = CreateListNode(directedReference, keyNode, collectionCascade, collFetchMode, lazy, inverse);
                    listNode.table = referenceMappedTable.Name.BackTick();
                    listNode.Item = new index
                    {
                        column1 = NHCollections.GetIndexColumnName(directedReference),
                    };
                    listNode.Item1 = manyToManyNode;

                    if (orderByClause.Length > 0)
                        listNode.orderby = orderByClause;

                    addItem(listNode);
                    break;
                // case AssociationType.IDBag:
                //     throw new NotImplementedException(
                //         string.Format("Have not implemented {0} association type for Many To Many relationships", type));
                default:
                    throw new NotImplementedException("AssociationType not handled yet: " + type.ToString());
            }
        }
Exemplo n.º 3
0
        private set CreateSetNode(DirectedReference reference, key keyNode, string cascade, collectionFetchMode colFetchMode, string lazyString, bool inverse)
        {
            var lazy = (collectionLazy)Enum.Parse(typeof(collectionLazy), lazyString.ToString().Replace("_", ""), true);

            set setNode = new set();
            setNode.name = reference.FromName;
            setNode.key = keyNode;
            setNode.cascade = cascade == "none" ? null : cascade;
            setNode.inverse = inverse;

            setNode.fetchSpecified = colFetchMode != collectionFetchMode.select;
            setNode.fetch = colFetchMode;
            setNode.lazySpecified = lazy != collectionLazy.@true;
            setNode.lazy = (collectionLazy)Enum.Parse(typeof(collectionLazy), lazy.ToString().Replace("_", ""), true);

            var sqlWhereClause = NHCollections.GetSqlWhereClause(reference);

            if (string.IsNullOrEmpty(sqlWhereClause) == false)
                setNode.where = sqlWhereClause;

            return setNode;
        }
Exemplo n.º 4
0
        private key GetKeyNode(ITable joinedTable)
        {
            int count = joinedTable.ColumnsInPrimaryKey.Count();
            key keyNode = new key();

            if (count > 1)
            {
                foreach (var col in joinedTable.ColumnsInPrimaryKey)
                {
                    column column = new column { name = col.Name };
                    keyNode.AddColumn(column);
                }
            }
            else if (count == 1)
                keyNode.column1 = joinedTable.ColumnsInPrimaryKey.First().Name.BackTick();

            return keyNode;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a property for an key in a joined subclass in a Fluent project.
        /// </summary>
        /// <param name="newEntity"></param>
        /// <param name="mapping"></param>
        /// <param name="hKey"></param>
        /// <returns></returns>
        private Property CreateProperty(Entity newEntity, Mapping mapping, key hKey, ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable table)
        {
            Property prop;
            //mapping.
            string columnName;

            if (!string.IsNullOrEmpty(hKey.column1))
                columnName = hKey.column1.UnBackTick();
            else
                columnName = hKey.column[0].name.UnBackTick();

            ArchAngel.Providers.EntityModel.Model.EntityLayer.Property existingProperty = newEntity.Properties.SingleOrDefault(p => p.MappedColumn() != null && p.MappedColumn().Name.ToLowerInvariant() == columnName.ToLowerInvariant());

            if (existingProperty != null)
            {
                existingProperty.IsKeyProperty = true;
                return existingProperty;
            }

            //if (hKey.column1 != null)
            //    prop = CreateProperty(newEntity, mapping, hKey.type, hKey.name, hKey.column1, hKey.length);
            //else
            //    prop = CreateProperty(newEntity, mapping, hKey.type, hKey.name, hKey.column[0].name, hKey.length);

            //if (hKey.column1 != null)
            //    prop = CreateProperty(newEntity, mapping, hKey.type, hKey.name, hKey.column1, hKey.length);
            //else

            //string csharpType = ArchAngel.Interfaces.ProjectOptions.TypeMappings.Utility.GetCSharpTypeName(newEntity.MappedTables().ElementAt(0).Database.DatabaseType.ToString(), hKey.column[0].sqltype);
            ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn column = table.Columns.SingleOrDefault(c => c.Name.ToLowerInvariant() == columnName.ToLowerInvariant().UnBackTick());
            ArchAngel.Interfaces.ProjectOptions.TypeMappings.Utility.ColumnInfo columnInfo = new Interfaces.ProjectOptions.TypeMappings.Utility.ColumnInfo();

            string type;
            bool nullability;

            if (column != null)
            {
                type = column.OriginalDataType;
                nullability = column.IsNullable;
                columnInfo.Precision = column.Precision;
                columnInfo.Scale = column.Scale;
                columnInfo.Size = column.Size;
            }
            else
            {
                type = hKey.column[0].sqltype;

                if (hKey.column[0].notnullSpecified)
                    nullability = !hKey.column[0].notnull;
                else
                    nullability = false;
            }
            columnInfo.IsNullable = nullability;
            columnInfo.Name = columnName;
            columnInfo.TypeName = type;

            string csharpType = ArchAngel.Interfaces.ProjectOptions.TypeMappings.Utility.GetCSharpTypeName(table.Database.DatabaseType.ToString(), columnInfo);
            prop = CreateProperty(newEntity, mapping, csharpType, columnName, columnName, "0");
            return prop;
        }
Exemplo n.º 6
0
        private void ProcessCollection(
			@class hClass,
			key keyNode,
			index indexNode,
			manytomany many,
			string className,
			string schema,
			string tableName,
			string propertyName,
			string cascade,
			AssociationType associationType,
			string whereClause,
			collectionFetchMode fetchMode,
			bool inverse,
			collectionLazy lazy,
			string orderByClause)
        {
            #region OrderBy
            string orderByPropertyName = "";
            bool orderByIsAsc = true;

            if (!string.IsNullOrWhiteSpace(orderByClause))
            {
                orderByClause = orderByClause.Trim();

                if (orderByClause.EndsWith(" desc", StringComparison.InvariantCultureIgnoreCase))
                {
                    orderByIsAsc = false;
                    orderByPropertyName = orderByClause.Substring(0, orderByClause.LastIndexOf(" desc", StringComparison.InvariantCultureIgnoreCase)).Trim();
                }
                else if (orderByClause.EndsWith(" asc", StringComparison.InvariantCultureIgnoreCase))
                {
                    orderByIsAsc = false;
                    orderByPropertyName = orderByClause.Substring(0, orderByClause.LastIndexOf(" asc", StringComparison.InvariantCultureIgnoreCase)).Trim();
                }
                else
                    orderByPropertyName = orderByClause;
            }
            #endregion

            string indexName = null;

            if (indexNode != null)
            {
                if (indexNode.column != null && indexNode.column.Count() > 0)
                    indexName = indexNode.column[0].name;
                else
                    indexName = indexNode.column1;
            }
            if (many != null)
            {
                var fkColumns = GetColumnNames(many.column, many.Columns()).ToList();
                string thisEntityName;
                string otherEntityName;
                EntityLoader.IsNameFullyQualified(hClass.name, out thisEntityName);
                EntityLoader.IsNameFullyQualified(many.@class, out otherEntityName);

                var collectionInfo = new AssociationInformation
                                        {
                                            PropertyName = propertyName,
                                            ForeignKeyColumnNames = fkColumns,
                                            ForeignKeyBelongsToThisTable = !ForeignKeyBelongsToThisTable(hClass, fkColumns),
                                            AssociationTableName = new AssociationInformation.TableNameType(schema, tableName),
                                            ThisEntityName = thisEntityName,
                                            OtherEntityName = otherEntityName,
                                            Cardinality = Cardinality.Many,
                                            CollectionCascade = ArchAngel.Interfaces.NHibernateEnums.Helper.GetCollectionCascadeType(cascade),
                                            CollectionLazy = ArchAngel.Interfaces.NHibernateEnums.Helper.GetCollectionLazyType(lazy.ToString()),
                                            CollectionFetchMode = (CollectionFetchModes)Enum.Parse(typeof(CollectionFetchModes), fetchMode.ToString(), true),
                                            IndexColumn = indexName,
                                            WhereClause = whereClause,
                                            AssociationType = associationType,
                                            Inverse = inverse ? ArchAngel.Interfaces.NHibernateEnums.BooleanInheritedTypes.@true : ArchAngel.Interfaces.NHibernateEnums.BooleanInheritedTypes.@false,
                                            OrderByColumnName = orderByPropertyName,
                                            OrderByIsAsc = orderByIsAsc
                                        };
                associationInformation.Add(collectionInfo);
            }
            else
            {
                var fkColumns = GetColumnNames(keyNode.column1, keyNode.Columns()).ToList();
                string thisEntityName;
                string otherEntityName;
                EntityLoader.IsNameFullyQualified(hClass.name, out thisEntityName);
                EntityLoader.IsNameFullyQualified(className, out otherEntityName);

                bool topLevelInverse = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultInverse();
                BooleanInheritedTypes inverseValue = inverse ? BooleanInheritedTypes.@true : BooleanInheritedTypes.@false;

                if ((inverseValue == BooleanInheritedTypes.@false && topLevelInverse == false) ||
                    (inverseValue == BooleanInheritedTypes.@true && topLevelInverse == true))
                    inverseValue = BooleanInheritedTypes.inherit_default;

                var collectionInfo = new AssociationInformation
                                        {
                                            PropertyName = propertyName,
                                            ForeignKeyColumnNames = fkColumns,
                                            ForeignKeyBelongsToThisTable = ForeignKeyBelongsToThisTable(hClass, fkColumns), // GFH
                                            ThisEntityName = thisEntityName,
                                            OtherEntityName = otherEntityName,
                                            Cardinality = Cardinality.Many,
                                            CollectionCascade = ArchAngel.Interfaces.NHibernateEnums.Helper.GetCollectionCascadeType(cascade),
                                            CollectionLazy = ArchAngel.Interfaces.NHibernateEnums.Helper.GetCollectionLazyType(lazy.ToString()),
                                            CollectionFetchMode = (CollectionFetchModes)Enum.Parse(typeof(CollectionFetchModes), fetchMode.ToString(), true),
                                            IndexColumn = indexName,
                                            AssociationType = associationType,
                                            Inverse = inverseValue,
                                            OrderByColumnName = orderByPropertyName,
                                            OrderByIsAsc = orderByIsAsc
                                        };
                associationInformation.Add(collectionInfo);
            }
        }