Merge remote-tracking branch 'origin/v8/dev' into v8/feature/reintroduce-tabs

This commit is contained in:
Niels Lyngsø
2021-07-23 14:15:48 +02:00
28 changed files with 231 additions and 77 deletions

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Core.Persistence.Dtos
public const string TableName = Constants.DatabaseSchema.Tables.PropertyTypeGroup;
[Column("id")]
[PrimaryKeyColumn(IdentitySeed = 12)]
[PrimaryKeyColumn(IdentitySeed = 56)]
public int Id { get; set; }
[Column("uniqueID")]

View File

@@ -62,26 +62,33 @@ namespace Umbraco.Core.Persistence
/// <returns>The number of records that were inserted.</returns>
public static int BulkInsertRecords<T>(this IUmbracoDatabase database, IEnumerable<T> records, bool useNativeBulkInsert = true)
{
var recordsA = records.ToArray();
if (recordsA.Length == 0) return 0;
if (!records.Any()) return 0;
var pocoData = database.PocoDataFactory.ForType(typeof(T));
if (pocoData == null) throw new InvalidOperationException("Could not find PocoData for " + typeof(T));
if (database.DatabaseType.IsSqlCe())
{
if (useNativeBulkInsert) return BulkInsertRecordsSqlCe(database, pocoData, recordsA);
if (useNativeBulkInsert)
{
return BulkInsertRecordsSqlCe(database, pocoData, records);
}
// else, no other choice
foreach (var record in recordsA)
var count = 0;
foreach (var record in records)
{
database.Insert(record);
return recordsA.Length;
count++;
}
return count;
}
if (database.DatabaseType.IsSqlServer())
{
return useNativeBulkInsert && database.DatabaseType.IsSqlServer2008OrLater()
? BulkInsertRecordsSqlServer(database, pocoData, recordsA)
: BulkInsertRecordsWithCommands(database, recordsA);
? BulkInsertRecordsSqlServer(database, pocoData, records)
: BulkInsertRecordsWithCommands(database, records.ToArray());
}
throw new NotSupportedException();
}
@@ -96,7 +103,9 @@ namespace Umbraco.Core.Persistence
private static int BulkInsertRecordsWithCommands<T>(IUmbracoDatabase database, T[] records)
{
foreach (var command in database.GenerateBulkInsertCommands(records))
{
command.ExecuteNonQuery();
}
return records.Length; // what else?
}
@@ -241,6 +250,10 @@ namespace Umbraco.Core.Persistence
/// <returns>The number of records that were inserted.</returns>
internal static int BulkInsertRecordsSqlServer<T>(IUmbracoDatabase database, PocoData pocoData, IEnumerable<T> records)
{
// TODO: The main reason this exists is because the NPoco InsertBulk method doesn't return the number of items.
// It is worth investigating the performance of this vs NPoco's because we use a custom BulkDataReader
// which in theory should be more efficient than NPocos way of building up an in-memory DataTable.
// create command against the original database.Connection
using (var command = database.CreateCommand(database.Connection, CommandType.Text, string.Empty))
{
@@ -252,7 +265,13 @@ namespace Umbraco.Core.Persistence
var syntax = database.SqlContext.SqlSyntax as SqlServerSyntaxProvider;
if (syntax == null) throw new NotSupportedException("SqlSyntax must be SqlServerSyntaxProvider.");
using (var copy = new SqlBulkCopy(tConnection, SqlBulkCopyOptions.Default, tTransaction) { BulkCopyTimeout = 10000, DestinationTableName = tableName })
using (var copy = new SqlBulkCopy(tConnection, SqlBulkCopyOptions.Default, tTransaction)
{
BulkCopyTimeout = 0, // 0 = no bulk copy timeout. If a timeout occurs it will be an connection/command timeout.
DestinationTableName = tableName,
// be consistent with NPoco: https://github.com/schotime/NPoco/blob/5117a55fde57547e928246c044fd40bd00b2d7d1/src/NPoco.SqlServer/SqlBulkCopyHelper.cs#L50
BatchSize = 4096
})
using (var bulkReader = new PocoDataDataReader<T, SqlServerSyntaxProvider>(records, pocoData, syntax))
{
//we need to add column mappings here because otherwise columns will be matched by their order and if the order of them are different in the DB compared

View File

@@ -40,9 +40,10 @@ namespace Umbraco.Core.Persistence
_tableDefinition = DefinitionFactory.GetTableDefinition(pd.Type, sqlSyntaxProvider);
if (_tableDefinition == null) throw new InvalidOperationException("No table definition found for type " + pd.Type);
// only real columns, exclude result columns
// only real columns, exclude result/computed columns
// Like NPoco does: https://github.com/schotime/NPoco/blob/5117a55fde57547e928246c044fd40bd00b2d7d1/src/NPoco.SqlServer/SqlBulkCopyHelper.cs#L59
_readerColumns = pd.Columns
.Where(x => x.Value.ResultColumn == false)
.Where(x => x.Value.ResultColumn == false && x.Value.ComputedColumn == false)
.Select(x => x.Value)
.ToArray();

View File

@@ -352,7 +352,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax
sql.Append(" ");
sql.Append(FormatIdentity(column));
var isNullable = column.IsNullable;
//var isNullable = column.IsNullable;
//var constraint = FormatConstraint(column)?.TrimStart("CONSTRAINT ");
//var hasConstraint = !string.IsNullOrWhiteSpace(constraint);
@@ -360,11 +360,14 @@ namespace Umbraco.Core.Persistence.SqlSyntax
//var defaultValue = FormatDefaultValue(column);
//var hasDefaultValue = !string.IsNullOrWhiteSpace(defaultValue);
if (isNullable /*&& !hasConstraint && !hasDefaultValue*/)
{
sqls = Enumerable.Empty<string>();
return sql.ToString();
}
// TODO: This used to exit if nullable but that means this would never work
// to return SQL if the column was nullable?!? I don't get it. This was here
// 4 years ago, I've removed it so that this works for nullable columns.
//if (isNullable /*&& !hasConstraint && !hasDefaultValue*/)
//{
// sqls = Enumerable.Empty<string>();
// return sql.ToString();
//}
var msql = new List<string>();
sqls = msql;