IBatisNet的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Data.SqlClient;

using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using IBatisNet.DataMapper.Exceptions;

using WHTR.Domain;
using WHTR.DataTypes;
using WHTR.OptimisticLocking;

namespace WHTR.IBatisNet
{
    /// <summary>
    /// 扩展类型。
    /// </summary>
    public static class SqlMapExtension
    {
        /// <summary>
        /// 插入。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static object Insert(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.Insert(statementId, paramValue);
        }

        /// <summary>
        /// 更新。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static int Update(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.Update(statementId, paramValue);
        }

        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static int Delete(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.Delete(statementId, paramValue);
        }

        /// <summary>
        /// 标量查询。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static object ExecuteScalar(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.QueryForObject(statementId, paramValue);
        }

        /// <summary>
        /// 标量查询。
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.QueryForObject<T>(statementId, paramValue);
        }

        /// <summary>
        /// 单记录查询。
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static T Get<T>(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.QueryForObject<T>(statementId, paramValue);
        }

        /// <summary>
        /// 多记录查询。
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <returns></returns>
        public static IList<T> GetList<T>(this ISqlMapper mapper, string statementId)
        {
            return mapper.QueryForList<T>(statementId, null);
        }

        /// <summary>
        /// 多记录查询。
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static IList<T> GetList<T>(this ISqlMapper mapper, string statementId, object paramValue)
        {
            return mapper.QueryForList<T>(statementId, paramValue);
        }

        /// <summary>
        /// 获取 SQL 字符串。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static string GetSql(this ISqlMapper mapper, string statementId, object paramValue)
        {
            IMappedStatement statement = mapper.GetMappedStatement(statementId);
            if (!mapper.IsSessionStarted)
            {
                mapper.OpenConnection();
            }

            RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramValue, mapper.LocalSession);

            return scope.PreparedStatement.PreparedSql;
        }

        /// <summary>
        /// 批量提交。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="action">The action.</param>
        public static void Commit(this ISqlMapper mapper, Action action)
        {
            Commit(mapper, action, null);
        }

        /// <summary>
        /// 批量提交,可自定义抛出的异常信息。
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="action">The action.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <exception cref="System.ApplicationException"></exception>
        public static void Commit(this ISqlMapper mapper, Action action, string errorMessage)
        {
            try
            {
                mapper.BeginTransaction();
                action();
                mapper.CommitTransaction();
            }
            catch (DataMapperException ex)
            {
                mapper.RollBackTransaction();

                if (null == errorMessage)
                {
                    throw ex;
                }
                else
                {
                    throw new ApplicationException(errorMessage, ex);
                }
            }
        }

        /// <summary>
        /// Gets the list with page.
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="pagerStatementId">The pager statement identifier.</param>
        /// <param name="pagerCountStatementId">The pager count statement identifier.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <returns></returns>
        public static IPagedResult<T> GetListWithPage<T>(this ISqlMapper mapper, string pagerStatementId, string pagerCountStatementId, IPageQueryParam paramValue)
        {
            var totalCount = mapper.QueryForObject<int>(pagerCountStatementId, paramValue);
            var lst = mapper.QueryForList<T>(pagerStatementId, paramValue);

            return new PagedResult<T>(lst, paramValue.PageIndex, paramValue.PageSize, totalCount);
        }

        /// <summary>
        /// Gets the list with page.
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="mapper">The mapper.</param>
        /// <param name="pagerStatementId">The pager statement identifier.</param>
        /// <param name="pagerCountStatementId">The pager count statement identifier.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="paramValue">The parameter value.</param>
        /// <param name="startIndexKeyName">Start name of the index key.</param>
        /// <param name="endIndexKeyName">End name of the index key.</param>
        /// <returns></returns>
        public static IPagedResult<T> GetListWithPage<T>(
                                        this ISqlMapper mapper,
                                        string pagerStatementId,
                                        string pagerCountStatementId,
                                        int pageIndex,
                                        int pageSize,
                                        Hashtable paramValue,
                                        string startIndexKeyName = "StartIndex",
                                        string endIndexKeyName = "EndIndex")
        {
            Hashtable newParamValue = new Hashtable();

            var pageQueryParam = new PageQueryParam(pageIndex, pageSize);
            newParamValue.Add(startIndexKeyName, pageQueryParam.StartIndex);
            newParamValue.Add(endIndexKeyName, pageQueryParam.EndIndex);

            if (paramValue != null)
            {
                foreach (DictionaryEntry item in paramValue)
                {
                    newParamValue.Add(item.Key, item.Value);
                }
            }

            var totalCount = mapper.QueryForObject<int>(pagerCountStatementId, pageQueryParam);
            var lst = mapper.QueryForList<T>(pagerStatementId, pageQueryParam);

            return new PagedResult<T>(lst, pageIndex, pageSize, totalCount);
        }

        /// <summary>
        /// 获取乐观锁
        /// </summary>
        /// <param name="mapper">mapper</param>
        /// <param name="table">表名</param>
        /// <param name="versionField">"版本号"的字段名</param>
        /// <param name="primaryKeyField">"标识主键"的字段名</param>
        /// <param name="primaryKeyValue">"标识主键"的值</param>
        /// <returns></returns>
        public static ILocker GetLocker(
                                        this ISqlMapper mapper,
                                        string table,
                                        string versionField,
                                        string primaryKeyField,
                                        int primaryKeyValue)
        {
            var currentVersion = 0;

            using (var connection = new SqlConnection(mapper.DataSource.ConnectionString))
            {
                connection.Open();
                var cmd = connection.CreateCommand();
                cmd.CommandText = string.Format("SELECT {0} FROM {1} WHERE {2} = {3}", versionField, table, primaryKeyField, primaryKeyValue);
                var reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    currentVersion = int.Parse(reader[0].ToString());
                }
            }

            return new IBatisNetLocker(mapper, table, currentVersion, versionField, primaryKeyValue, primaryKeyField);
        }

        /// <summary>
        /// Updates the with concurrency check.
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="statementId">Name of the statement.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="OptmisticConcurrencyException">并发异常</exception>
        public static void UpdateWithConcurrencyCheck(this ISqlMapper mapper, string statementId, IVersionable entity)
        {
            var result = mapper.Update(statementId, entity);

            if (result == 0)
            {
                throw new OptmisticConcurrencyException("并发异常");
            }

            entity.Version++;
        }
    }
}

 

posted @ 2017-07-11 16:47  Jara  阅读(171)  评论(0编辑  收藏  举报