Пример #1
0
        /// <summary>
        /// Gets a command text from the cache for the <see cref="DbConnectionExtension.Merge{TEntity}(IDbConnection, TEntity, IEnumerable{Field}, 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 GetMergeText <TEntity>(MergeRequest 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  = EnsureStatementBuilder(request.Connection, request.StatementBuilder);
                if (statementBuilder is SqlStatementBuilder)
                {
                    var sqlStatementBuilder = (SqlStatementBuilder)statementBuilder;
                    if (isPrimaryIdentity == false)
                    {
                        isPrimaryIdentity = PrimaryKeyIdentityCache.Get <TEntity>(request.Connection.ConnectionString);
                    }
                    commandText = sqlStatementBuilder.CreateMerge(queryBuilder: new QueryBuilder <TEntity>(),
                                                                  qualifiers: request.Qualifiers,
                                                                  isPrimaryIdentity: isPrimaryIdentity);
                }
                else
                {
                    commandText = statementBuilder.CreateMerge(queryBuilder: new QueryBuilder <TEntity>(),
                                                               qualifiers: request.Qualifiers);
                }
                m_cache.TryAdd(request, commandText);
            }
            return(commandText);
        }
Пример #2
0
        /// <summary>
        /// Gets a command text from the cache for the <see cref="DbConnectionExtension.Insert{TEntity}(IDbConnection, TEntity, 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 GetInsertText <TEntity>(InsertRequest 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.Insert);
                    }
                    commandText = sqlStatementBuilder.CreateInsert(queryBuilder: new QueryBuilder <TEntity>(),
                                                                   isPrimaryIdentity: isPrimaryIdentity);
                }
                else
                {
                    commandText = statementBuilder.CreateInsert(queryBuilder: new QueryBuilder <TEntity>());
                }
                m_cache.TryAdd(request, commandText);
            }
            return(commandText);
        }