private string DeleteBuild(bool condition) { DeleteSql.AppendFormat("DELETE FROM {0}", FromSql); if (_where.Length > 0) { DeleteSql.AppendFormat(" WHERE {0}", _where); } else if (condition) { DeleteSql.AppendFormat(" WHERE {0}=@{0}", TypeMapper.GetIdentityFieldName <T>()); } return(DeleteSql.ToString()); }
/// <summary> /// 插入数据,并更新对象的Idenity属性 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int InsertById(T entity) { if (entity == null) { return(0); } var sql = InsertBuild(); Param.AddDynamicParams(entity); var identity = Session.ExecuteScalar <int>(string.Format("{0};SELECT @@IDENTITY;", sql), Param, CommandType.Text); entity.GetType().GetProperty(TypeMapper.GetIdentityFieldName <T>()).SetValue(entity, identity); return(identity); }
/// <summary> /// 异步插入数据,并更新对象的Idenity属性 /// </summary> /// <param name="entity"></param> /// <returns></returns> public Task <int> InsertByIdAsync(T entity) { if (entity == null) { return(Task.Run(() => 0)); } var sql = InsertBuild(); Param.AddDynamicParams(entity); var task = Session.ExecuteScalarAsync <int>(string.Format("{0};SELECT @@IDENTITY;", sql), Param, CommandType.Text); if (task.IsCompleted) { entity.GetType().GetProperty(TypeMapper.GetIdentityFieldName <T>()).SetValue(entity, task.Result); } return(task); }
/// <summary> /// 构建更新语句 /// </summary> /// <returns></returns> public string UpdateBuild(bool condition) { if (_set.Length > 0) { UpdateSql.AppendFormat("UPDATE {0}", FromSql); UpdateSql.AppendFormat(" SET {0}", _set); } else { var colums = TypeMapper.GetColumns <T>(); UpdateSql.AppendFormat("UPDATE {0} SET {1}", FromSql, string.Join(",", colums.Select(s => s.ColumnName + " = @" + s.FieldName))); } if (condition) { UpdateSql.AppendFormat(" WHERE {0} = @{0}", TypeMapper.GetIdentityFieldName <T>()); } else if (_where.Length > 0) { UpdateSql.AppendFormat(" WHERE {0}", _where); } return(UpdateSql.ToString()); }