Ejemplo n.º 1
0
        /// <summary>
        /// Builds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="entityMap">The entity map.</param>
        /// <param name="executionList">The execution list.</param>
        /// <param name="session">The session.</param>
        /// <exception cref="System.ArgumentNullException">entity</exception>
        /// <exception cref="GoliathDataException">Property  + prop.Name +  not found in entity.</exception>
        public void Build(object entity, Type entityType, EntityMap entityMap, InsertSqlExecutionList executionList, ISession session)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (entityMap == null)
            {
                throw new ArgumentNullException("entityMap");
            }

            var info = new InsertSqlInfo {
                TableName = entityMap.TableName
            };
            var entityAccessor = entityAccessorStore.GetEntityAccessor(entityType, entityMap);
            var converterStore = session.SessionFactory.DbSettings.ConverterStore;

            Tuple <string, PropertyAccessor, DbType?> pkTuple = null;

            if (entityMap.PrimaryKey != null)
            {
                pkTuple = ProcessPrimaryKey(entity, entityType, entityMap, info, entityAccessor, executionList, session);
            }

            foreach (var prop in entityMap)
            {
                if (prop.IsPrimaryKey || prop.IsAutoGenerated)
                {
                    continue;
                }

                var rel = prop as Relation;
                if (rel != null)
                {
                    ProcessRelation(rel, entity, entityMap, info, entityAccessor, executionList, converterStore, session);
                }
                else
                {
                    var paramName = ParameterNameBuilderHelper.QueryParamName(entityMap, prop.ColumnName);
                    if (!info.Columns.ContainsKey(paramName))
                    {
                        PropertyAccessor pinf;
                        if (!entityAccessor.Properties.TryGetValue(prop.Name, out pinf))
                        {
                            throw new GoliathDataException("Property " + prop.Name + " not found in entity.");
                        }

                        info.Columns.Add(paramName, prop.ColumnName);
                        var propVal = pinf.GetMethod(entity);
                        //info.Parameters.Add(paramName, new QueryParam(paramName, propVal));
                        info.Parameters.Add(paramName, QueryParam.CreateParameter(prop, paramName, propVal));
                    }
                }
            }

            if ((pkTuple != null) && (pkTuple.Item2 != null))
            {
                var resultType = pkTuple.Item2.PropertyType;

                if (!info.DelayExecute)
                {
                    var        pkValue = executionList.ExcuteStatement(session, info, resultType);
                    QueryParam pkQueryParam;
                    if (!info.Parameters.TryGetValue(pkTuple.Item1, out pkQueryParam))
                    {
                        pkQueryParam = new QueryParam(pkTuple.Item1, pkTuple.Item3);
                    }

                    pkQueryParam.Value = pkValue;
                    pkTuple.Item2.SetMethod(entity, pkValue);
                    executionList.GeneratedKeys.Add(pkTuple.Item1, pkQueryParam);
                }
                else
                {
                    executionList.ExcuteStatement(session, info, typeof(object));
                }
            }
            else
            {
                executionList.ExcuteStatement(session, info, typeof(object));
            }

            //check many to many relations and process them.
            foreach (var rel in entityMap.Relations)
            {
                if (rel.RelationType == RelationshipType.ManyToMany)
                {
                    ProcessManyToManyRelation(rel, entity, entityMap, info, entityAccessor, executionList, converterStore, session);
                }
            }
        }
Ejemplo n.º 2
0
        void AddColumnAndParameterToUpdateInfo(UpdateSqlExecutionList execList, UpdateSqlBodyInfo updateBodyInfo, EntityMap entityMap, Property prop, PropertyAccessor propInfo, EntityAccessor accessor)
        {
            object val   = propInfo.GetMethod(entity);
            bool   isRel = false;

            if (prop is Relation)
            {
                var rel = (Relation)prop;
                if (updateBodyInfo.Columns.ContainsKey(prop.ColumnName))
                {
                    return;
                }

                isRel = true;
                if (val != null)
                {
                    if (rel.RelationType == RelationshipType.ManyToOne)
                    {
                        var store       = new EntityAccessorStore();
                        var relMap      = session.SessionFactory.DbSettings.Map.GetEntityMap(rel.ReferenceEntityName);
                        var relAccessor = store.GetEntityAccessor(val.GetType(), relMap);

                        var relPinfo = relAccessor.Properties[rel.ReferenceProperty];

                        if (relPinfo == null)
                        {
                            throw new MappingException(string.Format("could not find property {0} in mapped entity {1}", rel.ReferenceProperty, relMap.FullName));
                        }

                        val = relPinfo.GetMethod(val);
                    }
                    else if (rel.RelationType == RelationshipType.ManyToMany)
                    {
                        var trackableCollection = val as ITrackableCollection;
                        if (trackableCollection != null)
                        {
                            AddInsertManyToManyOperation(execList, trackableCollection.InsertedItems, rel, accessor, true);
                            AddInsertManyToManyOperation(execList, trackableCollection.DeletedItems, rel, accessor, false);
                        }
                        return;
                        //NOTE: if not trackable collection used mapped statement to add or remove many to many associations
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                Tuple <QueryParam, bool> etuple;
                if (updateBodyInfo.Columns.TryGetValue(prop.ColumnName, out etuple))
                {
                    if (etuple.Item2)
                    {
                        updateBodyInfo.Columns.Remove(prop.ColumnName);
                    }
                    else
                    {
                        return;
                    }
                }
            }

            //Tuple<QueryParam, bool> tuple = Tuple.Create(new QueryParam(string.Format("{0}_{1}",entityMap.TableAlias, prop.ColumnName)) { Value = val }, isRel);
            Tuple <QueryParam, bool> tuple = Tuple.Create(QueryParam.CreateParameter(prop, string.Format("{0}_{1}", TableQueryMap.CreatePrefix(2, 2), prop.ColumnName), val), isRel);

            updateBodyInfo.Columns.Add(prop.ColumnName, tuple);
        }