public InsertDataResult <T> InsertData <T>(string Schema, string TableName, object DataItem, bool ReturnError = false) { if (DataItem == null) { throw new Exception("It looks like yop forgot set data ('Data' parameter is null)"); } var ret = new InsertDataResult <T>(); var properties = DataItem.GetType().GetProperties().ToList(); var Params = properties.Select(p => new ParamInfo() { Name = p.Name, Value = p.GetValue(DataItem), Index = properties.IndexOf(p), DataType = p.PropertyType }); var tbl = this.GetTableInfo(this.db.GetConnectionString(), Schema, TableName); //Check require fields: var requiredFields = this.GetRequireFields(tbl, Params); var invalidDataType = this.GetInvalidDataTypeFields(tbl, Params); if (requiredFields.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.MissingFields, Fields = requiredFields.Select(p => p.Name).ToArray() }; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Missing fields:" + string.Join(",", err.Fields)) { Detail = err }); } } if (invalidDataType.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.InvalidDataType, Fields = invalidDataType.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Invalid data type at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var exceedFields = this.GetExceedFields(tbl, Params); if (exceedFields.Count > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.ExceedSize, Fields = exceedFields.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Exceed field len at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var sqlGetIdentitySQL = Globals.Compiler.CreateSQLGetIdentity(Schema, TableName, tbl); var sql = "insert into " + Globals.Compiler.GetQName(Schema, TableName) + "(" + string.Join(",", Params.Select(p => Globals.Compiler.GetQName("", p.Name)).ToArray()) + ") " + "values(" + string.Join(",", Params.Select(p => "{" + p.Index + "}").ToArray()) + ")"; sql = sql + ";" + sqlGetIdentitySQL; var retInsert = this.db.ExecCommand(sql, ReturnError, Params.Select(p => p.Value).ToArray()); retInsert.DataItem = DataItem; ret.Data = retInsert.Data; ret.DataItem = retInsert.DataItem; ret.EffectedRowCount = retInsert.EffectedRowCount; ret.Error = retInsert.Error; ret.NewID = retInsert.NewID; return(ret); }
public UpdateDataResult <T> UpdateData <T>(string Schema, string TableName, Expression <Func <T, bool> > Where, object DataItem, bool ReturnError = false) { if (DataItem == null) { throw new Exception("It looks like you forgot set data parameter for update command"); } var ret = new UpdateDataResult <T>(); var properties = DataItem.GetType().GetProperties().ToList(); var Params = properties.Select(p => new ParamInfo() { Name = p.Name, Value = p.GetValue(DataItem), Index = properties.IndexOf(p), DataType = p.PropertyType }).ToList(); var tbl = this.GetTableInfo(this.db.GetConnectionString(), Schema, TableName); //Check require fields: var requiredFields = this.GetRequireFieldsForUpdate(tbl, Params); var invalidDataType = this.GetInvalidDataTypeFields(tbl, Params); if (requiredFields.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.MissingFields, Fields = requiredFields.Select(p => p.Name).ToArray() }; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Missing fields:" + string.Join(",", err.Fields)) { Detail = err }); } } if (invalidDataType.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.InvalidDataType, Fields = invalidDataType.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Invalid data type at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var exceedFields = this.GetExceedFields(tbl, Params); if (exceedFields.Count > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.ExceedSize, Fields = exceedFields.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Exceed field len at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var ex = Where.Body as BinaryExpression; var lstParams = new List <object>(); lstParams.AddRange(Params.Select(p => p.Value)); var strWhere = SelectorCompiler.Gobble <string>(Where.Body, new string[] { Schema }, new string[] { TableName }, Where.Parameters, null, -1, ref lstParams); var Sql = "Update " + Globals.Compiler.GetQName(Schema, TableName) + " set " + String.Join(",", Params.Select(p => Globals.Compiler.GetQName("", p.Name) + "={" + p.Index + "}").ToArray()) + " where " + strWhere; var retUpdate = this.db.UpdateData(Sql, Schema, TableName, tbl, lstParams); ret.Data = retUpdate.Data; ret.DataItem = retUpdate.DataItem; ret.EffectedRowCount = retUpdate.EffectedRowCount; ret.Error = retUpdate.Error; ret.NewID = retUpdate.NewID; return(ret); }