public void GetEntityAccessor_with_null_entityMap_should_return_EntityAccessor_which_is_not_ready()
        {
            var store          = new EntityAccessorStore();
            var entityAccessor = store.GetEntityAccessor(typeof(FakeEntityAccessor1), null);

            Assert.IsFalse(entityAccessor.IsReady);
        }
Esempio n. 2
0
        QueryParam[] BuildParameterArray(StatementMap statement, QueryParam[] paramArray, IDictionary <string, object> inputObjects)
        {
            var dbParams = paramArray.ToDictionary(p => p.Name);

            if ((inputObjects != null) && (inputObjects.Count > 0))
            {
                foreach (var inParam in statement.ParamPropertyMap.Values)
                {
                    object paramObj;
                    if (inputObjects.TryGetValue(inParam.Property.VarName, out paramObj))
                    {
                        EntityAccessor getSetInfo = store.GetEntityAccessor(inParam.ClrType, inParam.Map);

                        PropertyAccessor pInfo;
                        if (getSetInfo.Properties.TryGetValue(inParam.Property.PropName, out pInfo))
                        {
                            QueryParam dbParam = new QueryParam(inParam.QueryParamName, null);
                            dbParam.Value = pInfo.GetMethod(paramObj);
                            if (!dbParams.ContainsKey(inParam.QueryParamName))
                            {
                                dbParams.Add(inParam.QueryParamName, dbParam);
                            }
                        }
                    }
                }
            }

            return(dbParams.Values.ToArray());
        }
        public void GetEntityAccessor_with_null_entityType_should_throw()
        {
            var store = new EntityAccessorStore();

            Assert.Throws <ArgumentNullException>(() => store.GetEntityAccessor(null));
            Assert.Fail("entityType was null. Should have thrown an exception.");
        }
Esempio n. 4
0
        void AddInsertManyToManyOperation(UpdateSqlExecutionList execList, IEnumerable insertedItems, Relation rel, EntityAccessor accessor, bool isInsertStatement)
        {
            if (insertedItems != null)
            {
                var relMap = session.SessionFactory.DbSettings.Map.GetEntityMap(rel.ReferenceEntityName);
                var store  = new EntityAccessorStore();

                var propInfo = accessor.Properties[rel.MapPropertyName];
                if (propInfo == null)
                {
                    throw new GoliathDataException(string.Format("Could not retrieve value of property {0} in mapped entity {1} with clr type {2}.",
                                                                 rel.MapPropertyName, Table.FullName, typeof(T).FullName));
                }

                var propValue = propInfo.GetMethod(entity);

                PropertyAccessor relPropInfo = null;
                foreach (var item in insertedItems)
                {
                    if (relPropInfo == null)
                    {
                        var relAccessor = store.GetEntityAccessor(item.GetType(), relMap);
                        if (!relAccessor.Properties.TryGetValue(rel.ReferenceProperty, out relPropInfo))
                        {
                            throw new GoliathDataException(string.Format("Could not retrieve value of property {0} in mapped entity {1} with clr type {2}.",
                                                                         rel.ReferenceProperty, relMap.FullName, item.GetType().FullName));
                        }
                    }

                    var relParam = new QueryParam(rel.MapReferenceColumn, rel.DbType)
                    {
                        Value = relPropInfo.GetMethod(item)
                    };
                    var propParm = new QueryParam(rel.MapColumn, rel.DbType)
                    {
                        Value = propValue
                    };
                    var dialect = session.SessionFactory.DbSettings.SqlDialect;
                    if (isInsertStatement)
                    {
                        var sql = string.Format("INSERT INTO {0} ({1}, {2}) VALUES({3},{4})",
                                                rel.MapTableName, rel.MapColumn, rel.MapReferenceColumn, dialect.CreateParameterName(propParm.Name), dialect.CreateParameterName(relParam.Name));

                        execList.ManyToManyStatements.Add(Tuple.Create(sql, new List <QueryParam> {
                            propParm, relParam
                        }));
                    }
                    else
                    {
                        var sql = string.Format("DELETE FROM {0} WHERE {1} = {3} AND {2} = {4}",
                                                rel.MapTableName, rel.MapColumn, rel.MapReferenceColumn, dialect.CreateParameterName(propParm.Name), dialect.CreateParameterName(relParam.Name));

                        execList.ManyToManyStatements.Add(Tuple.Create(sql, new List <QueryParam> {
                            propParm, relParam
                        }));
                    }
                }
            }
        }
        public void GetEntityAccessor_with_entityMap_should_return_accessor_with_loaded_properties()
        {
            var store    = new EntityAccessorStore();
            var map      = new Mapping.DynamicEntityMap(typeof(FakeEntityAccessor2));
            var accessor = store.GetEntityAccessor(typeof(FakeEntityAccessor2), map);

            Assert.IsTrue(accessor.IsReady);
            Assert.AreEqual(4, accessor.Properties.Count);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="entityMap">The entity map.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">entityMap</exception>
        public object CreateNewInstance(Type type, EntityMap entityMap)
        {
            if (entityMap == null)
            {
                throw new ArgumentNullException("entityMap");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            object instance;

            if (entityMap.IsTrackable)
            {
                instance = type.CreateProxy(entityMap, true);
            }
            else
            {
                instance = Activator.CreateInstance(type);
            }

            var getSetInfo = entityAccessorStore.GetEntityAccessor(type, entityMap);

            //load collections
            foreach (var rel in entityMap.Relations)
            {
                if ((rel.RelationType == RelationshipType.ManyToMany) ||
                    (rel.RelationType == RelationshipType.OneToMany))
                {
                    PropertyAccessor pInfo;
                    if (getSetInfo.Properties.TryGetValue(rel.PropertyName, out pInfo))
                    {
                        Type refEntityType  = pInfo.PropertyType.GetGenericArguments().FirstOrDefault();
                        var  collectionType = typeof(TrackableList <>).MakeGenericType(new Type[] { refEntityType });
                        var  lazyCol        = Activator.CreateInstance(collectionType);
                        pInfo.SetMethod(instance, lazyCol);
                    }
                }
            }

            return(instance);
        }
Esempio n. 7
0
        internal DeleteSqlExecutionList Build()
        {
            var execList = new DeleteSqlExecutionList();
            var store    = new EntityAccessorStore();
            var accessor = store.GetEntityAccessor(entityType, Table);
            var dialect  = session.SessionFactory.DbSettings.SqlDialect;

            LoadInfos(Table, execList);

            var whereExpression = BuildWhereExpression(dialect);

            foreach (var deleteSqlBodyInfo in execList.Statements)
            {
                deleteSqlBodyInfo.Value.WhereExpression = whereExpression;
            }

            return(execList);
        }
Esempio n. 8
0
        internal UpdateSqlExecutionList Build()
        {
            var execList = new UpdateSqlExecutionList();
            var store    = new EntityAccessorStore();
            var accessor = store.GetEntityAccessor(entityType, Table);
            var dialect  = session.SessionFactory.DbSettings.SqlDialect;

            LoadColumns(execList, Table, accessor);

            var whereExpression = BuildWhereExpression(dialect);

            foreach (var stat in execList.Statements)
            {
                stat.Value.WhereExpression = whereExpression;
            }

            return(execList);
        }
Esempio n. 9
0
        int ExecuteUpdateOrDeleteEntity(INonQuerySqlBuilder <TEntity> sqlBuilder, TEntity entity)
        {
            var pk                   = entityMap.PrimaryKey;
            var entAccessor          = entityAccessorStore.GetEntityAccessor(entityType, entityMap);
            var firstKeyPropertyName = pk.Keys[0].Key.PropertyName;
            var firstKey             = entAccessor.GetPropertyAccessor(firstKeyPropertyName);

            var filterBuilder = sqlBuilder.Where(firstKeyPropertyName).EqualToValue(firstKey.GetMethod(entity));

            if (pk.Keys.Count > 1)
            {
                for (int i = 0; i < pk.Keys.Count; i++)
                {
                    var propName     = pk.Keys[i].Key.PropertyName;
                    var propAccessor = entAccessor.GetPropertyAccessor(propName);
                    filterBuilder.And(pk.Keys[i].Key.PropertyName).EqualToValue(propAccessor.GetMethod(entity));
                }
            }

            return(filterBuilder.Execute());
        }
Esempio n. 10
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);
                }
            }
        }
Esempio n. 11
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);
        }