From 88ec514630b2a3737c50abf58dab49e2a1516be2 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 29 Sep 2014 14:38:03 +1000 Subject: [PATCH] fixes entity repo merges, this also means that the n+1 fixes for entity repository will be in this release. --- .../Repositories/EntityRepository.cs | 107 +++++++++++------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/EntityRepository.cs b/src/Umbraco.Core/Persistence/Repositories/EntityRepository.cs index 530c679f7a..838a6bbe23 100644 --- a/src/Umbraco.Core/Persistence/Repositories/EntityRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/EntityRepository.cs @@ -141,41 +141,56 @@ namespace Umbraco.Core.Persistence.Repositories { if (ids.Any()) { - foreach (var id in ids) + return PerformGetAll(objectTypeId, sql1 => sql1.Where(" umbracoNode.id in (@ids)", new {ids = ids})); + } + else + { + return PerformGetAll(objectTypeId); + } + } + + public virtual IEnumerable GetAll(Guid objectTypeId, params Guid[] keys) + { + if (keys.Any()) + { + return PerformGetAll(objectTypeId, sql1 => sql1.Where(" umbracoNode.uniqueID in (@keys)", new { keys = keys })); + } + else + { + return PerformGetAll(objectTypeId); + } + } + + private IEnumerable PerformGetAll(Guid objectTypeId, Action filter = null) + { + bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document); + bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media); + var sql = GetFullSqlForEntityType(isContent, isMedia, objectTypeId, filter); + + var factory = new UmbracoEntityFactory(); + + if (isMedia) + { + //for now treat media differently + //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields + var entities = _work.Database.Fetch( + new UmbracoEntityRelator().Map, sql); + foreach (var entity in entities) { - yield return Get(id, objectTypeId); + yield return entity; } } else { - bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document); - bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media); - var sql = GetFullSqlForEntityType(isContent, isMedia, objectTypeId, string.Empty); - - var factory = new UmbracoEntityFactory(); - - if (isMedia) + var dtos = _work.Database.Fetch(sql); + foreach (var entity in dtos.Select(dto => factory.BuildEntityFromDynamic(dto))) { - //for now treat media differently - //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields - var entities = _work.Database.Fetch( - new UmbracoEntityRelator().Map, sql); - foreach (var entity in entities) - { - yield return entity; - } - } - else - { - var dtos = _work.Database.Fetch(sql); - foreach (var entity in dtos.Select(dto => factory.BuildEntityFromDynamic(dto))) - { - yield return entity; - } + yield return entity; } } } + public virtual IEnumerable GetByQuery(IQuery query) { //TODO: We need to fix all of this and how it handles parameters! @@ -272,16 +287,16 @@ namespace Umbraco.Core.Persistence.Repositories return GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false))); } - protected Sql GetFullSqlForEntityType(bool isContent, bool isMedia, Guid objectTypeId, string additionalWhereClause) + protected Sql GetFullSqlForEntityType(bool isContent, bool isMedia, Guid objectTypeId, Action filter) { - var entitySql = GetBaseWhere(GetBase, isContent, isMedia, additionalWhereClause, objectTypeId); + var entitySql = GetBaseWhere(GetBase, isContent, isMedia, filter, objectTypeId); if (isMedia == false) return entitySql.Append(GetGroupBy(isContent, false)); - return GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false))); + return GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false)), filter); } - private Sql GetFullSqlForMedia(Sql entitySql, string additionWhereStatement = "") + private Sql GetFullSqlForMedia(Sql entitySql, Action filter = null) { //this will add any dataNvarchar property to the output which can be added to the additional properties @@ -294,7 +309,12 @@ namespace Umbraco.Core.Persistence.Repositories .On(dto => dto.Id, dto => dto.PropertyTypeId) .InnerJoin() .On(dto => dto.DataTypeId, dto => dto.DataTypeId) - .Where("umbracoNode.nodeObjectType = @nodeObjectType" + additionWhereStatement, new {nodeObjectType = Constants.ObjectTypes.Media}); + .Where("umbracoNode.nodeObjectType = @nodeObjectType", new {nodeObjectType = Constants.ObjectTypes.Media}); + + if (filter != null) + { + filter(joinSql); + } //We're going to create a query to query against the entity SQL // because we cannot group by nText columns and we have a COUNT in the entitySql we cannot simply left join @@ -310,7 +330,7 @@ namespace Umbraco.Core.Persistence.Repositories return wrappedSql; } - protected virtual Sql GetBase(bool isContent, bool isMedia, string additionWhereStatement = "") + protected virtual Sql GetBase(bool isContent, bool isMedia, Action customFilter) { var columns = new List { @@ -358,43 +378,48 @@ namespace Umbraco.Core.Persistence.Repositories .On("umbracoNode.id = latest.nodeId"); } + if (customFilter != null) + { + customFilter(entitySql); + } + return entitySql; } - protected virtual Sql GetBaseWhere(Func baseQuery, bool isContent, bool isMedia, string additionWhereStatement, Guid nodeObjectType) + protected virtual Sql GetBaseWhere(Func, Sql> baseQuery, bool isContent, bool isMedia, Action filter, Guid nodeObjectType) { - var sql = baseQuery(isContent, isMedia, additionWhereStatement) + var sql = baseQuery(isContent, isMedia, filter) .Where("umbracoNode.nodeObjectType = @NodeObjectType", new { NodeObjectType = nodeObjectType }); return sql; } - protected virtual Sql GetBaseWhere(Func baseQuery, bool isContent, bool isMedia, int id) + protected virtual Sql GetBaseWhere(Func, Sql> baseQuery, bool isContent, bool isMedia, int id) { - var sql = baseQuery(isContent, isMedia, " AND umbracoNode.id = '"+ id +"'") + var sql = baseQuery(isContent, isMedia, null) .Where("umbracoNode.id = @Id", new { Id = id }) .Append(GetGroupBy(isContent, isMedia)); return sql; } - protected virtual Sql GetBaseWhere(Func baseQuery, bool isContent, bool isMedia, Guid key) + protected virtual Sql GetBaseWhere(Func, Sql> baseQuery, bool isContent, bool isMedia, Guid key) { - var sql = baseQuery(isContent, isMedia, " AND umbracoNode.uniqueID = '" + key + "'") + var sql = baseQuery(isContent, isMedia, null) .Where("umbracoNode.uniqueID = @UniqueID", new { UniqueID = key }) .Append(GetGroupBy(isContent, isMedia)); return sql; } - protected virtual Sql GetBaseWhere(Func baseQuery, bool isContent, bool isMedia, Guid nodeObjectType, int id) + protected virtual Sql GetBaseWhere(Func, Sql> baseQuery, bool isContent, bool isMedia, Guid nodeObjectType, int id) { - var sql = baseQuery(isContent, isMedia, " AND umbracoNode.id = '"+ id +"'") + var sql = baseQuery(isContent, isMedia, null) .Where("umbracoNode.id = @Id AND umbracoNode.nodeObjectType = @NodeObjectType", new {Id = id, NodeObjectType = nodeObjectType}); return sql; } - protected virtual Sql GetBaseWhere(Func baseQuery, bool isContent, bool isMedia, Guid nodeObjectType, Guid key) + protected virtual Sql GetBaseWhere(Func, Sql> baseQuery, bool isContent, bool isMedia, Guid nodeObjectType, Guid key) { - var sql = baseQuery(isContent, isMedia, " AND umbracoNode.uniqueID = '" + key + "'") + var sql = baseQuery(isContent, isMedia, null) .Where("umbracoNode.uniqueID = @UniqueID AND umbracoNode.nodeObjectType = @NodeObjectType", new { UniqueID = key, NodeObjectType = nodeObjectType }); return sql;