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.");
        }
        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);
        }
        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);
        }
Beispiel #5
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);
        }
        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);
        }
 public SerializeOneToMany(SqlDialect sqlDialect, EntityAccessorStore store)
     : base(sqlDialect, store)
 {
 }
 public SerializeManyToOne(SqlDialect sqlDialect, EntityAccessorStore store)
     : base(sqlDialect, store)
 {
 }
Beispiel #9
0
 protected RelationSerializer(SqlDialect sqlDialect, EntityAccessorStore store)
 {
     this.sqlDialect = sqlDialect;
     this.store      = store;
 }
Beispiel #10
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);
        }