public AddOperation ( |
||
operation | ||
리턴 | void |
private void ApplyUpdatesSoFarAsNewCommand( IList <BaseCommand> results, IList <UpdateOperation> updates, ref string updateTableName, ref object updatePk) { if (updates.Count == 0) { return; } var command = new UpdateCommand(); updates.ForEach(update => command.AddOperation(update)); results.Add(command); updates.Clear(); updateTableName = null; updatePk = null; }
private void AppendPropertyToInsertStatement( Script script, StringBuilder colBuff, StringBuilder valBuff, PropertyMetadata property, ref int index, BaseInsertDeleteOperation operation, ArrayList values, MethodInfo getter, UpdateCommand updateCommand) { object columnValueForUpdate = null; if (property.HasAttribute <ForeignKeyReferenceAttribute>() && null != operation.OwnerMetadata && _dtoMetadataCache.GetValidatedMetadataFor( property.GetAttribute <ForeignKeyReferenceAttribute>().ReferencedDto).TableName == operation.OwnerMetadata.TableName) { columnValueForUpdate = new Func <object>(() => operation.OwnerPrimaryKeyAsObject); values.Add(columnValueForUpdate); } else if (property.HasAttribute <ManyToOneAttribute>() && property.GetAttribute <ManyToOneAttribute>().ForeignKeyTargetColumnName != null) { var propValue = property.GetValue(operation.Value); var propTypeMetadata = _dtoMetadataCache.GetValidatedMetadataFor(property.Prop.PropertyType); if (null != propValue && null != propTypeMetadata) { var targetName = property.GetAttribute <ManyToOneAttribute>().ForeignKeyTargetColumnName; var fkTargetProperty = propTypeMetadata[targetName]; if (fkTargetProperty == null) { throw new ArgumentException(string.Format( "Cannot INSERT foreign key value for non existent target column '{0}'" + " specified from column '{1}'.", targetName, property.ColumnName)); } columnValueForUpdate = new Func <object>(() => fkTargetProperty.GetValue(propValue)); values.Add(columnValueForUpdate); } else { columnValueForUpdate = new Func <object>(() => null); values.Add(columnValueForUpdate); } } else if (property.HasAttribute <ManyToOneAttribute>() || property.HasAttribute <OneToOneAttribute>()) { if (property.HasAttribute <OneToOneAttribute>() && !property.HasAttribute <ForeignKeyReferenceAttribute>()) { // One to one relationship where child table references parent rather than the other way around. // This will be saved along with the child object. return; } object propValue = property.GetValue(operation.Value); DtoMetadata propMetadata = _dtoMetadataCache.GetValidatedMetadataFor(property.Prop.PropertyType); columnValueForUpdate = new Func <object>( () => propValue == null || propMetadata == null ? null : propMetadata.GetPrimaryKeyValueAsObject(propValue)); values.Add(columnValueForUpdate); } else { columnValueForUpdate = getter.Invoke(operation.Value, new object[0]); values.Add(columnValueForUpdate); } if (columnValueForUpdate is Func <object> ) { script.WireUpActions.Add(() => { object newPropertyValue = ((Func <object>)columnValueForUpdate)(); if (CanAssignToProperty(newPropertyValue, property)) { property.Prop.SetValue(operation.Value, newPropertyValue); } }); } updateCommand.AddOperation(new UpdateOperation() { ColumnPropertyMetadata = property, TableName = operation.ValueMetadata.TableName, Value = columnValueForUpdate, ValueMetadata = property.IsString || property.IsNumericType || property.IsEnum || !property.IsReferenceType ? null : _dtoMetadataCache.GetMetadataFor(property.Prop.PropertyType), Owner = operation.Value, OwnerMetadata = operation.ValueMetadata, OwnerPrimaryKeyColumn = operation.ValueMetadata.PrimaryKey.ColumnName, OwnerPropertyMetadata = property }); if (colBuff.Length > 0) { colBuff.Append(@", "); valBuff.Append(@", "); } colBuff.Append("[" + property.ColumnName + "]"); valBuff.Append("{"); valBuff.Append(index); valBuff.Append("}"); ++index; }
private void ApplyUpdatesSoFarAsNewCommand( IList<BaseCommand> results, IList<UpdateOperation> updates, ref string updateTableName, ref object updatePk) { if (updates.Count == 0) { return; } var command = new UpdateCommand(); updates.ForEach(update => command.AddOperation(update)); results.Add(command); updates.Clear(); updateTableName = null; updatePk = null; }
private void AppendPropertyToInsertStatement( Script script, StringBuilder colBuff, StringBuilder valBuff, PropertyMetadata property, ref int index, BaseInsertDeleteOperation operation, ArrayList values, MethodInfo getter, UpdateCommand updateCommand) { object columnValueForUpdate = null; if (property.HasAttribute<ForeignKeyReferenceAttribute>() && null != operation.OwnerMetadata && _dtoMetadataCache.GetValidatedMetadataFor( property.GetAttribute<ForeignKeyReferenceAttribute>().ReferencedDto).TableName == operation.OwnerMetadata.TableName) { columnValueForUpdate = new Func<object>(() => operation.OwnerPrimaryKeyAsObject); values.Add(columnValueForUpdate); } else if (property.HasAttribute<ManyToOneAttribute>() && property.GetAttribute<ManyToOneAttribute>().ForeignKeyTargetColumnName != null) { var propValue = property.GetValue(operation.Value); var propTypeMetadata = _dtoMetadataCache.GetValidatedMetadataFor(property.Prop.PropertyType); if (null != propValue && null != propTypeMetadata) { var targetName = property.GetAttribute<ManyToOneAttribute>().ForeignKeyTargetColumnName; var fkTargetProperty = propTypeMetadata[targetName]; if (fkTargetProperty == null) { throw new ArgumentException(string.Format( "Cannot INSERT foreign key value for non existent target column '{0}'" + " specified from column '{1}'.", targetName, property.ColumnName)); } columnValueForUpdate = new Func<object>(() => fkTargetProperty.GetValue(propValue)); values.Add(columnValueForUpdate); } else { columnValueForUpdate = new Func<object>(() => null); values.Add(columnValueForUpdate); } } else if (property.HasAttribute<ManyToOneAttribute>() || property.HasAttribute<OneToOneAttribute>()) { if (property.HasAttribute<OneToOneAttribute>() && !property.HasAttribute<ForeignKeyReferenceAttribute>()) { // One to one relationship where child table references parent rather than the other way around. // This will be saved along with the child object. return; } object propValue = property.GetValue(operation.Value); DtoMetadata propMetadata = _dtoMetadataCache.GetValidatedMetadataFor(property.Prop.PropertyType); columnValueForUpdate = new Func<object>( () => propValue == null || propMetadata == null ? null : propMetadata.GetPrimaryKeyValueAsObject(propValue)); values.Add(columnValueForUpdate); } else { columnValueForUpdate = getter.Invoke(operation.Value, new object[0]); values.Add(columnValueForUpdate); } if (columnValueForUpdate is Func<object>) { script.WireUpActions.Add(() => { object newPropertyValue = ((Func<object>) columnValueForUpdate)(); if (CanAssignToProperty(newPropertyValue, property)) { property.Prop.SetValue(operation.Value, newPropertyValue); } }); } updateCommand.AddOperation(new UpdateOperation() { ColumnPropertyMetadata = property, TableName = operation.ValueMetadata.TableName, Value = columnValueForUpdate, ValueMetadata = property.IsString || property.IsNumericType || property.IsEnum || ! property.IsReferenceType ? null : _dtoMetadataCache.GetMetadataFor(property.Prop.PropertyType), Owner = operation.Value, OwnerMetadata = operation.ValueMetadata, OwnerPrimaryKeyColumn = operation.ValueMetadata.PrimaryKey.ColumnName, OwnerPropertyMetadata = property }); if (colBuff.Length > 0) { colBuff.Append(@", "); valBuff.Append(@", "); } colBuff.Append("[" + property.ColumnName + "]"); valBuff.Append("{"); valBuff.Append(index); valBuff.Append("}"); ++index; }