public UpdateStatement Update(Evaluant.Uss.SqlMapper.Mapping.Entity entity) { UpdateStatement update = new UpdateStatement() { From = new SqlExpressions.FromClause(new TableSourceExpression(null, entity.Table)) }; foreach (var item in entity.Ids.Values) { var idConstraint = new BinaryExpression(BinaryExpressionType.Equal, new ColumnExpression(null, item.ColumnName), new DbParameter(item.Name, item.DbType, System.Data.ParameterDirection.Input)); if (update.Where == null) update.Where = new WhereClause(idConstraint); else update.Where.Expression = new BinaryExpression(BinaryExpressionType.And, update.Where.Expression, idConstraint); } return update; }
public override IDbStatement Visit(UpdateStatement item) { writer.Write("UPDATE "); Visit(item.From[0]); writer.Write(" SET "); bool isFirst = true; foreach (var setValue in item.Set) { if (isFirst) isFirst = false; else writer.Write(", "); Visit(setValue); } Visit(item.Where); return item; }
public IDbStatement Create(Mapping.Reference reference, ValuedParameter idParameter) { //Case of single rule and FK is in the child table if (reference.Model.Cardinality.IsOne && reference.Model.Cardinality is Cardinality.ToMany) { Mapping.Rule rule = reference.Rules[0]; TableAlias alias = null; UpdateStatement update = new UpdateStatement() { From = new Evaluant.Uss.SqlExpressions.FromClause(new TableSourceExpression(alias, rule.ChildTable)) }; foreach (Field field in rule.ChildFields) update.Set.Add(new ColumnExpression(alias, field.ColumnName), new Evaluant.Uss.SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); BinaryExpression constraint = null; foreach (Mapping.Attribute attribute in reference.Target.Ids.Values) { Field field = attribute.Field; BinaryExpression fieldConstraint = new BinaryExpression(BinaryExpressionType.Equal, new ColumnExpression(alias, field.ColumnName), new SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); if (constraint == null) constraint = fieldConstraint; else constraint = new BinaryExpression(BinaryExpressionType.And, constraint, fieldConstraint); } update.Where = new WhereClause(constraint); return update; } //Case of single rule and FK is in the parent table if (reference.Model.Cardinality.IsMany && reference.Model.Cardinality is Cardinality.ToOne) { Mapping.Rule rule = reference.Rules[0]; TableAlias alias = null; UpdateStatement update = new UpdateStatement() { From = new Evaluant.Uss.SqlExpressions.FromClause(new TableSourceExpression(alias, rule.ParentTable)) }; foreach (Field field in rule.ParentFields) update.Set.Add(new ColumnExpression(alias, field.ColumnName), new Evaluant.Uss.SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); BinaryExpression constraint = null; foreach (Mapping.Attribute attribute in reference.Parent.Ids.Values) { Field field = attribute.Field; BinaryExpression fieldConstraint = new BinaryExpression(BinaryExpressionType.Equal, new ColumnExpression(alias, field.ColumnName), new SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); if (constraint == null) constraint = fieldConstraint; else constraint = new BinaryExpression(BinaryExpressionType.And, constraint, fieldConstraint); } update.Where = new WhereClause(constraint); return update; } //Case of 2 rules with an index table if (reference.Model.Cardinality.IsMany && reference.Model.Cardinality is Cardinality.ToMany) { TableAlias alias = null; Table indexTable = reference.Rules[0].ChildTable; Dictionary<ColumnExpression, Expression> values = new Dictionary<ColumnExpression, Expression>(); InsertStatement insert = new InsertStatement(new TableSourceExpression(alias, indexTable), values); foreach (Mapping.Rule rule in reference.Rules) { if (rule.ParentTable == indexTable) { foreach (Field field in rule.ParentFields) values.Add(new ColumnExpression(alias, field.ColumnName), new SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); } if (rule.ChildTable == indexTable) { foreach (Field field in rule.ChildFields) { if (field.IsIdentity) values.Add(new ColumnExpression(alias, field.ColumnName), new LazyParameter(field.ColumnName.Text, idParameter, System.Data.ParameterDirection.Input)); else values.Add(new ColumnExpression(alias, field.ColumnName), new SqlExpressions.DbParameter(field.ColumnName.Text, field.DbType, System.Data.ParameterDirection.Input)); } } } return insert; } throw new NotImplementedException(); }