Пример #1
0
        /// <summary>
        /// 指定实体类型映射器、视图映射器和条件创建一个查询的命令。
        /// </summary>
        /// <param name="entityMapper">实体的类型映射器。</param>
        /// <param name="viewMapper">视图的类型映射器。</param>
        /// <param name="where">条件参数。</param>
        /// <param name="top">指定 TOP 数量,小于 1 则忽略作用。</param>
        /// <param name="tunnel">用于个性化表名和命令的暗道,可以为 null 值。</param>
        /// <returns>一个查询命令。</returns>
        public virtual ExecuteCommand CreateQueryCommand(TypeMapper entityMapper, TypeMapper viewMapper, WhereParameters where, int top = 0, ICommandTunnel tunnel = null)
        {
            if(where == null) throw new ArgumentNullException(nameof(where));
            if(tunnel == null) tunnel = Empty;

            var fields = this.CreateFields(entityMapper, viewMapper);
            if(top > 0) fields = string.Concat("TOP ", top.ToString(), " ", fields);

            var commandText = string.Concat("SELECT ", fields, " FROM ", this.EscapeName(tunnel.GetTableName(entityMapper), NamePoint.Table));
            return tunnel.GetCommand(entityMapper, new ExecuteCommand(where.AppendTo(commandText), where.Parameters));
        }
Пример #2
0
        /// <summary>
        /// 指定类型映射器和条件参数创建一个表总行数的命令。
        /// </summary>
        /// <param name="mapper">类型映射器。</param>
        /// <param name="where">条件参数。</param>
        /// <param name="tunnel">用于个性化表名和命令的暗道,可以为 null 值。</param>
        /// <returns>一个查询命令。</returns>
        public virtual ExecuteCommand CreateRowCountCommand(TypeMapper mapper, WhereParameters where, ICommandTunnel tunnel = null)
        {
            if(mapper == null) throw new ArgumentNullException(nameof(mapper));
            if(where == null) throw new ArgumentNullException(nameof(where));
            if(tunnel == null) tunnel = Empty;

            var commandText = "SELECT COUNT(*) FROM " + this.EscapeName(tunnel.GetTableName(mapper), NamePoint.Table);
            return tunnel.GetCommand(mapper, new ExecuteCommand(where.AppendTo(commandText), where.Parameters));
        }
Пример #3
0
        /// <summary>
        /// 指定类型映射器和实体创建一个更新的命令。
        /// </summary>
        /// <param name="mapper">类型映射器。</param>
        /// <param name="entity">实体的实例对象。</param>
        /// <param name="where">条件参数。</param>
        /// <param name="tunnel">用于个性化表名和命令的暗道,可以为 null 值。</param>
        /// <returns>一个查询命令。</returns>
        public virtual ExecuteCommand CreateUpdateCommand(TypeMapper mapper, object entity, WhereParameters where, ICommandTunnel tunnel = null)
        {
            if(mapper == null) throw new ArgumentNullException(nameof(mapper));
            if(entity == null) throw new ArgumentNullException(nameof(entity));
            if(where == null) throw new ArgumentNullException(nameof(where));
            if(tunnel == null) tunnel = Empty;

            var setBuilder = new StringBuilder("UPDATE ")
                                .Append(this.EscapeName(tunnel.GetTableName(mapper), NamePoint.Table))
                                .Append(" SET ");
            var ps = where.Parameters ?? new ExecuteParameterCollection(mapper.Count);

            int index = 0;
            foreach(var property in FindProperties(mapper, ref entity))
            {
                if(property.IsIgnore || property.IsKey) continue;

                if(index++ > 0) setBuilder.Append(',');

                setBuilder.Append(this.EscapeName(property.Name, NamePoint.Field))
                          .Append('=');
                var value = property.GetValue(entity);
                this.AppendParameterValue(property, setBuilder, value, ps);
            }
            if(index==0) throw new NotSupportedException($"{entity.GetType().FullName} 的更新操作没有找到任何属性。");

            return tunnel.GetCommand(mapper, new ExecuteCommand(where.AppendTo(setBuilder.ToString()), ps));
        }