2021-03-11 19:35:43 +11:00
|
|
|
using System;
|
2020-02-15 11:08:32 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-09-17 09:42:55 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-02-15 11:08:32 +01:00
|
|
|
using NPoco;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Persistence.Querying;
|
|
|
|
|
using Umbraco.Cms.Core.Persistence.Repositories;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
2021-06-08 10:58:17 -06:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Querying;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2020-02-15 11:08:32 +01:00
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
2020-02-15 11:08:32 +01:00
|
|
|
{
|
2020-12-22 10:30:16 +11:00
|
|
|
internal class KeyValueRepository : EntityRepositoryBase<string, IKeyValue>, IKeyValueRepository
|
2020-02-15 11:08:32 +01:00
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
public KeyValueRepository(IScopeAccessor scopeAccessor, ILogger<KeyValueRepository> logger)
|
2020-02-15 11:08:32 +01:00
|
|
|
: base(scopeAccessor, AppCaches.NoCache, logger)
|
|
|
|
|
{ }
|
|
|
|
|
|
2021-06-08 10:58:17 -06:00
|
|
|
/// <inheritdoc />
|
2021-06-11 11:14:43 -06:00
|
|
|
public IReadOnlyDictionary<string, string> FindByKeyPrefix(string keyPrefix)
|
2021-06-08 10:58:17 -06:00
|
|
|
=> Get(Query<IKeyValue>().Where(entity => entity.Identifier.StartsWith(keyPrefix)))
|
|
|
|
|
.ToDictionary(x => x.Identifier, x => x.Value);
|
|
|
|
|
|
2020-03-28 15:20:17 +01:00
|
|
|
#region Overrides of IReadWriteQueryRepository<string, IKeyValue>
|
|
|
|
|
|
|
|
|
|
public override void Save(IKeyValue entity)
|
|
|
|
|
{
|
|
|
|
|
if (Get(entity.Identifier) == null)
|
|
|
|
|
PersistNewItem(entity);
|
|
|
|
|
else
|
|
|
|
|
PersistUpdatedItem(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-12-22 10:30:16 +11:00
|
|
|
#region Overrides of EntityRepositoryBase<string, IKeyValue>
|
2020-02-15 11:08:32 +01:00
|
|
|
|
|
|
|
|
protected override Sql<ISqlContext> GetBaseQuery(bool isCount)
|
|
|
|
|
{
|
|
|
|
|
var sql = SqlContext.Sql();
|
|
|
|
|
|
|
|
|
|
sql = isCount
|
|
|
|
|
? sql.SelectCount()
|
|
|
|
|
: sql.Select<KeyValueDto>();
|
|
|
|
|
|
|
|
|
|
sql
|
|
|
|
|
.From<KeyValueDto>();
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 10:58:17 -06:00
|
|
|
protected override string GetBaseWhereClause() => Core.Constants.DatabaseSchema.Tables.KeyValue + ".key = @id";
|
2020-02-15 11:08:32 +01:00
|
|
|
|
2021-06-08 10:58:17 -06:00
|
|
|
protected override IEnumerable<string> GetDeleteClauses() => Enumerable.Empty<string>();
|
2020-02-15 11:08:32 +01:00
|
|
|
|
|
|
|
|
protected override IKeyValue PerformGet(string id)
|
|
|
|
|
{
|
2020-02-17 22:12:42 +01:00
|
|
|
var sql = GetBaseQuery(false).Where<KeyValueDto>(x => x.Key == id);
|
|
|
|
|
var dto = Database.Fetch<KeyValueDto>(sql).FirstOrDefault();
|
2020-02-15 11:08:32 +01:00
|
|
|
return dto == null ? null : Map(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<IKeyValue> PerformGetAll(params string[] ids)
|
|
|
|
|
{
|
|
|
|
|
var sql = GetBaseQuery(false).WhereIn<KeyValueDto>(x => x.Key, ids);
|
|
|
|
|
var dtos = Database.Fetch<KeyValueDto>(sql);
|
|
|
|
|
return dtos.WhereNotNull().Select(Map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<IKeyValue> PerformGetByQuery(IQuery<IKeyValue> query)
|
|
|
|
|
{
|
2021-06-08 10:58:17 -06:00
|
|
|
var sqlClause = GetBaseQuery(false);
|
|
|
|
|
var translator = new SqlTranslator<IKeyValue>(sqlClause, query);
|
|
|
|
|
var sql = translator.Translate();
|
|
|
|
|
return Database.Fetch<KeyValueDto>(sql).Select(Map);
|
2020-02-15 11:08:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PersistNewItem(IKeyValue entity)
|
|
|
|
|
{
|
|
|
|
|
var dto = Map(entity);
|
|
|
|
|
Database.Insert(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PersistUpdatedItem(IKeyValue entity)
|
|
|
|
|
{
|
|
|
|
|
var dto = Map(entity);
|
|
|
|
|
Database.Update(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static KeyValueDto Map(IKeyValue keyValue)
|
|
|
|
|
{
|
|
|
|
|
if (keyValue == null) return null;
|
|
|
|
|
|
|
|
|
|
return new KeyValueDto
|
|
|
|
|
{
|
|
|
|
|
Key = keyValue.Identifier,
|
|
|
|
|
Value = keyValue.Value,
|
|
|
|
|
UpdateDate = keyValue.UpdateDate,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IKeyValue Map(KeyValueDto dto)
|
|
|
|
|
{
|
|
|
|
|
if (dto == null) return null;
|
|
|
|
|
|
|
|
|
|
return new KeyValue
|
|
|
|
|
{
|
|
|
|
|
Identifier = dto.Key,
|
|
|
|
|
Value = dto.Value,
|
|
|
|
|
UpdateDate = dto.UpdateDate,
|
|
|
|
|
};
|
2020-09-17 09:42:55 +02:00
|
|
|
}
|
2020-02-15 11:08:32 +01:00
|
|
|
|
2020-09-17 09:42:55 +02:00
|
|
|
#endregion
|
2020-02-15 11:08:32 +01:00
|
|
|
}
|
|
|
|
|
}
|