Exemplo n.º 1
0
        /// <summary>
        /// 根据具体Model对象生成Update参数及子句
        /// </summary>
        /// <param name="value">更新Model对象,用于生成Update部分,属性为null值的不作为生成条件</param>
        /// <param name="condition">条件Model对象,用于生成Where部分,属性为null值的不作为生成条件</param>
        /// <param name="isLikeMode">是否模糊查询</param>
        /// <returns>Update参数及子句</returns>
        public virtual ParmCollection PrepareUpdateParms(object value, bool isLikeMode = false, object condition = null)
        {
            ParmCollection temp = new ParmCollection();

            string update = string.Empty;

            foreach (IDataParameter p in this.Parameters)
            {
                foreach (DBFieldInfoAttribute f in this.Fields)
                {
                    if (f.ColumnName == p.SourceColumn && f.Field.GetValue(value) != null && f.SqlDbType != SqlDbType.Timestamp)
                    {
                        p.Value = f.Field.GetValue(value);
                        update += string.Format("{0}={1}U, ", f.ColumnNameFix, f.ParameterName);
                        IDataParameter pp = this.CloneParameter(p);
                        pp.ParameterName = f.ParameterName + "U";
                        temp.Add(pp);
                        break;
                    }
                }
            }

            if (update.Length == 0)
            {
                return(temp);
            }

            ParmCollection where = this.PrepareConditionParms(condition, isLikeMode);

            update          = string.Format("UPDATE {0} SET {1}  OUTPUT INSERTED.*  {2}", this.TableName, update.Substring(0, update.Length - 2), "{0}");
            where.UpdateSql = update;

            where.AddRange(temp);
            return(where);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据具体Model对象获取非自动增量的参数集合
        /// </summary>
        /// <param name="o">Model对象</param>
        /// <returns>非自动增量的参数集合</returns>
        public virtual ParmCollection PrepareNotIdentityParms(object o)
        {
            ParmCollection temp = new ParmCollection();

            foreach (IDataParameter p in this.Parameters)
            {
                foreach (DBFieldInfoAttribute f in this.Fields)
                {
                    if (!f.IsIdentity && f.ColumnName == p.SourceColumn)
                    {
                        object v = f.Field.GetValue(o);
                        if (f.IsKey && v == null)
                        {
                            v = Guid.NewGuid();
                        }
                        if (f.DefaultValue != null)
                        {
                            if (f.DefaultValue.GetType().IsEnum)
                            {
                                f.DefaultValue = (int)f.DefaultValue;
                            }
                        }
                        p.Value  = v == null ? (f.DefaultValue == null ? DBNull.Value : f.DefaultValue) : v;
                        p.DbType = v == null ? (f.DefaultValue == null ? p.DbType : DbType.String) : p.DbType;
                        temp.Add(p);
                        break;
                    }
                }
            }

            return(temp);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据具体Model对象生成Where参数及子句
        /// 对象中属性为null值的不作为生成条件
        /// </summary>
        /// <param name="o">Model对象</param>
        /// <param name="or">条件连接是否使用or,true为使用or,false为使用and</param>
        /// <param name="isLikeMode">是否模糊查询</param>
        /// <returns>Where参数及子句</returns>
        public virtual ParmCollection PrepareSearchParms(object o, bool or, bool isLikeMode)
        {
            ParmCollection temp = new ParmCollection();

            if (o == null)
            {
                return(temp);
            }

            string where = string.Empty;
            foreach (IDataParameter p in this.Parameters)
            {
                foreach (DBFieldInfoAttribute f in this.Fields)
                {
                    object val = f.Field.GetValue(o);
                    if (f.ColumnName == p.SourceColumn && val != null && val.ToString() != string.Empty)
                    {
                        if (f.Field.FieldType == typeof(byte[]))
                        {//字段为byte[0]时不做查询条件
                            var byteVal = (byte[])val;
                            if (byteVal.Length == 0)
                            {
                                break;
                            }
                        }
                        string link;
                        if (f.Field.FieldType == typeof(string) && isLikeMode)
                        {
                            val  = val.ToString().Replace("[", "[[]").Replace("%", "[%]").Replace("^", "[^]").Replace("_", "[_]");
                            link = "LIKE";
                            if (f.LikeEqual == EnumLikeMode.NoLike)
                            {
                                link = "=";
                            }
                            if (f.LikeEqual == EnumLikeMode.AllLike)
                            {
                                val = "%" + val + "%";
                            }
                            if (f.LikeEqual == EnumLikeMode.BackwardLike)
                            {
                                val = val + "%";
                            }
                            if (f.LikeEqual == EnumLikeMode.ForwardLike)
                            {
                                val = "%" + val;
                            }
                        }
                        else
                        {
                            val  = f.Field.GetValue(o);
                            link = "=";
                        }

                        p.Value = val;
                        where  += string.Format("{0} {1} {2}   {3} ", f.ColumnNameFix, link, f.ParameterName, or ? "OR" : "AND");
                        temp.Add(p);
                        break;
                    }
                }
            }

            if (where.Length > 0)
            {
                where         = string.Format("{0}{1}", " WHERE ", where.Substring(0, where.Length - 5));
                temp.WhereSql = where;
            }

            return(temp);
        }