/// <summary> /// Gets a command text from the cache for the <see cref="DbConnectionExtension.Query{TEntity}(IDbConnection, QueryGroup, IEnumerable{OrderField}, int?, string, int?, IDbTransaction, ICache, ITrace, IStatementBuilder, bool?, int?)"/> operation. /// </summary> /// <typeparam name="TEntity">The type of the target entity.</typeparam> /// <param name="request">The request object.</param> /// <returns>The cached command text.</returns> public static string GetQueryText <TEntity>(QueryRequest request) where TEntity : class { var commandText = (string)null; if (m_cache.TryGetValue(request, out commandText) == false) { var statementBuilder = (request.StatementBuilder ?? StatementBuilderMapper.Get(request.Connection?.GetType())?.StatementBuilder ?? new SqlDbStatementBuilder()); commandText = statementBuilder.CreateQuery(queryBuilder: new QueryBuilder <TEntity>(), where : request.Where, orderBy: request.OrderBy, top: request.Top); m_cache.TryAdd(request, commandText); } return(commandText); }
/// <summary> /// Gets a command text from the cache for the <see cref="DbConnectionExtension.Update{TEntity}(IDbConnection, TEntity, QueryGroup, int?, IDbTransaction, ITrace, IStatementBuilder)"/> operation. /// </summary> /// <typeparam name="TEntity">The type of the target entity.</typeparam> /// <param name="request">The request object.</param> /// <returns>The cached command text.</returns> public static string GetUpdateText <TEntity>(UpdateRequest request) where TEntity : class { var commandText = (string)null; if (m_cache.TryGetValue(request, out commandText) == false) { var statementBuilder = (request.StatementBuilder ?? StatementBuilderMapper.Get(request.Connection?.GetType())?.StatementBuilder ?? new SqlDbStatementBuilder()); commandText = statementBuilder.CreateUpdate(queryBuilder: new QueryBuilder <TEntity>(), where : request.Where); m_cache.TryAdd(request, commandText); } else { request.Where?.AppendParametersPrefix(); } return(commandText); }
/// <summary> /// Gets a command text from the cache for the <see cref="DbConnectionExtension.InlineMerge{TEntity}(IDbConnection, object, bool?, int?, IDbTransaction, ITrace, IStatementBuilder)"/> operation. /// </summary> /// <typeparam name="TEntity">The type of the target entity.</typeparam> /// <param name="request">The request object.</param> /// <returns>The cached command text.</returns> public static string GetInlineMergeText <TEntity>(InlineMergeRequest request) where TEntity : class { var commandText = (string)null; if (m_cache.TryGetValue(request, out commandText) == false) { var primary = PrimaryKeyCache.Get <TEntity>(); var identity = IdentityCache.Get <TEntity>(); if (identity != null && identity != primary) { throw new InvalidOperationException($"Identity property must be the primary property for type '{typeof(TEntity).FullName}'."); } var isPrimaryIdentity = (identity != null); var statementBuilder = (request.StatementBuilder ?? StatementBuilderMapper.Get(request.Connection?.GetType())?.StatementBuilder ?? new SqlDbStatementBuilder()); if (statementBuilder is SqlDbStatementBuilder) { var sqlStatementBuilder = (SqlDbStatementBuilder)statementBuilder; if (isPrimaryIdentity == false) { isPrimaryIdentity = PrimaryKeyIdentityCache.Get <TEntity>(request.Connection.ConnectionString, Command.InlineMerge); } commandText = sqlStatementBuilder.CreateInlineMerge(queryBuilder: new QueryBuilder <TEntity>(), fields: request.Fields, qualifiers: request.Qualifiers, overrideIgnore: request.OverrideIgnore, isPrimaryIdentity: isPrimaryIdentity); } else { commandText = statementBuilder.CreateInlineMerge(queryBuilder: new QueryBuilder <TEntity>(), fields: request.Fields, qualifiers: request.Qualifiers, overrideIgnore: request.OverrideIgnore); } m_cache.TryAdd(request, commandText); } return(commandText); }