Try your search with a different keyword or use * as a wildcard.
BaseDataProvider.cs
using System.Data.Common;
using System.Linq.Expressions;
using System.Reflection;
using FluentMigrator;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.DataProvider;
using LinqToDB.Tools;
using Nop.Core;
using Nop.Core.Infrastructure;
using Nop.Data.Mapping;
using Nop.Data.Migrations;
namespace Nop.Data.DataProviders;
public abstract partial class BaseDataProvider
{
#region Utilities
///
/// Gets a connection to the database for a current data provider
///
/// Connection string
/// Connection to a database
protected abstract DbConnection GetInternalDbConnection(string connectionString);
///
/// Creates the database connection
///
protected virtual DataConnection CreateDataConnection()
{
return CreateDataConnection(LinqToDbDataProvider);
}
///
/// Creates the database connection
///
/// Data provider
/// Database connection
protected virtual DataConnection CreateDataConnection(IDataProvider dataProvider)
{
ArgumentNullException.ThrowIfNull(dataProvider);
var dataConnection = new DataConnection(dataProvider, CreateDbConnection(), NopMappingSchema.GetMappingSchema(ConfigurationName, LinqToDbDataProvider))
{
CommandTimeout = DataSettingsManager.GetSqlCommandTimeout()
};
return dataConnection;
}
///
/// Creates a connection to a database
///
/// Connection string
/// Connection to a database
protected virtual DbConnection CreateDbConnection(string connectionString = null)
{
return GetInternalDbConnection(!string.IsNullOrEmpty(connectionString) ? connectionString : GetCurrentConnectionString());
}
///
/// Gets a data hash from database side
///
/// Array for a hashing function
/// Data hash
///
/// For SQL Server 2014 (12.x) and earlier, allowed input values are limited to 8000 bytes.
/// https://docs.microsoft.com/en-us/sql/t-sql/functions/hashbytes-transact-sql
///
[Sql.Expression("CONVERT(VARCHAR(128), HASHBYTES('SHA2_512', SUBSTRING({0}, 0, 8000)), 2)", ServerSideOnly = true, Configuration = ProviderName.SqlServer)]
[Sql.Expression("SHA2({0}, 512)", ServerSideOnly = true, Configuration = ProviderName.MySql)]
[Sql.Expression("encode(digest({0}, 'sha512'), 'hex')", ServerSideOnly = true, Configuration = ProviderName.PostgreSQL)]
protected static string SqlSha2(object binaryData)
{
throw new InvalidOperationException("This function should be used only in database code");
}
#endregion
#region Methods
///
/// Initialize database
///
public virtual void InitializeDatabase()
{
var migrationManager = EngineContext.Current.Resolve();
var targetAssembly = typeof(NopDbStartup).Assembly;
migrationManager.ApplyUpMigrations(targetAssembly);
var typeFinder = Singleton.Instance;
var mAssemblies = typeFinder.FindClassesOfType()
.Select(t => t.Assembly)
.Where(assembly => !assembly.FullName?.Contains("FluentMigrator.Runner") ?? false)
.Distinct()
.ToArray();
//mark update migrations as applied
foreach (var assembly in mAssemblies)
migrationManager.ApplyUpMigrations(assembly, MigrationProcessType.Update, true);
}
///
/// Creates a new temporary storage and populate it using data from provided query
///
/// Name of temporary storage
/// Query to get records to populate created storage with initial data
/// Storage record mapping class
///
/// A task that represents the asynchronous operation
/// The task result contains the iQueryable instance of temporary storage
///
public virtual Task> CreateTempDataStorageAsync(string storeKey, IQueryable query)
where TItem : class
{
return Task.FromResult>(new TempSqlDataStorage(storeKey, query, CreateDataConnection()));
}
///
/// Get hash values of a stored entity field
///
/// A function to test each element for a condition.
/// A key selector which should project to a dictionary key
/// A field selector to apply a transform to a hash value
/// Entity type
/// Dictionary
public virtual async Task> GetFieldHashesAsync(Expression> predicate,
Expression> keySelector,
Expression> fieldSelector) where TEntity : BaseEntity
{
if (keySelector.Body is not MemberExpression keyMember ||
keyMember.Member is not PropertyInfo keyPropInfo)
{
throw new ArgumentException($"Expression '{keySelector}' refers to method or field, not a property.");
}
if (fieldSelector.Body is not MemberExpression member ||
member.Member is not PropertyInfo propInfo)
{
throw new ArgumentException($"Expression '{fieldSelector}' refers to a method or field, not a property.");
}
var hashes = GetTable()
.Where(predicate)
.Select(x => new
{
Id = Sql.Property(x, keyPropInfo.Name),
Hash = SqlSha2(Sql.Property