public ExcuteResult Delete(string id) { var result = new ExcuteResult(Operate.Delete, _connString, _table.Name); var old = Find(id); if (old.HasData) { var paramDictionary = ReflectionUtility.GetObjDic(((string)old.Value).ToObject()); result.RollBackScript = ToFinalSql(_deleteRollBackSql, paramDictionary); if (Excute(_deleteSql, id) > 0) { result.AddMessage("删除成功(id=" + id + ")"); result.Successful = true; } else { result.AddMessage("删除失败(id=" + id + ")"); result.RollBackScript = ""; //删除失败时不需要回滚 result.Successful = false; } } else {//无旧记录 result.AddMessage("删除成功(未找到id=" + id + "的旧记录)"); result.Successful = true; } return(result); }
public ExcuteResult Update(Dictionary <string, object> paramDictionary) { var result = new ExcuteResult(Operate.Update, _connString, _table.Name); if (!paramDictionary.ContainsKey(_table.PrimaryKey)) { throw new Exception("传入参数中没有找到" + _table.Name + "的主键值(" + _table.PrimaryKey + ")"); } var old = Find(paramDictionary[_table.PrimaryKey] + ""); if (old.HasData) { result.RollBackScript = ToFinalSql(_updateRollBackSql, paramDictionary); var ex = Excute(_updateSql, paramDictionary); if (!ex.HasValue()) { result.AddMessage("更新成功"); result.Successful = true; } else { result.AddMessage("更新失败=>" + ex); result.RollBackScript = "";//更新失败不需要回滚 result.Successful = false; } } else {//无旧记录-执行全新添加 var temp = Add(paramDictionary); if (temp.Successful) { result.AddMessage("更新成功(全新添加)"); result.Successful = true; result.RollBackScript = temp.RollBackScript; } else { result.AddMessage("更新失败(全新添加)"); result.AddMessages(temp.Messages); //此种情况无需回滚-更新时不存在旧记录且尝试添加旧记录失败 result.RollBackScript = "";//清空回滚脚本 result.Successful = false; } } return(result); }
public ExcuteResult Add(Dictionary <string, object> paramDictionary) {//失败时返回"" var result = new ExcuteResult(Operate.Add, _connString, _table.Name); if (!paramDictionary.ContainsKey(_table.PrimaryKey)) { throw new Exception("传入参数中没有找到" + _table.Name + "的主键值(" + _table.PrimaryKey + ")"); } var primaryKeyValue = paramDictionary[_table.PrimaryKey] + ""; //容错处理,自动赋值为id paramDictionary[_table.PrimaryKey] = primaryKeyValue = primaryKeyValue.HasValue() ? primaryKeyValue : paramDictionary["_id"] + ""; var old = Find(primaryKeyValue); if (old.HasData) {//存在旧记录 result.AddMessage("添加失败(已存在ID=" + primaryKeyValue + "的记录)"); result.Successful = false; } else { result.RollBackScript = ToFinalSql(_insertRollBackSql, primaryKeyValue); var ex = Excute(_insertSql, paramDictionary); if (!ex.HasValue()) { result.AddMessage("添加成功"); result.Successful = true; result.Value = paramDictionary[_table.PrimaryKey]; } else { result.AddMessage("添加失败=>" + ex); result.RollBackScript = "";//添加失败不需要回滚 result.Successful = false; } } return(result); }