/// <summary> /// 将制定的数据复制到当前数据访问对象关联的数据库中 /// </summary> /// <param name="log">事务日志实体</param> /// <returns>如果数据已经存在或者复制成功,返回真;如果遇到错误,返回假</returns> public bool DataReplication(MyCommandLogEntity log) { this.ErrorMessage = string.Empty; this.CurrentStatus = ReplicationStatus.UnKnown; if (log.LogFlag > 0) { this.CurrentStatus = ReplicationStatus.Executed; return(true); } var query = this.NewQuery <MyCommandLogEntity>(); if (query.ExistsEntity(log)) { this.CurrentStatus = ReplicationStatus.LogExists; return(true); } else { string errorMessage; MyCommandLogEntity newLog = new MyCommandLogEntity(); newLog.CommandID = log.CommandID; newLog.CommandName = log.CommandName; newLog.CommandText = log.CommandText; newLog.CommandType = log.CommandType; newLog.ExecuteTime = DateTime.Now; //新执行的时间 newLog.LogFlag = 2; //表示已经复制到目标库的状态 newLog.ParameterInfo = log.ParameterInfo; newLog.SQLType = log.SQLType; newLog.LogTopic = log.LogTopic; //log 可能映射了新的表名 newLog.MapNewTableName(log.GetTableName()); bool result = Transaction(ctx => { query.Insert(newLog); //解析得到真正的命令参数信息 var paras = log.ParseParameter(this.CurrentDataBase); int count = this.CurrentDataBase.ExecuteNonQuery(log.CommandText, log.CommandType, paras); //可以在此处考虑引发处理后事件 if (AfterReplications != null) { AfterReplications(this, new ReplicationEventArgs(log, count)); } }, out errorMessage); this.ErrorMessage = errorMessage; this.CurrentStatus = result ? ReplicationStatus.Succeed : ReplicationStatus.Error; return(result); } }
/// <summary> /// 按照顺序读取事务日志并进行处理 /// </summary> /// <param name="pageSize">每次要读取的日志页大小</param> /// <param name="func">处理日志的自定义方法,如果返回假则不再继续读取处理</param> /// <param name="partLogName">分部的日志消息表名字,可以为空</param> /// <returns>返回已经读取的记录数量</returns> public int ReadLog(int pageSize, Func <List <MyCommandLogEntity>, bool> func, string partLogName = null) { int pageNumber = 1; int readCount = 0; int allCount = 0; //先查询出所有记录数和第一页的数据 MyCommandLogEntity log = new MyCommandLogEntity(); if (!string.IsNullOrEmpty(partLogName)) { log.MapNewTableName(log.GetTableName() + "_" + partLogName); } var oql = OQL.From(log) .Select() .OrderBy(o => o.Asc(log.CommandID)) .END .Limit(pageSize, pageNumber); oql.PageWithAllRecordCount = 0; var list = EntityQuery <MyCommandLogEntity> .QueryList(oql, this.CurrentDataBase); allCount = oql.PageWithAllRecordCount; while (list.Count > 0) { readCount += list.Count; ReadLogEventArgs args = new ReadLogEventArgs(allCount, readCount, list); if (OnReadLog != null) { OnReadLog(this, args); } if (!func(list)) { break; } if (list.Count < pageSize) { break; } pageNumber++; /* * //使用GOQL简化查询 * list = OQL.From<MyCommandLogEntity>() * .Select() * .OrderBy((o, p) => o.Asc(p.CommandID)) * .Limit(pageSize, pageNumber,allCount) * .ToList(this.CurrentDataBase); */ //因为日志可能分表,需要修改下面的方式: var oql1 = OQL.From(log) .Select() .OrderBy(o => o.Asc(log.CommandID)) .END .Limit(pageSize, pageNumber); oql1.PageWithAllRecordCount = allCount; list = EntityQuery <MyCommandLogEntity> .QueryList(oql1, this.CurrentDataBase); } return(readCount); }