Пример #1
0
        /// <summary>
        /// 根据ID删除记录
        /// </summary>
        /// <param name="id">要删除的记录ID</param>
        /// <returns></returns>
        public int DeleteById(object id)
        {
            int ret = -1;

            DeleteCondition con = new DeleteCondition(EntityInfo.DBInfo);

            con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName));
            ParamList list = new ParamList();

            ScopeList      lstScope = new ScopeList();
            PrimaryKeyInfo pkInfo   = id as PrimaryKeyInfo;

            if (pkInfo == null)
            {
                lstScope.AddEqual(EntityInfo.PrimaryProperty[0].PropertyName, id);
            }
            else
            {
                pkInfo.FillScope(EntityInfo.PrimaryProperty, lstScope, true);
            }
            con.Condition.Append("1=1");
            con.Condition.Append(DataAccessCommon.FillCondition(EntityInfo, list, lstScope));
            Dictionary <string, bool> cacheTables = null;

            cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName);

            ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables);
            return(ret);
        }
Пример #2
0
        /// <summary>
        /// 获取全部查询的条件
        /// </summary>
        /// <param name="list">参数列表</param>
        /// <param name="scopeList">范围查找的集合</param>
        /// <returns></returns>
        protected string GetSelectPageContant(ParamList list, ScopeList scopeList)
        {
            SelectCondition condition = new SelectCondition(CurEntityInfo.DBInfo);

            condition.Oper = this._oper;
            condition.Tables.Append(CurEntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(CurEntityInfo.TableName));
            condition.SqlParams.Append(GetSelectParams(scopeList));
            if (scopeList.UseCache)
            {
                condition.CacheTables = CurEntityInfo.DBInfo.QueryCache.CreateMap(CurEntityInfo.TableName);
            }
            condition.Condition.Append("1=1");
            foreach (EntityPropertyInfo ep in CurEntityInfo.PrimaryProperty)
            {
                condition.PrimaryKey.Add(CurEntityInfo.DBInfo.CurrentDbAdapter.FormatParam(ep.ParamName));
            }
            string conditionWhere = "";

            SortList sortList = scopeList.OrderBy;


            if (scopeList != null)
            {
                condition.Condition.Append(DataAccessCommon.FillCondition(CurEntityInfo, list, scopeList));
            }

            if (conditionWhere.Length > 0)
            {
                condition.Condition.Append(conditionWhere);
            }
            //排序方式
            if (sortList != null && sortList.Count > 0)
            {
                string orderBy = GetSortCondition(sortList);
                if (orderBy != "")
                {
                    if (condition.Orders.Length > 0)
                    {
                        condition.Orders.Append("," + orderBy);
                    }
                    else
                    {
                        condition.Orders.Append(orderBy);
                    }
                }
            }
            condition.PageContent = scopeList.PageContent;
            //throw new Exception("");
            condition.DbParamList = list;

            //if (scopeList.UseCache)
            //{

            //    cachetables[CurEntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(CurEntityInfo.TableName)]=true;
            //}

            return(condition.GetSql(true));
        }
Пример #3
0
        /// <summary>
        /// 获取全部查询的条件
        /// </summary>
        /// <param name="list">参数列表</param>
        /// <param name="scopeList">范围查找的集合</param>
        /// <param name="param">输出字段</param>
        /// <returns></returns>
        protected SelectCondition GetSelectContant(ParamList list, ScopeList scopeList, string param)
        {
            string          conditionWhere = "";
            string          orderBy        = "";
            SelectCondition condition      = new SelectCondition(CurEntityInfo.DBInfo);

            if (condition.SqlParams.Length > 0)
            {
                condition.SqlParams.Append(",");
            }
            condition.SqlParams.Append(param);

            condition.Tables.Append(CurEntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(CurEntityInfo.TableName));

            condition.Condition.Append("1=1");

            if (scopeList.UseCache)
            {
                condition.CacheTables = CurEntityInfo.DBInfo.QueryCache.CreateMap(CurEntityInfo.TableName);
            }

            if (scopeList != null)
            {
                condition.Condition.Append(DataAccessCommon.FillCondition(CurEntityInfo, list, scopeList));
            }
            if (conditionWhere.Length > 0)
            {
                condition.Condition.Append(conditionWhere);
            }
            SortList sortList = scopeList.OrderBy;

            if (sortList != null && sortList.Count > 0)
            {
                if (orderBy != "")
                {
                    orderBy = orderBy + "," + GetSortCondition(sortList);
                }
                else
                {
                    orderBy = GetSortCondition(sortList);
                }
            }

            if (orderBy != "")
            {
                condition.Orders.Append(orderBy);
            }
            condition.DbParamList = list;
            return(condition);
        }
Пример #4
0
        /// <summary>
        /// 删除指定数据
        /// </summary>
        /// <param name="obj">要删除的实体</param>
        /// <param name="scopeList">要删除的条件</param>
        /// <param name="isConcurrency">是否版本并发删除</param>
        /// <returns></returns>
        public int Delete(EntityBase obj, ScopeList scopeList, bool isConcurrency)
        {
            DeleteCondition con = new DeleteCondition(EntityInfo.DBInfo);

            con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName));
            ParamList list = new ParamList();
            Type      type = EntityInfo.EntityType;

            con.Condition.Append("1=1");

            if (obj != null)
            {
                if (scopeList == null)//通过ID删除
                {
                    scopeList = new ScopeList();
                    foreach (EntityPropertyInfo info in EntityInfo.PrimaryProperty)
                    {
                        scopeList.AddEqual(info.PropertyName, info.GetValue(obj));
                    }
                }
            }
            con.Condition.Append(DataAccessCommon.FillCondition(EntityInfo, list, scopeList));


            if (isConcurrency)
            {
                int index = 0;
                foreach (EntityPropertyInfo pInfo in EntityInfo.PropertyInfo)
                {
                    if (pInfo.IsVersion)
                    {
                        FillWhereConcurrency(con.Condition, pInfo, list, pInfo.GetValue(obj), ref index);
                    }
                }
            }
            Dictionary <string, bool> cacheTables = null;

            cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName);

            int ret = -1;

            ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables);

            return(ret);
        }
Пример #5
0
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="obj">修改的对象</param>
        /// <param name="scopeList">条件列表</param>
        /// <param name="setList">Set值列表</param>
        /// <param name="optimisticConcurrency">是否进行并发控制</param>
        /// <returns></returns>
        public int Update(EntityBase obj, ScopeList scopeList, ValueSetList setList, bool optimisticConcurrency)
        {
            StringBuilder sql  = new StringBuilder(500);
            ParamList     list = new ParamList();

            StringBuilder where = new StringBuilder(500);
            //where.Append("1=1");
            Type type = EntityInfo.EntityType;
            List <VersionInfo> lstVersionInfo = null;
            int index = 0;

            KeyWordInfomation keyinfo = BQLValueItem.GetKeyInfo().Clone() as KeyWordInfomation;

            keyinfo.ParamList   = list;
            keyinfo.OutPutModle = false;
            if (obj != null)
            {
                if (!(obj is IEntityProxy))
                {
                    throw new System.InvalidCastException("Update的实体类型必须为代理类,请用CH.Create<T>创建实体或者使用查询出来的实体来更新");
                }
                ///读取属性别名
                foreach (EntityPropertyInfo info in EntityInfo.PropertyInfo)
                {
                    object curValue = info.GetValue(obj);
                    if (!info.ReadOnly)
                    {
                        if (optimisticConcurrency == true && info.IsVersion) //并发控制
                        {
                            object newValue = FillUpdateConcurrency(sql, info, list, curValue, ref index);
                            FillWhereConcurrency(where, info, list, curValue, ref index);
                            if (lstVersionInfo == null)
                            {
                                lstVersionInfo = new List <VersionInfo>();
                            }
                            lstVersionInfo.Add(new VersionInfo(info, curValue, newValue));//添加信息
                        }
                        else
                        {
                            //string paramVal = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatValueName(DataAccessCommon.FormatParam(info.ParamName, index));
                            //string paramKey = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatParamKeyName(DataAccessCommon.FormatParam(info.ParamName, index));
                            if (info.IsNormal)
                            {
                                //if (obj._dicUpdateProperty___ == null || obj._dicUpdateProperty___.Count == 0)
                                //{
                                //    //if (DefaultType.IsDefaultValue(curValue))
                                //    //{
                                //    continue;
                                //    //}
                                //}
                                if (!obj.HasPropertyChange(info.PropertyName))
                                {
                                    continue;
                                }
                                if (setList != null)
                                {
                                    BQLValueItem bvalue = null;
                                    if (setList.TryGetValue(info.PropertyName, out bvalue))
                                    {
                                        continue;
                                    }
                                }
                                sql.Append(",");
                                sql.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName));
                                sql.Append("=");
                                if (curValue != null)
                                {
                                    DBParameter dbPrm = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo);
                                    sql.Append(dbPrm.ValueName);
                                }
                                else
                                {
                                    sql.Append("null");
                                }
                            }
                        }
                    }

                    if (info.IsPrimaryKey && scopeList == null)//当不强制指定条件时候,用主键做更新条件
                    {
                        //if (DefaultType.IsDefaultValue(curValue))
                        //{
                        //    continue;
                        //}
                        //if (obj._dicUpdateProperty___ != null)
                        //{
                        //    if (!obj._dicUpdateProperty___.ContainsKey(info.PropertyName))
                        //    {
                        //        continue;
                        //    }
                        //}

                        DBParameter dbPrm = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo);
                        where.Append(" and ");
                        where.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName));
                        where.Append("=");
                        where.Append(dbPrm.ValueName);
                        //primaryKeyValue=curValue;
                    }
                    index++;
                }
            }
            if (setList != null)
            {
                foreach (KeyValuePair <string, BQLValueItem> kvp in setList)
                {
                    EntityPropertyInfo epinfo = EntityInfo.PropertyInfo[kvp.Key];
                    if (epinfo == null)
                    {
                        throw new Exception("实体:" + EntityInfo.EntityType.FullName + "  找不到属性:" + kvp.Key);
                    }
                    sql.Append(",");
                    sql.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(epinfo.ParamName));
                    sql.Append("=");
                    sql.Append(kvp.Value.DisplayValue(keyinfo));
                }
            }
            where.Append(DataAccessCommon.FillCondition(EntityInfo, list, scopeList));
            if (sql.Length <= 0)
            {
                return(0);
            }
            else
            {
                sql.Remove(0, 1);
            }
            UpdateCondition con = new UpdateCondition(EntityInfo.DBInfo);

            con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName));
            con.UpdateSetValue.Append(sql);
            con.Condition.Append("1=1");

            con.Condition.Append(where);

            int ret = -1;
            Dictionary <string, bool> cacheTables = null;

            cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName);

            ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables);
            if (obj != null && obj._dicUpdateProperty___ != null)
            {
                obj._dicUpdateProperty___.Clear();
            }
            if (lstVersionInfo != null && lstVersionInfo.Count > 0)
            {
                foreach (VersionInfo info in lstVersionInfo)
                {
                    info.Info.SetValue(obj, info.NewValue);
                }
            }

            return(ret);
        }