Esempio n. 1
0
        /// <summary>
        /// Creator UpdateBuilder By entity
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        /// <remarks>
        /// Auto Add ModifyFields
        /// </remarks>
        public static UpdateBuilder <TResult> Create <TResult>(TResult entity) where TResult : class
        {
            var builder = new UpdateBuilder <TResult>(entity);

            if (entity != null)
            {
                builder.AutoModifyFields(entity);
            }

            return(builder);
        }
Esempio n. 2
0
        /// <summary>
        /// Update data for table with a specified Expression.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="builder"></param>
        /// <param name="predicate"></param>
        /// <param name="table"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public static int Update <TResult>(this IDbConnection connection,
                                           UpdateBuilder <TResult> builder, Expression <Func <TResult, bool> > predicate, string table,
                                           IDbTransaction transaction = null, int?commandTimeout = null)
            where TResult : class
        {
            StringBuilder sqlSb;
            Dictionary <string, object> parameters;

            InternalGetUpdateUBExprParam(connection, builder, predicate, table, out sqlSb, out parameters);

            return(connection.Execute(sqlSb.ToString(), parameters, transaction, commandTimeout));
        }
Esempio n. 3
0
        private static void InternalGetUpdateUBExprParam <TResult>(IDbConnection connection,
                                                                   UpdateBuilder <TResult> builder, Expression <Func <TResult, bool> > predicate, string table,
                                                                   out StringBuilder sqlSb, out Dictionary <string, object> parameters) where TResult : class
        {
            ThrowHelper.ThrowIfNull(connection, "connection");
            ThrowHelper.ThrowIfNull(builder, "builder");
            ThrowHelper.ThrowIfNull(predicate, "predicate");
            ThrowHelper.ThrowIfNull(table, "table");

            var adapter          = GetFormatter(connection);
            var data             = builder.ToDictionary();
            var updateProperties = data.Select(p => p.Key);
            var updateFields     = string.Join(",", updateProperties.Select(p => adapter.AppendColumnNameEqualsValue(p)));

            sqlSb = new StringBuilder();
            sqlSb.AppendFormat("UPDATE {0} SET {1}", adapter.AppendColumnName(table), updateFields);

            #region where

            var wherePropertyInfos = GetPropertyInfos <TResult>();
            var sqlGenerator       = new SqlGenerator <TResult>(adapter, wherePropertyInfos);
            var sqlQuery           = sqlGenerator.GetWhereQuery(predicate);

            IList <KeyValuePair <string, object> > conditionObj = null;
            if (sqlQuery != null && sqlQuery.SqlBuilder != null && sqlQuery.Condition != null)
            {
                sqlSb.Append(sqlQuery.SqlBuilder);
                conditionObj = sqlQuery.Condition as IList <KeyValuePair <string, object> >; //IList<KeyValuePair<string, object>>
            }

            #endregion

            parameters = new Dictionary <string, object>(data);
            if (conditionObj != null)
            {
                parameters.AddRange(conditionObj);
            }
        }