Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="where"></param>
        public WhereClipBuilder(WhereClip where)
        {
            expressionStringBuilder.Append(where.ToString());

            parameters.AddRange(where.Parameters);

        }
Ejemplo n.º 2
0
        /// <summary>
        /// AND
        /// </summary>
        /// <param name="where"></param>
        public void And(WhereClip where)
        {
            if (WhereClip.IsNullOrEmpty(where))
                return;


            if (expressionStringBuilder.Length > 0)
            {
                expressionStringBuilder.Append(" AND ");
                expressionStringBuilder.Append(where.ToString());
                expressionStringBuilder.Append(")");
                expressionStringBuilder.Insert(0, "(");
            }
            else
            {
                expressionStringBuilder.Append(where.ToString());
            }

            parameters.AddRange(where.Parameters);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Or
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public WhereClip Or(WhereClip where)
        {
            if (WhereClip.IsNullOrEmpty(this) && WhereClip.IsNullOrEmpty(where))
                return All;

            if (WhereClip.IsNullOrEmpty(where))
                return this;
            if (WhereClip.IsNullOrEmpty(this))
                return where;

            WhereClip orwhere = new WhereClip(string.Concat(this.Where, " OR ", where.Where));
            orwhere.parameters.AddRange(this.Parameters);
            orwhere.parameters.AddRange(where.Parameters);


            return orwhere;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// And
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public WhereClip And(WhereClip where)
        {
            if (WhereClip.IsNullOrEmpty(this) && WhereClip.IsNullOrEmpty(where))
                return All;

            if (WhereClip.IsNullOrEmpty(where))
                return this;
            if (WhereClip.IsNullOrEmpty(this))
                return where;



            WhereClip andwhere = new WhereClip(string.Concat(this.Where, " AND ", where.Where));
            andwhere.parameters.AddRange(this.Parameters);
            andwhere.parameters.AddRange(where.Parameters);


            return andwhere;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 判断 WhereClip  是否为null
 /// </summary>
 /// <param name="whereClip"></param>
 /// <returns></returns>
 public static bool IsNullOrEmpty(WhereClip whereClip)
 {
     if ((null == whereClip) || string.IsNullOrEmpty(whereClip.expressionString))
         return true;
     return false;
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 判断条件是否一样
        /// </summary>
        /// <param name="leftWhere"></param>
        /// <param name="rightWhere"></param>
        /// <returns></returns>
        public static bool Equals(WhereClip leftWhere, WhereClip rightWhere)
        {
            string leftWhereString = leftWhere.ToString();
            string rightWhereString = rightWhere.ToString();

            foreach (Parameter p in leftWhere.parameters)
            {
                leftWhereString.Replace(p.ParameterName, (p.ParameterValue == null) ? string.Empty : p.ParameterValue.ToString());
            }

            foreach (Parameter p in rightWhere.parameters)
            {
                rightWhereString.Replace(p.ParameterName, (p.ParameterValue == null) ? string.Empty : p.ParameterValue.ToString());
            }

            return (string.Compare(leftWhereString, rightWhereString, true) == 0);
        }
Ejemplo n.º 7
0
 /// <summary>
 ///  删除
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Delete <TEntity>(WhereClip where)
     where TEntity : Entity
 {
     return(dbSession.Delete <TEntity>(trans, where));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 更新多个值
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="fieldValue"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <TEntity>(Dictionary <Field, object> fieldValue, WhereClip where)
     where TEntity : Entity
 {
     return(dbSession.Update <TEntity>(trans, fieldValue, where));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 更新
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="fields"></param>
 /// <param name="values"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <TEntity>(Field[] fields, object[] values, WhereClip where)
     where TEntity : Entity
 {
     return(dbSession.Update <TEntity>(trans, fields, values, where));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 更新单个值
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="field"></param>
 /// <param name="value"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <TEntity>(Field field, object value, WhereClip where)
     where TEntity : Entity
 {
     return(dbSession.Update <TEntity>(trans, field, value, where));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 更新
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <TEntity>(TEntity entity, WhereClip where)
     where TEntity : Entity
 {
     return(dbSession.Update <TEntity>(trans, entity, where));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// 创建删除DbCommand
 /// </summary>
 /// <param name="where"></param>
 /// <returns></returns>
 public DbCommand CreateDeleteCommand <TEntity>(WhereClip where)
     where TEntity : Entity
 {
     return(CreateDeleteCommand(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>(), where));
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 创建更新DbCommand
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DbCommand CreateUpdateCommand <TEntity>(Field[] fields, object[] values, WhereClip where)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            if (null == fields || fields.Length == 0 || null == values || values.Length == 0)
            {
                return(null);
            }

            Check.Require(fields.Length == values.Length, "fields.Length must be equal values.Length");

            var length = fields.Length;

            if (WhereClip.IsNullOrEmpty(where))
            {
                where = WhereClip.All;
            }

            var sql = new StringBuilder();

            sql.Append("UPDATE ");
            sql.Append(db.DbProvider.BuildTableName(EntityCache.GetTableName <TEntity>(), EntityCache.GetUserName <TEntity>()));
            sql.Append(" SET ");

            var identityField = EntityCache.GetIdentityField <TEntity>();
            var identityExist = !Field.IsNullOrEmpty(identityField);
            var list          = new List <Parameter>();
            var colums        = new StringBuilder();

            for (var i = 0; i < length; i++)
            {
                if (identityExist)
                {
                    //标识列  排除
                    if (fields[i].PropertyName.Equals(identityField.PropertyName))
                    {
                        continue;
                    }
                }

                colums.Append(",");
                colums.Append(fields[i].FieldName);
                colums.Append("=");

                if (values[i] is Expression)
                {
                    var expression = (Expression)values[i];
                    colums.Append(expression);
                    list.AddRange(expression.Parameters);
                }
                else if (values[i] is Field)
                {
                    var fieldValue = (Field)values[i];
                    colums.Append(fieldValue.TableFieldName);
                }
                else
                {
                    var pname = DataUtils.MakeUniqueKey(fields[i]);
                    //var pname = string.Concat("@", fields[i].Name, i);
                    colums.Append(pname);
                    var p = new Parameter(pname, values[i], fields[i].ParameterDbType, fields[i].ParameterSize);
                    list.Add(p);
                }
            }
            sql.Append(colums.ToString().Substring(1));
            sql.Append(where.WhereString);
            list.AddRange(where.Parameters);

            var cmd = db.GetSqlStringCommand(sql.ToString());

            db.AddCommandParameter(cmd, list.ToArray());
            return(cmd);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="where"></param>
        public WhereClipBuilder(WhereClip where)
        {
            expressionStringBuilder.Append(where.ToString());

            parameters.AddRange(where.Parameters);
        }