Пример #1
0
        /// <summary>更新</summary>
        /// <param name="session">实体会话</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public virtual Int32 Update(IEntitySession session, IEntity entity)
        {
            // 没有脏数据,不需要更新
            if (!entity.HasDirty)
            {
                return(0);
            }

            IDataParameter[] dps = null;
            var sql = "";

            // 双锁判断脏数据
            lock (entity)
            {
                if (!entity.HasDirty)
                {
                    return(0);
                }

                sql = SQL(session, entity, DataObjectMethodType.Update, ref dps);
                if (sql.IsNullOrEmpty())
                {
                    return(0);
                }

                //清除脏数据,避免重复提交
                entity.Dirtys.Clear();
            }

            var rs = session.Execute(sql, CommandType.Text, dps);

            //EntityAddition.ClearValues(entity as EntityBase);

            return(rs);
        }
Пример #2
0
        /// <summary>把整个集合从数据库中删除</summary>
        /// <param name="list">实体列表</param>
        /// <param name="useTransition">是否使用事务保护</param>
        /// <param name="session">指定会话,分表分库时必用</param>
        /// <returns></returns>
        public static Int32 Delete <T>(this IEnumerable <T> list, Boolean?useTransition = null, IEntitySession session = null) where T : IEntity
        {
            // 避免列表内实体对象为空
            var entity = list.FirstOrDefault(e => e != null);

            if (entity == null)
            {
                return(0);
            }

            // 单一主键,采用批量操作
            var fact = entity.GetType().AsFactory();
            var pks  = fact.Table.PrimaryKeys;

            if (pks != null && pks.Length == 1)
            {
                session ??= fact.Session;
                var pk    = pks[0];
                var count = 0;
                var rs    = 0;
                var ks    = new List <Object>();
                var sql   = $"Delete From {session.FormatedTableName} Where ";
                foreach (var item in list)
                {
                    ks.Add(item[pk.Name]);
                    count++;

                    // 分批执行
                    if (count >= 1000)
                    {
                        rs += session.Execute(sql + pk.In(ks));

                        ks.Clear();
                        count = 0;
                    }
                }
                if (count > 0)
                {
                    rs += session.Execute(sql + pk.In(ks));
                }

                return(rs);
            }

            return(DoAction(list, useTransition, e => e.Delete(), session));
        }
Пример #3
0
        /// <summary>插入</summary>
        /// <param name="session">实体会话</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public virtual Int32 Insert(IEntitySession session, IEntity entity)
        {
            var factory = Factory;

            // 添加数据前,处理Guid
            SetGuidField(factory.AutoSetGuidField, entity);

            IDataParameter[] dps = null;
            var sql = SQL(session, entity, DataObjectMethodType.Insert, ref dps);

            if (String.IsNullOrEmpty(sql))
            {
                return(0);
            }

            var rs = 0;

            //检查是否有标识列,标识列需要特殊处理
            var field  = factory.Table.Identity;
            var bAllow = factory.AllowInsertIdentity;

            if (field != null && field.IsIdentity && !bAllow && factory.AutoIdentity)
            {
                var id = session.InsertAndGetIdentity(sql, CommandType.Text, dps);
                if (id > 0)
                {
                    entity[field.Name] = id;
                }
                rs = id > 0 ? 1 : 0;
            }
            else
            {
                if (bAllow)
                {
                    var dal = DAL.Create(factory.ConnName);
                    if (dal.DbType == DatabaseType.SqlServer)
                    {
                        // 如果所有字段都不是自增,则取消对自增的处理
                        if (factory.Fields.All(f => !f.IsIdentity))
                        {
                            bAllow = false;
                        }
                        if (bAllow)
                        {
                            sql = $"SET IDENTITY_INSERT {session.FormatedTableName} ON;{sql};SET IDENTITY_INSERT {session.FormatedTableName} OFF";
                        }
                    }
                }
                rs = session.Execute(sql, CommandType.Text, dps);
            }

            // 清除脏数据,避免连续两次调用Save造成重复提交
            entity.Dirtys.Clear();

            return(rs);
        }
Пример #4
0
        /// <summary>从数据库中删除指定条件的实体对象。</summary>
        /// <param name="session">实体会话</param>
        /// <param name="whereClause">限制条件</param>
        /// <returns></returns>
        public virtual Int32 Delete(IEntitySession session, String whereClause)
        {
            var sql = $"Delete From {session.FormatedTableName}";

            if (!whereClause.IsNullOrEmpty())
            {
                sql += " Where " + whereClause;
            }
            return(session.Execute(sql));
        }
Пример #5
0
        /// <summary>把一个实体对象持久化到数据库</summary>
        /// <param name="session">实体会话</param>
        /// <param name="names">更新属性列表</param>
        /// <param name="values">更新值列表</param>
        /// <returns>返回受影响的行数</returns>
        public virtual Int32 Insert(IEntitySession session, String[] names, Object[] values)
        {
            if (names == null)
            {
                throw new ArgumentNullException(nameof(names), "属性列表和值列表不能为空");
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values), "属性列表和值列表不能为空");
            }
            if (names.Length != values.Length)
            {
                throw new ArgumentException("属性列表必须和值列表一一对应");
            }

            var factory = Factory;
            var db      = session.Dal.Db;
            var fs      = new Dictionary <String, FieldItem>(StringComparer.OrdinalIgnoreCase);

            foreach (var fi in factory.Fields)
            {
                fs.Add(fi.Name, fi);
            }
            var sbn = Pool.StringBuilder.Get();
            var sbv = Pool.StringBuilder.Get();

            for (var i = 0; i < names.Length; i++)
            {
                if (!fs.ContainsKey(names[i]))
                {
                    throw new ArgumentException("类[" + factory.EntityType.FullName + "]中不存在[" + names[i] + "]属性");
                }
                // 同时构造SQL语句。names是属性列表,必须转换成对应的字段列表
                if (i > 0)
                {
                    sbn.Append(", ");
                    sbv.Append(", ");
                }

                var column = fs[names[i]].Field;
                sbn.Append(db.FormatName(column));
                //sbv.Append(SqlDataFormat(values[i], fs[names[i]]));
                sbv.Append(db.FormatValue(column, values[i]));
            }
            var sn = sbn.Put(true);
            var sv = sbv.Put(true);

            return(session.Execute($"Insert Into {session.FormatedTableName}({sn}) values({sv})"));
        }
Пример #6
0
        /// <summary>更新一批实体数据</summary>
        /// <param name="session">实体会话</param>
        /// <param name="setClause">要更新的项和数据</param>
        /// <param name="whereClause">指定要更新的实体</param>
        /// <returns></returns>
        public virtual Int32 Update(IEntitySession session, String setClause, String whereClause)
        {
            if (setClause.IsNullOrEmpty() || !setClause.Contains("=") || setClause.ToLower().Contains(" or "))
            {
                throw new ArgumentException("非法参数");
            }

            var sql = $"Update {session.FormatedTableName} Set {setClause.Replace("And", ",")}";

            if (!String.IsNullOrEmpty(whereClause))
            {
                sql += " Where " + whereClause;
            }
            return(session.Execute(sql));
        }
Пример #7
0
        /// <summary>删除</summary>
        /// <param name="session">实体会话</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public virtual Int32 Delete(IEntitySession session, IEntity entity)
        {
            IDataParameter[] dps = null;
            var sql = SQL(session, entity, DataObjectMethodType.Delete, ref dps);

            if (String.IsNullOrEmpty(sql))
            {
                return(0);
            }

            var rs = session.Execute(sql, CommandType.Text, dps);

            // 清除脏数据,避免重复提交保存
            entity.Dirtys.Clear();

            return(rs);
        }