Example #1
0
        /// <summary>
        /// 创建一个条件查询语句。
        /// </summary>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="ps">参数集合实例。</param>
        /// <param name="binary">二元运算符。</param>
        /// <returns>返回一个条件查询语句。</returns>
        public static string CreateWhere(this IDbEngine engine, ExecuteParameterCollection ps, string binary = "AND")
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }
            if (ps == null || ps.Count == 0)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(binary))
            {
                throw new ArgumentNullException("binary");
            }

            var parSetting = engine.Owner.Injector.ParameterSettings;
            var builder    = new StringBuilder();
            int index      = 0;

            foreach (var p in ps)
            {
                if (index++ > 0)
                {
                    builder.Append(' ').Append(binary).Append(' ');
                }
                builder
                .Append(p.Name)
                .Append('=').Append(parSetting.PrefixWithText)
                .Append(p.Name);
            }
            return(builder.ToString());
        }
Example #2
0
        /// <summary>
        /// 创建一个条件查询语句。
        /// </summary>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="ps">参数集合实例。</param>
        /// <param name="binary">二元运算符。</param>
        /// <returns>条件查询语句。</returns>
        public static string CreateWhere(this IDbEngine engine, ExecuteParameterCollection ps, string binary = "AND")
        {
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            if (ps == null || ps.Count == 0)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(binary))
            {
                throw new ArgumentNullException(nameof(binary));
            }

            var factory = engine.Provider.SqlFactory;
            var builder = new StringBuilder();
            int index   = 0;

            foreach (var p in ps)
            {
                if (index++ > 0)
                {
                    builder.Append(' ').Append(binary).Append(' ');
                }
                var name = factory.UnescapeName(p.Name);
                builder
                .Append(factory.EscapeName(name, NamePoint.Field))
                .Append('=').Append(factory.EscapeName(name, NamePoint.Value));
            }
            return(builder.ToString());
        }
Example #3
0
 /// <summary>
 /// 判断指定的条件的数据是否已存在。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="ps">参数集合实例。</param>
 public static Result <bool> ExistsWhere <TEntity>(this IDbEngine engine, ExecuteParameterCollection ps)
 {
     if (ps == null || ps.Count == 0)
     {
         throw new ArgumentNullException("objectInstance");
     }
     return(ExistsWhere <TEntity>(engine, CreateWhere(engine, ps), ps));
 }
Example #4
0
        /// <summary>
        /// 提供匹配条件,执行一个更新的命令,可以是匿名对象的部分成员(<paramref name="entity"/> 属性成员和 <typeparamref name="TEntity"/> 属性成员必须一致)。
        /// </summary>
        /// <typeparam name="TEntity">实体的数据类型。</typeparam>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="entity">实体的实例对象,可以是匿名对象的部分成员(<paramref name="entity"/> 属性成员和 <typeparamref name="TEntity"/> 属性成员必须一致)。</param>
        /// <param name="where">条件表达式。</param>
        /// <param name="ps">参数集合实例。</param>
        public static DbResult <int> ModifyWhere <TEntity>(this IDbEngine engine, object entity, string where, ExecuteParameterCollection ps = null)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var mapper1     = TypeMapper.Instance <TEntity> .Mapper;
            var mapper2     = TypeMapper.Create(entity.GetType());
            var parSettings = engine.Owner.Injector.ParameterSettings;
            var setBuilder  = new StringBuilder("UPDATE ")
                              .Append(parSettings.EscapeName(mapper1.Name))
                              .Append(" SET ");
            int index = 0;

            if (ps == null)
            {
                ps = new ExecuteParameterCollection();
            }
            foreach (var property2 in mapper2.Properties)
            {
                if (property2.IsIgnore || property2.IsKey)
                {
                    continue;
                }
                if (!mapper1.Contains(property2.Name))
                {
                    throw new NotSupportedException("表 {0} 没有属性 {1}。".Fmt(mapper1.Name, property2.Name));
                }
                if (index > 0)
                {
                    setBuilder.Append(',');
                }

                setBuilder.Append(parSettings.EscapeName(property2.Name))
                .Append('=')
                .Append(parSettings.PrefixWithText)
                .Append("_value_")
                .Append(index);
                ps.Add(parSettings.PrefixWithCollection + "_value_" + index, property2.GetValue(entity));
                index++;
            }
            if (where != null)
            {
                setBuilder.Append(" WHERE ").Append(where);
            }
            return(engine.Execute(setBuilder.ToString(), ps).ToNonQuery());
        }
Example #5
0
        /// <summary>
        /// 指定类型映射器和实体创建一个插入的命令。
        /// </summary>
        /// <param name="mapper">类型映射器。</param>
        /// <param name="entity">实体的实例对象。</param>
        /// <param name="tunnel">用于个性化表名和命令的暗道,可以为 null 值。</param>
        /// <returns>查询命令。</returns>
        public virtual ExecuteCommand CreateInsertCommand(TypeMapper mapper, object entity, ICommandTunnel tunnel = null)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (tunnel == null)
            {
                tunnel = Empty;
            }

            if (mapper.Count == 0)
            {
                throw new NotSupportedException($"{entity.GetType().FullName} 的插入操作没有找到任何属性。");
            }

            var fieldsBuilder = new StringBuilder("INSERT INTO ")
                                .Append(this.EscapeName(tunnel.GetTableName(mapper), NamePoint.Table))
                                .Append('(');
            var valueBuilder = new StringBuilder(")VALUES(");
            var ps           = new ExecuteParameterCollection(mapper.Count);
            int index        = 0;

            foreach (var property in FindProperties(mapper, ref entity))
            {
                if (property.IsIgnore)
                {
                    continue;
                }
                var value = property.GetValue(entity, true);
                if (property.IsKey && object.Equals(value, property.TypeDefaultValue))
                {
                    continue;
                }

                if (index++ > 0)
                {
                    fieldsBuilder.Append(',');
                    valueBuilder.Append(',');
                }
                fieldsBuilder.Append(this.EscapeName(property.Name, NamePoint.Field));
                this.AppendParameterValue(property, valueBuilder, value, ps);
            }
            return(tunnel.GetCommand(mapper, new ExecuteCommand(fieldsBuilder.Append(valueBuilder.Append(')').ToString()).ToString(), ps)));
        }
Example #6
0
 /// <summary>
 /// 获取数据表的总行数。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="ps">参数集合。</param>
 /// <returns>返回一个查询结果。</returns>
 public static DbResult <long> RowCount <TEntity>(ExecuteParameterCollection ps)
 {
     return(Engine.RowCount <TEntity>(ps));
 }
Example #7
0
 /// <summary>
 /// 判断指定的条件的数据是否已存在。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="ps">参数集合实例。</param>
 public static Result <bool> ExistsWhere <TEntity>(ExecuteParameterCollection ps)
 {
     return(Engine.ExistsWhere <TEntity>(ps));
 }
Example #8
0
 /// <summary>
 /// 提供匹配条件,获取对象的列表。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <typeparam name="TView">视图的数据类型。</typeparam>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <List <TView> > FindAllWhere <TEntity, TView>(string where, ExecuteParameterCollection ps = null)
 {
     return(Engine.FindAllWhere <TEntity, TView>(where, ps));
 }
Example #9
0
 /// <summary>
 /// 生成执行数据源查询与交互的执行器。
 /// </summary>
 /// <param name="commandText">Transact-SQL 语句。第一个字符为“>”时,表示一个存储过程。</param>
 /// <param name="parameters">参数集合。</param>
 /// <returns>返回执行数据源查询与交互的执行器。</returns>
 public static IDbExecutor Execute(string commandText, ExecuteParameterCollection parameters)
 {
     return(Engine.Execute(commandText, parameters));
 }
Example #10
0
 /// <summary>
 /// 提供匹配条件,获取一个对象。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <TEntity> FindOneWhere <TEntity>(string where, ExecuteParameterCollection ps = null)
 {
     return(Engine.FindOneWhere <TEntity>(where, ps));
 }
Example #11
0
 /// <summary>
 /// 创建一个筛选执行器。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <param name="binary">二元运算符。</param>
 /// <returns>筛选执行器。</returns>
 public static IFilterExecutor Filter(this IDbEngine engine, ExecuteParameterCollection ps, string binary = "AND")
 => Filter(engine, CreateWhere(engine, ps, binary), ps);
Example #12
0
 public SqlFormatter(ExecuteParameterCollection parameters, IDbEngineProvider engineProvider)
 {
     _parameters = parameters;
     _engineProvider = engineProvider;
 }
Example #13
0
 public SqlFormatProvider(ExecuteParameterCollection parameters, IDbEngineProvider engineProvider)
 {
     _formatter = new SqlFormatter(parameters, engineProvider);
 }
Example #14
0
        /// <summary>
        /// 将一个复合格式字符串转换为 <see cref="ExecuteCommand"/> 的对象实例。
        /// </summary>
        /// <param name="fs">一个复合格式字符串</param>
        /// <returns><see cref="ExecuteCommand"/> 的对象实例。</returns>
        public ExecuteCommand Parse(FormattableString fs)
        {
            if(fs == null) throw new ArgumentNullException(nameof(fs));

            var parameters = new ExecuteParameterCollection();
            var sfp = new SqlFormatProvider(parameters, this.Provider);
            var text = fs.ToString(sfp);
            return new ExecuteCommand(text, parameters);
        }
Example #15
0
 /// <summary>
 /// 创建一个条件查询语句。
 /// </summary>
 /// <param name="ps">参数集合实例。</param>
 /// <param name="binary">二元运算符。</param>
 /// <returns>返回一个条件查询语句。</returns>
 public static string CreateWhere(ExecuteParameterCollection ps, string binary = "AND")
 {
     return(Engine.CreateWhere(ps, binary));
 }
Example #16
0
 /// <summary>
 /// 初始化一个 <see cref="WhereParameters"/> 类的新实例。
 /// </summary>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">条件表达式的参数集合。</param>
 public WhereParameters(string where, ExecuteParameterCollection ps = null)
 {
     this.Where      = where;
     this.Parameters = ps;
 }
Example #17
0
 /// <summary>
 /// 执行一个以分页方式查找多项的命令模型。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <typeparam name="TView">视图的数据类型。</typeparam>
 /// <param name="bus">命令总线。</param>
 /// <param name="page">分页的数据。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <param name="binary">二元运算符。</param>
 /// <returns>返回一个实体的分页集合。</returns>
 public static GridData <TView> FindAllPage <TEntity, TView>(this ICommandBus bus, IPagination page, ExecuteParameterCollection ps, string binary = "AND")
 {
     return(FindAllPage <TEntity, TView>(bus, page, WhereParameters.Parse(bus.GetDbEngine(), ps, binary)));
 }
Example #18
0
        internal static WhereParameters GetRemoveWhere <TEntity>(this IDbEngine engine, object entityOrPKValues)
        {
            if (engine == null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            if (entityOrPKValues == null)
            {
                throw new ArgumentNullException(nameof(entityOrPKValues));
            }

            /*
             * 1、Remove(val)
             * 2、Remove({key=val})
             * 3、Remove({key=val1},val2)
             * 4、Remove([{key1=val2,key2=val2},{key1=val3,key2=val4}])
             */

            var mapper = TypeMapper.Instance <TEntity> .Mapper.ThrowWithNotFoundKeys();

            if (!(entityOrPKValues is string) && entityOrPKValues is Collections.IEnumerable)
            {
                entityOrPKValues = ((Collections.IEnumerable)entityOrPKValues).Cast <object>().ToArray();
            }

            var type = entityOrPKValues.GetType();

            if (mapper.KeyProperties.Length == 1)
            {//- 单个主键,1,2,3
                var keyName = mapper.KeyProperties[0].Name;

                if (type.IsArray)
                {//-3
                    var           array     = (Array)entityOrPKValues;
                    List <object> newValues = new List <object>(array.Length);
                    foreach (var item in array)
                    {
                        var itemType = item.GetType();
                        if (itemType.IsSimpleType())
                        {
                            newValues.Add(item);
                        }
                        else
                        {
                            var keyValue = TypeMapper.Create(itemType)[keyName]?.GetValue(item, true);
                            if (keyValue == null)
                            {
                                throw CreateKeyNotFoundException <TEntity>(keyName);
                            }
                            newValues.Add(keyValue);
                        }
                    }
                    var builder = new SqlBuilder(engine);
                    builder.Where(keyName, keyName, newValues.ToArray());
                    return(new WhereParameters(builder.WhereText, builder.Parameters));
                }
                if (!type.IsSimpleType())
                {//-2
                    entityOrPKValues = TypeMapper.Create(type)[keyName]?.GetValue(entityOrPKValues, true);
                    if (entityOrPKValues == null)
                    {
                        throw CreateKeyNotFoundException <TEntity>(keyName);
                    }
                }
                //- 1,2
                var ps = new ExecuteParameterCollection(keyName, entityOrPKValues);
                return(new WhereParameters(CreateWhere(engine, ps), ps));
            }
            else
            {//- 多个主键,4
                if (!type.IsArray)
                {
                    throw new NotSupportedException($"类型“{typeof(TEntity).FullName}”执行删除命令时,{nameof(entityOrPKValues)} 参数必须是一个数组。");
                }

                var array   = (Array)entityOrPKValues;
                var builder = new SqlBuilder(engine);
                var factory = engine.Provider.SqlFactory;
                builder.Where();
                foreach (var item in array)
                {
                    var itemType = item.GetType();
                    if (itemType.IsSimpleType())
                    {
                        throw new NotSupportedException($"类型“{typeof(TEntity).FullName}”包含多个主键,{nameof(entityOrPKValues)} 参数所有值都必须是一个复杂对象。");
                    }
                    else
                    {
                        var itemMapper = TypeMapper.Create(itemType);
                        builder.Or().BeginGroup();

                        foreach (var keyProp in mapper.KeyProperties)
                        {
                            var tmpKV = itemMapper[keyProp.Name]?.GetValue(item, true);
                            if (tmpKV == null)
                            {
                                throw CreateKeyNotFoundException <TEntity>(keyProp.Name);
                            }
                            builder.AndValue(keyProp.Name, tmpKV);
                        }
                        builder.EndGroup();
                    }
                }
                return(new WhereParameters(builder.WhereText, builder.Parameters));
            }
        }
Example #19
0
 /// <summary>
 /// 执行一个查询条件是否存在的命令模型。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="bus">命令总线。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <returns>返回一个值,表示数据是否存在。</returns>
 public static bool ExistsWhere <TEntity>(this ICommandBus bus, string where, ExecuteParameterCollection ps = null)
 {
     return(ExistsWhere <TEntity>(bus, new WhereParameters(where, ps)));
 }
Example #20
0
 /// <summary>
 /// 提供匹配条件,执行一个删除的命令。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <int> RemoveWhere <TEntity>(ExecuteParameterCollection ps)
 {
     return(Engine.RemoveWhere <TEntity>(ps));
 }
Example #21
0
 /// <summary>
 /// 执行一个一个获取查询条件的数据表行数的命令模型。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="bus">命令总线。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <returns>返回数据的行数。</returns>
 public static long RowCount <TEntity>(this ICommandBus bus, string where, ExecuteParameterCollection ps = null)
 {
     return(RowCount <TEntity>(bus, new WhereParameters(where, ps)));
 }
Example #22
0
 /// <summary>
 /// 提供匹配条件,获取一个对象。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <typeparam name="TView">视图的数据类型。</typeparam>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <TView> FindOneWhere <TEntity, TView>(ExecuteParameterCollection ps = null)
 {
     return(Engine.FindOneWhere <TEntity, TView>(ps));
 }
Example #23
0
 /// <summary>
 /// 执行一个一个获取查询条件的数据表行数的命令模型。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="bus">命令总线。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <param name="binary">二元运算符。</param>
 /// <returns>返回数据的行数。</returns>
 public static long RowCount <TEntity>(this ICommandBus bus, ExecuteParameterCollection ps, string binary = "AND")
 {
     return(RowCount <TEntity>(bus, WhereParameters.Parse(bus.GetDbEngine(), ps, binary)));
 }
Example #24
0
 /// <summary>
 /// 提供匹配条件,获取对象的列表。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <List <TEntity> > FindAllWhere <TEntity>(ExecuteParameterCollection ps = null)
 {
     return(Engine.FindAllWhere <TEntity>(ps));
 }
Example #25
0
 /// <summary>
 /// 执行一个条件的修改的命令模型。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="bus">命令总线。</param>
 /// <param name="entity">实体的实例。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <returns>返回受影响的行。</returns>
 public static int ModifyWhere <TEntity>(this ICommandBus bus, object entity, string where, ExecuteParameterCollection ps = null)
 {
     return(ModifyWhere <TEntity>(bus, entity, new WhereParameters(where, ps)));
 }
Example #26
0
 /// <summary>
 /// 提供匹配条件,获取对象的列表。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <typeparam name="TView">视图的数据类型。</typeparam>
 /// <param name="page">一个分页的实现。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <GridData <TView> > FindAllPage <TEntity, TView>(IPagination page, string where, ExecuteParameterCollection ps = null)
 {
     return(Engine.FindAllPage <TEntity, TView>(page, where, ps));
 }
Example #27
0
 /// <summary>
 /// 初始化一个 <see cref="WhereParameters"/> 类的新实例。
 /// </summary>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">条件表达式的参数集合。</param>
 public WhereParameters(string where, ExecuteParameterCollection ps = null)
 {
     this.Where = where;
     this.Parameters = ps;
 }
Example #28
0
 /// <summary>
 /// 判断指定的条件的数据是否已存在。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 public static Result <bool> ExistsWhere <TEntity>(string where, ExecuteParameterCollection ps = null)
 {
     return(Engine.ExistsWhere <TEntity>(where, ps));
 }
Example #29
0
 /// <summary>
 /// 解析参数集合,并用 AND 符拼接 WHERE 语句。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="ps">参数集合。</param>
 /// <param name="binary">二元运算符。</param>
 public static WhereParameters Parse(IDbEngine engine, ExecuteParameterCollection ps = null, string binary = "AND")
     => new WhereParameters(engine.CreateWhere(ps, binary), ps);
Example #30
0
 /// <summary>
 /// 获取数据表的总行数。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="where">筛选条件,不含 WHERE 关键字。</param>
 /// <param name="ps">参数集合。</param>
 /// <returns>返回一个查询结果。</returns>
 public static DbResult <long> RowCount <TEntity>(string where = null, ExecuteParameterCollection ps = null)
 {
     return(Engine.RowCount <TEntity>(where, ps));
 }
Example #31
0
 /// <summary>
 /// 创建作为当前实例副本的新对象。
 /// </summary>
 /// <returns>作为此实例副本的新对象。</returns>
 public object Clone()
 {
     ExecuteParameterCollection newParameters = null;
     if(this._Parameters != null)
     {
         newParameters = new ExecuteParameterCollection(this._Parameters.Count);
         foreach(var p in this._Parameters)
         {
             newParameters.Add(p.Clone() as IExecuteParameter);
         }
     }
     return new ExecuteCommand(this.Text, newParameters);
 }
Example #32
0
 /// <summary>
 /// 解析参数集合,并用 AND 符拼接 WHERE 语句。
 /// </summary>
 /// <param name="ps">参数集合。</param>
 /// <param name="binary">二元运算符。</param>
 public static WhereParameters Parse(ExecuteParameterCollection ps = null, string binary = "AND")
 {
     return(Parse(Db.Engine, ps, binary));
 }
Example #33
0
 /// <summary>
 /// 指定查询字符串和查询参数,初始化一个 <see cref="ExecuteCommand"/> 类的新实例。
 /// </summary>
 /// <param name="text">Transact-SQL 语句。第一个字符为“>”时,表示一个存储过程。</param>
 /// <param name="parameters">参数集合。</param>
 public ExecuteCommand(string text, ExecuteParameterCollection parameters)
 {
     this.Text = text;
     this._Parameters = parameters;
 }
Example #34
0
 /// <summary>
 /// 解析参数集合,并用 AND 符拼接 WHERE 语句。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="ps">参数集合。</param>
 /// <param name="binary">二元运算符。</param>
 public static WhereParameters Parse(IDbEngine engine, ExecuteParameterCollection ps = null, string binary = "AND")
 => new WhereParameters(engine.CreateWhere(ps, binary), ps);
Example #35
0
 /// <summary>
 /// 追加指定属性映射器名称到 <paramref name="builder"/>,并将其参数和值添加到集合。
 /// </summary>
 /// <param name="property">属性映射器。</param>
 /// <param name="builder">字符串生成器。</param>
 /// <param name="value">属性的值。</param>
 /// <param name="ps">参数集合。</param>
 protected virtual void AppendParameterValue(PropertyMapper property, StringBuilder builder, object value, ExecuteParameterCollection ps)
 {
     var upperName = property.Name.ToUpper();
     builder.Append(this.EscapeName(upperName, NamePoint.Value));
     ps.Add(this.EscapeName(upperName, NamePoint.Parameter), value);
 }
Example #36
0
 /// <summary>
 /// 创建一个筛选执行器。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 /// <returns>筛选执行器。</returns>
 public static IFilterExecutor Filter(this IDbEngine engine, string where, ExecuteParameterCollection ps)
 => Filter(engine, new WhereParameters(where, ps));
Example #37
0
        /// <summary>
        /// 指定类型映射器和实体创建一个插入的命令。
        /// </summary>
        /// <param name="mapper">类型映射器。</param>
        /// <param name="entity">实体的实例对象。</param>
        /// <param name="tunnel">用于个性化表名和命令的暗道,可以为 null 值。</param>
        /// <returns>一个查询命令。</returns>
        public virtual ExecuteCommand CreateInsertCommand(TypeMapper mapper, object entity, ICommandTunnel tunnel = null)
        {
            if(mapper == null) throw new ArgumentNullException(nameof(mapper));
            if(entity == null) throw new ArgumentNullException(nameof(entity));
            if(tunnel == null) tunnel = Empty;

            if(mapper.Count == 0) throw new NotSupportedException($"{entity.GetType().FullName} 的插入操作没有找到任何属性。");

            var fieldsBuilder = new StringBuilder("INSERT INTO ")
                                .Append(this.EscapeName(tunnel.GetTableName(mapper), NamePoint.Table))
                                .Append('(');
            var valueBuilder = new StringBuilder(")VALUES(");
            var ps = new ExecuteParameterCollection(mapper.Count);
            int index = 0;
            foreach(var property in FindProperties(mapper, ref entity))
            {
                if(property.IsIgnore) continue;
                var value = property.GetValue(entity);
                if(property.IsKey && object.Equals(value, property.TypeDefaultValue)) continue;

                if(index++ > 0)
                {
                    fieldsBuilder.Append(',');
                    valueBuilder.Append(',');
                }
                fieldsBuilder.Append(this.EscapeName(property.Name, NamePoint.Field));
                this.AppendParameterValue(property, valueBuilder, value, ps);
            }
            return tunnel.GetCommand(mapper, new ExecuteCommand(fieldsBuilder.Append(valueBuilder.Append(')').ToString()).ToString(), ps));
        }
Example #38
0
 /// <summary>
 /// 生成执行数据源查询与交互的执行器。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="commandText">Transact-SQL 语句。第一个字符为“>”时,表示一个存储过程。</param>
 /// <param name="parameters">参数集合。</param>
 /// <returns>执行数据源查询与交互的执行器。</returns>
 public static IDbExecutor Execute(this IDbEngine engine, string commandText, ExecuteParameterCollection parameters)
 {
     return(engine.Execute(new ExecuteCommand(commandText, parameters)));
 }
Example #39
0
 /// <summary>
 /// 提供匹配条件,执行一个更新的命令,可以是匿名对象的部分成员(<paramref name="entity"/> 属性成员和 <typeparamref name="TEntity"/> 属性成员必须一致)。
 /// </summary>
 /// <typeparam name="TEntity">实体的数据类型。</typeparam>
 /// <param name="entity">实体的实例对象,可以是匿名对象的部分成员(<paramref name="entity"/> 属性成员和 <typeparamref name="TEntity"/> 属性成员必须一致)。</param>
 /// <param name="where">条件表达式。</param>
 /// <param name="ps">参数集合实例。</param>
 public static DbResult <int> ModifyWhere <TEntity>(object entity, string where, ExecuteParameterCollection ps = null)
 {
     return(Engine.ModifyWhere <TEntity>(entity, where, ps));
 }