/// <summary> /// /// </summary> /// <param name="entity"></param> /// <param name="output"></param> /// <returns></returns> public virtual string GenerateDelete(IEntityExplain entity, ParameterCollection output) { var conditions = entity.GetFieldValues(entity.GetKeys()); if (conditions == null || conditions.Any() == false) { throw new ArgumentException("该实体对象不包含主键信息,无法执行删除命令。"); } return(_stmBuilder.Delete(entity, conditions, output, SqlOptions.None)); }
/// <summary> /// /// </summary> /// <param name="entity"></param> /// <param name="output"></param> /// <returns></returns> public virtual string GenerateUpdate(IEntityExplain entity, ParameterCollection output) { if (entity.AnySetted() == false) { return(null); } var fields = entity.GetSettedFields(); var conditions = entity.GetFieldValues(entity.GetKeys()); if (conditions == null || conditions.Any() == false) { throw new ArgumentException("实体对象缺少主键值。"); } var values = entity.GetFieldValues(fields.Where(x => entity.IsKey(x) == false)); return(_stmBuilder.Update(entity, values, conditions, output, SqlOptions.None)); }
/// <summary> /// /// </summary> /// <param name="entity"></param> /// <param name="output"></param> /// <param name="includeIncrement"></param> /// <param name="incrementFieldName"></param> /// <returns></returns> public virtual string GenerateInsertOrUpdate(IEntityExplain entity, ParameterCollection output, bool includeIncrement, out bool hasIncrement, string incrementFieldName = null) { if (entity.AnySetted() == false) { hasIncrement = false; return(null); } // 有增量标识的实体对象的特殊处理 if (entity.Increment != null) { // 实体对象如果包含主键信息,则只生成update语句,否则只生成insert语句。 var val = entity[entity.Increment.ColumnName]; if (val != null && val != DBNull.Value && DbValueCast.Cast <int>(val) >= entity.Increment.StartVal) { hasIncrement = false; return(GenerateUpdate(entity, output)); } else { return(GenerateInsert(entity, output, includeIncrement, out hasIncrement, incrementFieldName)); } } var keys = entity.GetKeys(); if (keys == null || keys.Length == 0) { throw new ArgumentException("实体对象缺少主键值。"); } var conditions = entity.GetFieldValues(keys); if (conditions == null || conditions.Any() == false) { throw new ArgumentException("实体对象缺少主键值。"); } var ifConditionSql = _stmBuilder.QueryFormat(null, _stmBuilder.BuildField(keys[0]), _stmBuilder.BuildTableName(entity.TableName), _stmBuilder.Where(conditions, output, SqlOptions.None), null, null, null, SqlOptions.None); var ifTrueSql = GenerateUpdate(entity, output); var ifFalseSql = GenerateInsert(entity, output, includeIncrement, out hasIncrement, incrementFieldName); return(_stmBuilder.IfExistsFormat(ifConditionSql, ifTrueSql, ifFalseSql, SqlOptions.None)); }