public void Process(DeleteReferenceCommand c)
        {
            ReferenceMapping referenceMapping = null;
            if (!string.IsNullOrEmpty(c.ChildType))
                referenceMapping = _Mapping.Entities[c.ParentType, true].References[c.Role, c.ChildType];
            else
                referenceMapping = _Mapping.Entities[c.ParentType, true].References[c.Role];

            if (referenceMapping == null)
            {
                Evaluant.Uss.Models.Entity current = _Engine.Model.GetEntity(c.ParentType);
                while (referenceMapping == null && current != null)
                {
                    Evaluant.Uss.Models.Entity currentChild = _Engine.Model.GetEntity(c.ChildType);
                    while (referenceMapping == null && currentChild != null)
                    {
                        referenceMapping = _Mapping.Entities[current.Type, true].References[c.Role, current.Type, currentChild.Type];
                        currentChild = _Engine.Model.GetParent(currentChild);
                    }

                    if (referenceMapping == null)
                        current = _Engine.Model.GetParent(current);
                }
            }

            if (referenceMapping == null)
                throw new Exception(String.Format("The reference '{0}' of the entity '{1}' is not defined in your mapping file", c.Role, c.ParentType));

            EntityMapping parentMapping = referenceMapping.EntityParent;
            EntityMapping childMapping = _Mapping.Entities[referenceMapping.EntityChild, true];

            RuleMappingCollection rules = referenceMapping.Rules;

            bool isParentRefered = true;

            // Delete all records to index tables
            for (int index = 0; index < rules.Count; index++)
            {
                ISQLExpression query = null;

                //if first rule : ParentField is a foreign key ==> update parent table
                if ((index == 0) && (rules[index].ParentField != parentMapping.IdFields))
                {
                    // UPDATE parenttable SET FK_column = NULL WHERE PK_Column = PK_Value
                    query = new UpdateCommand(rules[index], rules[index].ParentTable);

                    for (int i = 0; i < rules[index].ParentFields.Length; i++)
                    {
                        if (parentMapping.Ids[rules[index].ParentFields[i]] == null)
                        {
                            Constant constant = new Constant(DBNull.Value, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator));
                            if (rules[index].ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rules[index].ParentDefaultValues[i]))
                                constant = new Constant(rules[index].ParentDefaultValues[i], _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator));

                            ((UpdateCommand)query).ColumnValueCollection.Add(rules[index].ParentFields[i], constant);
                        }
                    }

                    for (int i = 0; i < parentMapping.Ids.Count; i++)
                    {
                        string PK_Column = parentMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i];
                        string PK_Value = ConvertId(parentMapping, PK_Column, c.ParentId);
                        ((UpdateCommand)query).WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                             new Column(parentMapping.Ids[i], PK_Column),
                             BinaryLogicOperator.Equals,
                             new Constant(PK_Value, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator))));
                    }



                    ExecuteCommand(query);
                }

                //if last rule : ChildField is a foreign key ==> update child table
                if ((index == rules.Count - 1) && (rules[index].ChildField != childMapping.IdFields))
                {
                    // UPDATE childtable SET FK_column = NULL WHERE PK_Column = PK_Value
                    query = new UpdateCommand(rules[index], rules[index].ChildTable);


                    for (int i = 0; i < rules[index].ChildFields.Length; i++)
                    {
                        if (childMapping.Ids[rules[index].ChildFields[i]] == null)
                        {
                            Constant constant = new Constant(DBNull.Value, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator));
                            if (rules[index].ChildDefaultValues.Length > i && !string.IsNullOrEmpty(rules[index].ChildDefaultValues[i]))
                                constant = new Constant(rules[index].ChildDefaultValues[i], _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator));

                            ((UpdateCommand)query).ColumnValueCollection.Add(rules[index].ChildFields[i], constant);
                        }
                    }

                    for (int i = 0; i < childMapping.Ids.Count; i++)
                        ((UpdateCommand)query).WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                           new Column(childMapping.Ids[i], childMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i]),
                           BinaryLogicOperator.Equals,
                           new Constant(ConvertId(childMapping, childMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i], c.ChildId), _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator))));

                    //PrimaryKeyMapping PK_mapping = index == 0 ? parentMapping.Ids[0] : childMapping.Ids[0];
                    //string PK_column = childMapping.IdFields;
                    //string PK_value = c.ChildId;



                    ExecuteCommand(query);
                }

                // Execute a delete command to index table
                if (index != rules.Count - 1)
                {
                    // Delete records to index table with one foreign key : rule(n).ChildField == rule(n+1).ParentField
                    if (rules[index].ChildField == rules[index + 1].ParentField)
                    {
                        // the refered table is parent table ==> WHERE rule(n).ChildField == ParentId
                        if (isParentRefered)
                        {
                            query = new DeleteCommand(rules[index], rules[index].ChildTable);
                            ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(rules[index], rules[index].ChildField),
                                BinaryLogicOperator.Equals,
                                new Constant(c.ParentId, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[0].Generator))));

                        }

                        // the refered table is child table ==>  WHERE rule(n).ChildField == ChildId
                        else
                        {
                            query = new DeleteCommand(rules[index], rules[index].ChildTable);
                            ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(rules[index], rules[index].ChildField),
                                BinaryLogicOperator.Equals,
                                new Constant(c.ChildId, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[0].Generator))));
                        }

                    }

                    // Delete records to index table with two foreign key : rule(n).ChildField != rule(n+1).ParentField
                    else
                    {
                        query = new DeleteCommand(rules[index], rules[index].ChildTable);
                        ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                            new Column(rules[index], rules[index].ChildField),
                            BinaryLogicOperator.Equals,
                            new Constant(c.ParentId, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[0].Generator))));
                        ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                            new Column(rules[index + 1], rules[index + 1].ParentField),
                            BinaryLogicOperator.Equals,
                            new Constant(c.ChildId, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[0].Generator))));
                        isParentRefered = false;
                    }

                    ExecuteCommand(query);
                }
            }
        }
        public void Process(DeleteEntityCommand c)
        {
            EntityMapping deleteEntityMapping = _Mapping.Entities[c.Type, true];
            Hashtable cmds = new Hashtable();

            foreach (ReferenceMapping refm in deleteEntityMapping.References)
            {
                Evaluant.Uss.Models.Reference reference = _Engine.Model.GetReference(c.Type, refm.Name);
                EntityMapping entityChildMapping = _Mapping.Entities[refm.EntityChild];

                if (!reference.ToMany)
                    continue;
                foreach (RuleMapping rulem in refm.Rules)
                {
                    if (rulem.ParentTable == deleteEntityMapping.Table)
                    {
                        // update child table
                        SQLCommand cmd = (SQLCommand)cmds[rulem.ChildTable];
                        EntityMapping childEntity = _Mapping.Entities[refm.EntityChild];
                        if (!reference.IsComposition && rulem.ChildTable == childEntity.Table)
                        {
                            if (cmd == null)
                            {
                                cmd = new UpdateCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);
                            }
                        }
                        else
                        {
                            if (cmd == null)
                            {
                                cmd = new DeleteCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);
                            }
                        }

                        //if (cmd is DeleteCommand)
                        //    continue;

                        for (int i = 0; i < rulem.ParentFields.Length; i++)
                        {
                            DbType dbType;
                            if (deleteEntityMapping.IdFields.Contains(refm.Rules[0].ParentFields[i]))
                            {
                                dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[refm.Rules[0].ParentFields[i]].Generator);
                            }
                            else
                            {
                                dbType = _Dialect.GetDbTypeToPrimaryKey(childEntity.Ids[refm.Rules[0].ChildFields[i]].Generator);
                            }


                            Constant newValue;
                            if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i]))
                                newValue = new Constant(rulem.ParentDefaultValues[i], dbType);
                            else
                                newValue = new Constant(DBNull.Value, dbType);

                            // SSX 28/01/08: add contains test
                            if (entityChildMapping.Ids[rulem.ChildFields[i]] == null && cmd is UpdateCommand
                                && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ChildFields[i]))
                                ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ChildFields[i], newValue);

                            Column childField = new Column(rulem, rulem.ChildFields[i]);
                            Constant childParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType);
                            BinaryLogicExpression parentClause = new BinaryLogicExpression(childField, BinaryLogicOperator.Equals, childParameter);
                            if (cmd is UpdateCommand)
                                ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause);
                            else
                                ((DeleteCommand)cmd).Condition.SearchCondition.Add(parentClause);
                        }

                        if (!string.IsNullOrEmpty(rulem.Constraint))
                            DisableForeignKeys(rulem.ChildTable);
                    }
                }
            }

            // Deletes all references to and from it first
            foreach (EntityMapping em in _Mapping.Entities)
            {
                foreach (ReferenceMapping refm in em.References)
                {
                    //Evaluant.Uss.Models.Model childModel = _Engine.Model.GetEntity(

                    if (refm.EntityChild == deleteEntityMapping.Type) // To
                    {
                        for (int indexRule = 0; indexRule < refm.Rules.Count; indexRule++)
                        {
                            RuleMapping rulem = refm.Rules[indexRule];
                            if ((rulem.ChildField == deleteEntityMapping.IdFields &&
                                deleteEntityMapping.DiscriminatorField == null) ||
                                (rulem.ChildField + ";" + deleteEntityMapping.DiscriminatorField == deleteEntityMapping.IdFields &&
                                deleteEntityMapping.DiscriminatorField != null))
                            {
                                // delete childtable

                                if (cmds.ContainsKey(rulem.ChildTable))
                                    continue;

                                DeleteCommand cmd = new DeleteCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);

                                for (int index = 0; index < rulem.ChildFields.Length; index++)
                                {
                                    DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[index].Generator);

                                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                        new Column(rulem, rulem.ChildFields[index]),
                                        BinaryLogicOperator.Equals,
                                        new Constant(ConvertId(deleteEntityMapping, rulem.ChildFields[index], c.ParentId), dbType)
                                    ));
                                }


                                if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1)
                                {
                                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                        new Column(rulem, refm.DiscriminatorField),
                                        BinaryLogicOperator.Equals,
                                        new Parameter(rulem, refm.DiscriminatorValue)
                                        ));
                                }

                                if (!string.IsNullOrEmpty(rulem.Constraint))
                                    DisableForeignKeys(rulem.ChildTable);
                            }

                            if (rulem.ParentField != em.IdFields && em.DiscriminatorField != null
                                || rulem.ParentTable != em.Table)
                            {
                                // update parent table
                                SQLCommand cmd = (SQLCommand)cmds[rulem.ParentTable];
                                if (cmd == null)
                                {
                                    cmd = new UpdateCommand(rulem, rulem.ParentTable);
                                    cmds.Add(rulem.ParentTable, cmd);
                                }

                                if (cmd is DeleteCommand)
                                    continue;

                                for (int i = 0; i < rulem.ParentFields.Length; i++)
                                {
                                    DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[i].Generator);

                                    Constant newValue = new Constant(DBNull.Value, dbType);
                                    if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i]))
                                        newValue = new Constant(rulem.ParentDefaultValues[i], dbType);

                                    // SSX 28/01/08: add contains test
                                    if (em.Ids[rulem.ParentFields[i]] == null
                                        && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ParentFields[i]))
                                        ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ParentFields[i], newValue);

                                    Column parentField = new Column(rulem, rulem.ParentFields[i]);
                                    Constant parentParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType);
                                    BinaryLogicExpression parentClause = new BinaryLogicExpression(parentField, BinaryLogicOperator.Equals, parentParameter);
                                    ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause);
                                }

                                // UPDATE parentTable SET parentField = NULL WHERE (parentField = @parentValue AND discriminator = 'name's relation')
                                if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1)
                                {
                                    Column discriminatorField = new Column(rulem, rulem.ParentField);
                                    Constant discriminatorValue = new Constant(refm.DiscriminatorValue, DbType.AnsiString);
                                    BinaryLogicExpression discriminatorClause = new BinaryLogicExpression(discriminatorField, BinaryLogicOperator.Equals, discriminatorValue);
                                    ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(discriminatorClause);
                                }

                                if (!string.IsNullOrEmpty(rulem.Constraint))
                                    DisableForeignKeys(rulem.ParentTable);

                            }
                        }

                    }

                }
            }


            // Deletes all attributes if they are in a separate table
            if (deleteEntityMapping.Attributes != null)
            {
                foreach (AttributeMapping am in deleteEntityMapping.Attributes)
                {
                    if (am.Table != deleteEntityMapping.Table)
                    {
                        DeleteCommand cmd = new DeleteCommand(am, am.Table);
                        for (int index = 0; index < deleteEntityMapping.Ids.Count; index++)
                        {
                            PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index];
                            cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(am, am.ParentFields[index]),
                                BinaryLogicOperator.Equals,
                                new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator))));
                        }
                        cmds.Add(am.Table, cmd);
                    }
                }
            }

            // SSX 28/01/08: remove table if already existing
            if (cmds.Contains(deleteEntityMapping.Table))
            {
                cmds.Remove(deleteEntityMapping.Table);
            }

            if (!cmds.ContainsKey(deleteEntityMapping.Table))
            {
                DeleteCommand cmd = new DeleteCommand(deleteEntityMapping, deleteEntityMapping.Table);
                for (int index = 0; index < deleteEntityMapping.Ids.Count; index++)
                {
                    PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index];
                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                        new Column(deleteEntityMapping, pkm.Field),
                        BinaryLogicOperator.Equals,
                        new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator))));
                }
                cmds.Add(deleteEntityMapping.Table, cmd);
            }

            foreach (string tableName in cmds.Keys)
            {

                foreach (string query in _Dialect.RenderQueries((ISQLExpression)cmds[tableName]))
                {
                    if (!string.IsNullOrEmpty(query))
                    {
                        IDbCommand cmd = _Driver.CreateCommand(query, _Connection, _Transaction);
                        if (_Engine.TraceSqlSwitch.Enabled)
                            TraceHelpler.Trace(cmd, _Dialect);

                        cmd.ExecuteNonQuery();
                    }
                }


            }


        }
        public void Process(DeleteAttributeCommand c)
        {
            EntityMapping e = _Mapping.Entities[c.ParentType, true];
            AttributeMapping a = e.Attributes[c.Name, true];

            IDbCommand command;

            // Specific case
            if (a.Table == null | e.Table == a.Table)
            {
                UpdateCommand upd = new UpdateCommand(a, e.Table);
                upd.ColumnValueCollection.Add(a.Field, new Constant(DBNull.Value, a.DbType));
                upd.WhereClause.SearchCondition.Add(new BinaryLogicExpression(new Column(a, e.IdFields), BinaryLogicOperator.Equals, new Parameter(null, "Id")));
                upd.Accept(_Dialect);

                command = _Driver.CreateCommand(_Dialect.RenderQueries(upd)[0], _Connection, _Transaction);

                /*command = _Driver.CreateCommand(String.Format(@"UPDATE {0} SET {1} = NULL  WHERE ({2} = {3})",
                                                               _Dialect.FormatAttribute(e.Table),
                                                               _Dialect.FormatAttribute(a.Field),
                                                               _Dialect.FormatAttribute(e.IdFields),
                                                               _Driver.FormatParameter("Id")), _Connection, _Transaction);*/

                string parentid = ConvertId(e.Ids[0].Generator, c.ParentId).ToString();
                command.Parameters.Add(_Driver.CreateParameter("Id", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), parentid));
            }
            // Generic case
            else
            {
                if (a.Discriminator != null)
                {
                    command = _Driver.CreateCommand(String.Format(@"DELETE FROM {0} WHERE ({1} = {3} and {2} = {4})",
                                                                  _Dialect.FormatAttribute(a.Table),
                                                                  _Dialect.FormatAttribute(a.Discriminator),
                                                                  _Dialect.FormatAttribute(a.ParentField),
                                                                  _Driver.FormatParameter("Name"),
                                                                  _Driver.FormatParameter("FK_Entity")), _Connection, _Transaction);

                    command.Parameters.Add(_Driver.CreateParameter("Name", DbType.AnsiString, c.Name));
                    string parentid = ConvertId(e.Ids[0].Generator, c.ParentId).ToString();
                    command.Parameters.Add(_Driver.CreateParameter("FK_Entity", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), parentid));
                }
                else
                {
                    command = _Driver.CreateCommand(String.Format(@"DELETE FROM {0} WHERE ({1} = {2})",
                                                                   _Dialect.FormatAttribute(a.Table),
                                                                   _Dialect.FormatAttribute(a.ParentField),
                                                                   _Driver.FormatParameter("FK_Entity")), _Connection, _Transaction);

                    string parentid = ConvertId(e.Ids[0].Generator, c.ParentId).ToString();
                    command.Parameters.Add(_Driver.CreateParameter("FK_Entity", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), parentid));
                }
            }

            command.Transaction = _Transaction;

            if (_Engine.TraceSqlSwitch.Enabled)
            {
                TraceHelpler.Trace(command, _Dialect);
            }

            command.ExecuteNonQuery();

        }
Exemple #4
0
		public virtual void Visit(UpdateCommand updateCommand)
		{
            if (updateCommand.ColumnValueCollection.Count != 0)
            {

                _Query.Append(UPDATE).Append(FormatAttribute(updateCommand.TableName)).Append(SPACE).Append(SET);
                SortedList coll = updateCommand.ColumnValueCollection;
                foreach (string key in coll.Keys)
                {
                    _Query.Append(FormatAttribute(key)).Append(" = ");
                    ((ISQLExpression)coll[key]).Accept(this);
                    if (coll.Keys.Count - 1 != coll.IndexOfKey(key))
                        _Query.Append(COMMA);
                }
                updateCommand.WhereClause.Accept(this);
            }
		}