Exemple #1
0
 public virtual InsertStatement Update(InsertStatement item, TableExpression table, KeyValuePair<ColumnExpression, NLinq.Expressions.Expression>[] original_values, KeyValuePair<ColumnExpression, NLinq.Expressions.Expression>[] values, IDbExpression select)
 {
     if (item.Select != select)
         return new InsertStatement(table, (SelectStatement)select);
     if (original_values != values)
     {
         IDictionary<ColumnExpression, NLinq.Expressions.Expression> dictValues = new Dictionary<ColumnExpression, NLinq.Expressions.Expression>();
         foreach (KeyValuePair<ColumnExpression, NLinq.Expressions.Expression> kvp in values)
             dictValues.Add(kvp);
         return new InsertStatement(table, dictValues);
     }
     return item;
 }
Exemple #2
0
        public InsertStatement Create(Mapping.Entity entity)
        {
            InsertStatement insert = new InsertStatement(new TableSourceExpression(null, entity.Table), new Dictionary<ColumnExpression, NLinq.Expressions.Expression>());

            Create(entity, insert, new List<string>());

            return insert;
        }
Exemple #3
0
        private void Create(Mapping.Entity entity, InsertStatement insert, List<string> columnNamesToInsert)
        {
            foreach (Mapping.Attribute field in entity.Attributes.Values)
            {
                if (field.Generator == SqlMapper.Mapping.Generator.Native)
                {
                    insert.Identity = new ValuedParameter("outIdentity", null, field.DbType, System.Data.ParameterDirection.Output);
                    continue;
                }
                if (columnNamesToInsert.Contains(field.ColumnName))
                    continue;
                columnNamesToInsert.Add(field.ColumnName);
                SqlExpressions.DbParameter param = new SqlExpressions.DbParameter(field.Name, field.DbType, System.Data.ParameterDirection.Input);
                insert.Values.Add(new ColumnExpression(insert.Table.Alias, field.ColumnName), param);
            }

            foreach (Mapping.Reference reference in entity.References.Values)
            {
                if (!reference.IsComposite)
                    continue;

                if (reference.Target.Table == entity.Table)
                    Create(reference.Target, insert, columnNamesToInsert);
            }

            foreach (Field field in entity.NotMappedFields)
            {
                if (columnNamesToInsert.Contains(field.ColumnName.Text))
                    continue;
                insert.Values.Add(new ColumnExpression(insert.Table.Alias, field.ColumnName), new Constant(field.DefaultValue, field.DbType));
            }
        }
Exemple #4
0
        public void Create(Mapping.Entity entity, InsertStatement insert)
        {
            List<string> columnNamesToInsert = new List<string>();

            foreach (ColumnExpression column in insert.Values.Keys)
                columnNamesToInsert.Add(column.ColumnName.Text);

            Create(entity, insert, columnNamesToInsert);
        }
Exemple #5
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();
        }