Skip to content

Commit bf5f136

Browse files
committed
Fix build after merge
1 parent 02bf616 commit bf5f136

File tree

13 files changed

+1017
-910
lines changed

13 files changed

+1017
-910
lines changed
Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,39 @@
11
using System.Collections.Generic;
22

3-
namespace Umbraco.Core.Collections
3+
namespace Umbraco.Cms.Core.Collections
44
{
55
/// <summary>
6-
/// Collection that can be both a queue and a stack.
6+
/// Collection that can be both a queue and a stack.
77
/// </summary>
88
/// <typeparam name="T"></typeparam>
99
public class StackQueue<T>
1010
{
11-
private readonly LinkedList<T> _linkedList = new LinkedList<T>();
11+
private readonly LinkedList<T> _linkedList = new();
1212

13-
public void Clear()
14-
{
15-
_linkedList.Clear();
16-
}
13+
public int Count => _linkedList.Count;
1714

18-
public void Push(T obj)
19-
{
20-
_linkedList.AddFirst(obj);
21-
}
15+
public void Clear() => _linkedList.Clear();
2216

23-
public void Enqueue(T obj)
24-
{
25-
_linkedList.AddFirst(obj);
26-
}
17+
public void Push(T obj) => _linkedList.AddFirst(obj);
18+
19+
public void Enqueue(T obj) => _linkedList.AddFirst(obj);
2720

2821
public T Pop()
2922
{
30-
var obj = _linkedList.First.Value;
23+
T obj = _linkedList.First.Value;
3124
_linkedList.RemoveFirst();
3225
return obj;
3326
}
3427

3528
public T Dequeue()
3629
{
37-
var obj = _linkedList.Last.Value;
30+
T obj = _linkedList.Last.Value;
3831
_linkedList.RemoveLast();
3932
return obj;
4033
}
4134

42-
public T PeekStack()
43-
{
44-
return _linkedList.First.Value;
45-
}
35+
public T PeekStack() => _linkedList.First.Value;
4636

47-
public T PeekQueue()
48-
{
49-
return _linkedList.Last.Value;
50-
}
51-
52-
public int Count
53-
{
54-
get
55-
{
56-
return _linkedList.Count;
57-
}
58-
}
37+
public T PeekQueue() => _linkedList.Last.Value;
5938
}
6039
}
Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,76 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Data.Common;
45
using System.Data.SqlClient;
56
using System.Linq;
67
using NPoco;
8+
using Umbraco.Cms.Core;
79
using Umbraco.Cms.Infrastructure.Persistence;
810

911
namespace Umbraco.Extensions
1012
{
1113
/// <summary>
12-
/// Provides extension methods to NPoco Database class.
14+
/// Provides extension methods to NPoco Database class.
1315
/// </summary>
1416
public static partial class NPocoDatabaseExtensions
1517
{
1618
/// <summary>
17-
/// Configures NPoco's SqlBulkCopyHelper to use the correct SqlConnection and SqlTransaction instances from the underlying RetryDbConnection and ProfiledDbTransaction
19+
/// Configures NPoco's SqlBulkCopyHelper to use the correct SqlConnection and SqlTransaction instances from the
20+
/// underlying RetryDbConnection and ProfiledDbTransaction
1821
/// </summary>
1922
/// <remarks>
20-
/// This is required to use NPoco's own <see cref="Database.InsertBulk{T}(IEnumerable{T})" /> method because we use wrapped DbConnection and DbTransaction instances.
21-
/// NPoco's InsertBulk method only caters for efficient bulk inserting records for Sql Server, it does not cater for bulk inserting of records for
22-
/// any other database type and in which case will just insert records one at a time.
23-
/// NPoco's InsertBulk method also deals with updating the passed in entity's PK/ID once it's inserted whereas our own BulkInsertRecords methods
24-
/// do not handle this scenario.
23+
/// This is required to use NPoco's own <see cref="Database.InsertBulk{T}(IEnumerable{T})" /> method because we use
24+
/// wrapped DbConnection and DbTransaction instances.
25+
/// NPoco's InsertBulk method only caters for efficient bulk inserting records for Sql Server, it does not cater for
26+
/// bulk inserting of records for
27+
/// any other database type and in which case will just insert records one at a time.
28+
/// NPoco's InsertBulk method also deals with updating the passed in entity's PK/ID once it's inserted whereas our own
29+
/// BulkInsertRecords methods
30+
/// do not handle this scenario.
2531
/// </remarks>
2632
public static void ConfigureNPocoBulkExtensions()
2733
{
28-
2934
SqlBulkCopyHelper.SqlConnectionResolver = dbConn => GetTypedConnection<SqlConnection>(dbConn);
3035
SqlBulkCopyHelper.SqlTransactionResolver = dbTran => GetTypedTransaction<SqlTransaction>(dbTran);
3136
}
3237

3338

3439
/// <summary>
35-
/// Creates bulk-insert commands.
40+
/// Creates bulk-insert commands.
3641
/// </summary>
3742
/// <typeparam name="T">The type of the records.</typeparam>
3843
/// <param name="database">The database.</param>
3944
/// <param name="records">The records.</param>
4045
/// <returns>The sql commands to execute.</returns>
4146
internal static IDbCommand[] GenerateBulkInsertCommands<T>(this IUmbracoDatabase database, T[] records)
4247
{
43-
if (database?.Connection == null) throw new ArgumentException("Null database?.connection.", nameof(database));
48+
if (database?.Connection == null)
49+
{
50+
throw new ArgumentException("Null database?.connection.", nameof(database));
51+
}
4452

45-
var pocoData = database.PocoDataFactory.ForType(typeof(T));
53+
PocoData pocoData = database.PocoDataFactory.ForType(typeof(T));
4654

4755
// get columns to include, = number of parameters per row
48-
var columns = pocoData.Columns.Where(c => IncludeColumn(pocoData, c)).ToArray();
56+
KeyValuePair<string, PocoColumn>[] columns =
57+
pocoData.Columns.Where(c => IncludeColumn(pocoData, c)).ToArray();
4958
var paramsPerRecord = columns.Length;
5059

5160
// format columns to sql
5261
var tableName = database.DatabaseType.EscapeTableName(pocoData.TableInfo.TableName);
53-
var columnNames = string.Join(", ", columns.Select(c => tableName + "." + database.DatabaseType.EscapeSqlIdentifier(c.Key)));
62+
var columnNames = string.Join(", ",
63+
columns.Select(c => tableName + "." + database.DatabaseType.EscapeSqlIdentifier(c.Key)));
5464

5565
// example:
5666
// assume 4168 records, each record containing 8 fields, ie 8 command parameters
5767
// max 2100 parameter per command
5868
// Math.Floor(2100 / 8) = 262 record per command
5969
// 4168 / 262 = 15.908... = there will be 16 command in total
6070
// (if we have disabled db parameters, then all records will be included, in only one command)
61-
var recordsPerCommand = paramsPerRecord == 0 ? int.MaxValue : Convert.ToInt32(Math.Floor(2000.00 / paramsPerRecord));
71+
var recordsPerCommand = paramsPerRecord == 0
72+
? int.MaxValue
73+
: Convert.ToInt32(Math.Floor((double)Constants.Sql.MaxParameterCount / paramsPerRecord));
6274
var commandsCount = Convert.ToInt32(Math.Ceiling((double)records.Length / recordsPerCommand));
6375

6476
var commands = new IDbCommand[commandsCount];
@@ -67,43 +79,42 @@ internal static IDbCommand[] GenerateBulkInsertCommands<T>(this IUmbracoDatabase
6779
var prefix = database.DatabaseType.GetParameterPrefix(database.ConnectionString);
6880
for (var commandIndex = 0; commandIndex < commandsCount; commandIndex++)
6981
{
70-
var command = database.CreateCommand(database.Connection, CommandType.Text, string.Empty);
82+
DbCommand command = database.CreateCommand(database.Connection, CommandType.Text, string.Empty);
7183
var parameterIndex = 0;
7284
var commandRecords = Math.Min(recordsPerCommand, recordsLeftToInsert);
7385
var recordsValues = new string[commandRecords];
74-
for (var commandRecordIndex = 0; commandRecordIndex < commandRecords; commandRecordIndex++, recordsIndex++, recordsLeftToInsert--)
86+
for (var commandRecordIndex = 0;
87+
commandRecordIndex < commandRecords;
88+
commandRecordIndex++, recordsIndex++, recordsLeftToInsert--)
7589
{
76-
var record = records[recordsIndex];
90+
T record = records[recordsIndex];
7791
var recordValues = new string[columns.Length];
7892
for (var columnIndex = 0; columnIndex < columns.Length; columnIndex++)
7993
{
8094
database.AddParameter(command, columns[columnIndex].Value.GetValue(record));
8195
recordValues[columnIndex] = prefix + parameterIndex++;
8296
}
97+
8398
recordsValues[commandRecordIndex] = "(" + string.Join(",", recordValues) + ")";
8499
}
85100

86-
command.CommandText = $"INSERT INTO {tableName} ({columnNames}) VALUES {string.Join(", ", recordsValues)}";
101+
command.CommandText =
102+
$"INSERT INTO {tableName} ({columnNames}) VALUES {string.Join(", ", recordsValues)}";
87103
commands[commandIndex] = command;
88104
}
89105

90106
return commands;
91107
}
92108

93109
/// <summary>
94-
/// Determines whether a column should be part of a bulk-insert.
110+
/// Determines whether a column should be part of a bulk-insert.
95111
/// </summary>
96112
/// <param name="pocoData">The PocoData object corresponding to the record's type.</param>
97113
/// <param name="column">The column.</param>
98114
/// <returns>A value indicating whether the column should be part of the bulk-insert.</returns>
99115
/// <remarks>Columns that are primary keys and auto-incremental, or result columns, are excluded from bulk-inserts.</remarks>
100-
public static bool IncludeColumn(PocoData pocoData, KeyValuePair<string, PocoColumn> column)
101-
{
102-
return column.Value.ResultColumn == false
103-
&& (pocoData.TableInfo.AutoIncrement == false || column.Key != pocoData.TableInfo.PrimaryKey);
104-
}
105-
106-
107-
116+
public static bool IncludeColumn(PocoData pocoData, KeyValuePair<string, PocoColumn> column) =>
117+
column.Value.ResultColumn == false
118+
&& (pocoData.TableInfo.AutoIncrement == false || column.Key != pocoData.TableInfo.PrimaryKey);
108119
}
109120
}

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/AuditEntryRepository.cs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using Microsoft.Extensions.Logging;
55
using NPoco;
6+
using Umbraco.Cms.Core;
67
using Umbraco.Cms.Core.Cache;
78
using Umbraco.Cms.Core.Models;
89
using Umbraco.Cms.Core.Persistence.Querying;
@@ -16,26 +17,47 @@
1617
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
1718
{
1819
/// <summary>
19-
/// Represents the NPoco implementation of <see cref="IAuditEntryRepository"/>.
20+
/// Represents the NPoco implementation of <see cref="IAuditEntryRepository" />.
2021
/// </summary>
2122
internal class AuditEntryRepository : EntityRepositoryBase<int, IAuditEntry>, IAuditEntryRepository
2223
{
2324
/// <summary>
24-
/// Initializes a new instance of the <see cref="AuditEntryRepository"/> class.
25+
/// Initializes a new instance of the <see cref="AuditEntryRepository" /> class.
2526
/// </summary>
2627
public AuditEntryRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger<AuditEntryRepository> logger)
2728
: base(scopeAccessor, cache, logger)
28-
{ }
29+
{
30+
}
31+
32+
/// <inheritdoc />
33+
public IEnumerable<IAuditEntry> GetPage(long pageIndex, int pageCount, out long records)
34+
{
35+
Sql<ISqlContext> sql = Sql()
36+
.Select<AuditEntryDto>()
37+
.From<AuditEntryDto>()
38+
.OrderByDescending<AuditEntryDto>(x => x.EventDateUtc);
39+
40+
Page<AuditEntryDto> page = Database.Page<AuditEntryDto>(pageIndex + 1, pageCount, sql);
41+
records = page.TotalItems;
42+
return page.Items.Select(AuditEntryFactory.BuildEntity);
43+
}
44+
45+
/// <inheritdoc />
46+
public bool IsAvailable()
47+
{
48+
var tables = SqlSyntax.GetTablesInSchema(Database).ToArray();
49+
return tables.InvariantContains(Constants.DatabaseSchema.Tables.AuditEntry);
50+
}
2951

3052
/// <inheritdoc />
3153
protected override IAuditEntry PerformGet(int id)
3254
{
33-
var sql = Sql()
55+
Sql<ISqlContext> sql = Sql()
3456
.Select<AuditEntryDto>()
3557
.From<AuditEntryDto>()
3658
.Where<AuditEntryDto>(x => x.Id == id);
3759

38-
var dto = Database.FirstOrDefault<AuditEntryDto>(sql);
60+
AuditEntryDto dto = Database.FirstOrDefault<AuditEntryDto>(sql);
3961
return dto == null ? null : AuditEntryFactory.BuildEntity(dto);
4062
}
4163

@@ -44,7 +66,7 @@ protected override IEnumerable<IAuditEntry> PerformGetAll(params int[] ids)
4466
{
4567
if (ids.Length == 0)
4668
{
47-
var sql = Sql()
69+
Sql<ISqlContext> sql = Sql()
4870
.Select<AuditEntryDto>()
4971
.From<AuditEntryDto>();
5072

@@ -53,9 +75,9 @@ protected override IEnumerable<IAuditEntry> PerformGetAll(params int[] ids)
5375

5476
var entries = new List<IAuditEntry>();
5577

56-
foreach (var group in ids.InGroupsOf(Constants.Sql.MaxParameterCount))
78+
foreach (IEnumerable<int> group in ids.InGroupsOf(Constants.Sql.MaxParameterCount))
5779
{
58-
var sql = Sql()
80+
Sql<ISqlContext> sql = Sql()
5981
.Select<AuditEntryDto>()
6082
.From<AuditEntryDto>()
6183
.WhereIn<AuditEntryDto>(x => x.Id, group);
@@ -69,68 +91,41 @@ protected override IEnumerable<IAuditEntry> PerformGetAll(params int[] ids)
6991
/// <inheritdoc />
7092
protected override IEnumerable<IAuditEntry> PerformGetByQuery(IQuery<IAuditEntry> query)
7193
{
72-
var sqlClause = GetBaseQuery(false);
94+
Sql<ISqlContext> sqlClause = GetBaseQuery(false);
7395
var translator = new SqlTranslator<IAuditEntry>(sqlClause, query);
74-
var sql = translator.Translate();
96+
Sql<ISqlContext> sql = translator.Translate();
7597
return Database.Fetch<AuditEntryDto>(sql).Select(AuditEntryFactory.BuildEntity);
7698
}
7799

78100
/// <inheritdoc />
79101
protected override Sql<ISqlContext> GetBaseQuery(bool isCount)
80102
{
81-
var sql = Sql();
103+
Sql<ISqlContext> sql = Sql();
82104
sql = isCount ? sql.SelectCount() : sql.Select<AuditEntryDto>();
83105
sql = sql.From<AuditEntryDto>();
84106
return sql;
85107
}
86108

87109
/// <inheritdoc />
88-
protected override string GetBaseWhereClause()
89-
{
90-
return $"{Cms.Core.Constants.DatabaseSchema.Tables.AuditEntry}.id = @id";
91-
}
110+
protected override string GetBaseWhereClause() => $"{Constants.DatabaseSchema.Tables.AuditEntry}.id = @id";
92111

93112
/// <inheritdoc />
94-
protected override IEnumerable<string> GetDeleteClauses()
95-
{
113+
protected override IEnumerable<string> GetDeleteClauses() =>
96114
throw new NotSupportedException("Audit entries cannot be deleted.");
97-
}
98115

99116
/// <inheritdoc />
100117
protected override void PersistNewItem(IAuditEntry entity)
101118
{
102119
entity.AddingEntity();
103120

104-
var dto = AuditEntryFactory.BuildDto(entity);
121+
AuditEntryDto dto = AuditEntryFactory.BuildDto(entity);
105122
Database.Insert(dto);
106123
entity.Id = dto.Id;
107124
entity.ResetDirtyProperties();
108125
}
109126

110127
/// <inheritdoc />
111-
protected override void PersistUpdatedItem(IAuditEntry entity)
112-
{
128+
protected override void PersistUpdatedItem(IAuditEntry entity) =>
113129
throw new NotSupportedException("Audit entries cannot be updated.");
114-
}
115-
116-
/// <inheritdoc />
117-
public IEnumerable<IAuditEntry> GetPage(long pageIndex, int pageCount, out long records)
118-
{
119-
var sql = Sql()
120-
.Select<AuditEntryDto>()
121-
.From<AuditEntryDto>()
122-
.OrderByDescending<AuditEntryDto>(x => x.EventDateUtc);
123-
124-
var page = Database.Page<AuditEntryDto>(pageIndex + 1, pageCount, sql);
125-
records = page.TotalItems;
126-
return page.Items.Select(AuditEntryFactory.BuildEntity);
127-
}
128-
129-
/// <inheritdoc />
130-
public bool IsAvailable()
131-
{
132-
var tables = SqlSyntax.GetTablesInSchema(Database).ToArray();
133-
return tables.InvariantContains(Cms.Core.Constants.DatabaseSchema.Tables.AuditEntry);
134-
}
135130
}
136131
}

0 commit comments

Comments
 (0)