Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
 public LazyParameter(ValuedParameter innerParameter)
     : base(innerParameter.Name, null)
 {
     this.innerParameter = innerParameter;
 }
Ejemplo n.º 3
0
 public LazyParameter(ValuedParameter idParameter, ParameterDirection parameterDirection)
     : base(idParameter.Name, null, idParameter.Type, parameterDirection)
 {
     this.innerParameter = idParameter;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates the parameter.
 /// </summary>
 /// <remarks>
 /// The parameter will be null if the command already contains a parameter for this field.
 /// </remarks>
 /// <param name="command">The command.</param>
 /// <param name="field">The field.</param>
 /// <returns></returns>
 private ValuedParameter CreateParameter(IDbStatement command, Field field)
 {
     if (command.Parameters.ContainsKey(field.ColumnName.Text))
         return null;
     ValuedParameter param = new ValuedParameter(field.ColumnName.Text, null, field.DbType, ParameterDirection.Input);
     param.Precision = field.Precision;
     param.Scale = field.Scale;
     param.Size = field.Size;
     return param;
 }