Ejemplo n.º 1
0
 public override CompoundCreateCommand Visit(CompoundCreateCommand c)
 {
     Visit((CreateEntityCommand)c);
     foreach (CreateAttributeCommand cc in c.InnerCommands)
         Visit(cc);
     return c;
 }
Ejemplo n.º 2
0
        public override CompoundCreateCommand Visit(CompoundCreateCommand item)
        {
            Visit((CreateEntityCommand)item);
            foreach (Command command in item.InnerCommands)
                Visit(command);
            //Assign a null value if the parameter was not defined in the inner commands
            foreach (Mapping.Attribute attribute in engine.Provider.Mapping.Entities[item.ParentEntity.Type].Attributes.Values)
                AddParameter(commands[item.ParentEntity], attribute, null);

            return item;
        }
Ejemplo n.º 3
0
		public void Process(CompoundCreateCommand c)
		{
			Process((CreateEntityCommand)c);
			foreach(CreateAttributeCommand cc in c.InnerCommands)
				Process(cc);
		}
Ejemplo n.º 4
0
        public void Process(CompoundCreateCommand c)
        {
            ArrayList queries = new ArrayList();
            CacheEntityEntry entityEntry = _Engine.GetCacheEntityEntry(c.Type);

            if (entityEntry == null)
                throw new SqlMapperException("Cannot find the Entity " + c.Type + " in the mapping file");

            EntityMapping e = (EntityMapping)entityEntry.Mapping;

            //Get the inherited attributes if one table per sub class hierarchy

            Hashtable table = ProcessCreateAttributeCommands(e, c.InnerCommands);

            foreach (CacheQueryEntry entry in entityEntry.InsertCompoundQueryEntries)
            {
                ArrayList genericEntries = new ArrayList();
                if (entry.IsAttributeGenericQuery)
                {
                    AttributeMapping a = e.Attributes["*"];
                    for (int index = 0; index < ((ArrayList)table[a.Name]).Count; index++)
                    {
                        genericEntries.Add(entry.Clone());
                    }
                }

                foreach (Parameter p in entry.Parameters)
                {
                    switch (p.Name)
                    {
                        // Process parameter value to "Entity Id"
                        case "EntityId":
                            {
                                DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                if (!queries.Contains(dEntry))
                                    queries.Add(dEntry);

                                IDbDataParameter param = _Driver.CreateParameter("EntityId", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), c.ParentId);
                                ((IDbCommand)dEntry.Value).Parameters.Add(param);
                            }
                            break;

                        // Process parameter value to "Entity Discriminator"
                        case "EntityDiscriminator":
                            {
                                DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                if (!queries.Contains(dEntry))
                                    queries.Add(dEntry);

                                IDbDataParameter param = _Driver.CreateParameter("EntityDiscriminator", DbType.String, c.Type);
                                ((IDbCommand)dEntry.Value).Parameters.Add(param);
                            }
                            break;

                        // Process parameter value to "FK_Entity"
                        case "FK_Entity":
                            {
                                AttributeMapping a = (AttributeMapping)p.TagMapping;
                                if (!entry.IsAttributeGenericQuery && ((ArrayList)table[a.Name]).Count == 0)
                                {
                                    DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);

                                    IDbDataParameter param = _Driver.CreateParameter("FK_Entity", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), c.ParentId);
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }
                                foreach (CreateAttributeCommand cc in (ArrayList)table[a.Name])
                                {
                                    DictionaryEntry dEntry;
                                    if (entry.IsAttributeGenericQuery)
                                        dEntry = FindDictionaryEntry((CacheQueryEntry)genericEntries[((ArrayList)table[a.Name]).IndexOf(cc)], queries);
                                    else
                                        dEntry = FindDictionaryEntry(entry, queries);


                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);
                                    IDbDataParameter param = _Driver.CreateParameter("FK_Entity", _Dialect.GetDbTypeToPrimaryKey(e.Ids[0].Generator), c.ParentId);
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }
                            }
                            break;

                        // Process parameter value to "AttributeDiscriminator"
                        case "AttributeDiscriminator":
                            {
                                AttributeMapping a = (AttributeMapping)p.TagMapping;
                                if (!entry.IsAttributeGenericQuery && ((ArrayList)table[a.Name]).Count == 0)
                                {
                                    DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);

                                    IDbDataParameter param = _Driver.CreateParameter("AttributeDiscriminator", DbType.String, a.Name);
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }
                                foreach (CreateAttributeCommand cc in (ArrayList)table[a.Name])
                                {
                                    DictionaryEntry dEntry;
                                    if (entry.IsAttributeGenericQuery)
                                        dEntry = FindDictionaryEntry((CacheQueryEntry)genericEntries[((ArrayList)table[a.Name]).IndexOf(cc)], queries);
                                    else
                                        dEntry = FindDictionaryEntry(entry, queries);

                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);

                                    IDbDataParameter param = _Driver.CreateParameter("AttributeDiscriminator", DbType.String, cc.Name);
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }
                            }
                            break;

                        // Process parameter value to "AttributeType"
                        case "AttributeType":
                            {
                                AttributeMapping a = (AttributeMapping)p.TagMapping;
                                foreach (CreateAttributeCommand cc in (ArrayList)table[a.Name])
                                {
                                    DictionaryEntry dEntry;
                                    if (entry.IsAttributeGenericQuery)
                                        dEntry = FindDictionaryEntry((CacheQueryEntry)genericEntries[((ArrayList)table[a.Name]).IndexOf(cc)], queries);
                                    else
                                        dEntry = FindDictionaryEntry(entry, queries);

                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);

                                    IDbDataParameter param = _Driver.CreateParameter("AttributeType", DbType.String, Utils.GetFullName(cc.Type));
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }
                            }
                            break;

                        // Process value to attribute
                        default:
                            {
                                AttributeMapping a = (AttributeMapping)p.TagMapping;



                                if (!entry.IsAttributeGenericQuery && ((ArrayList)table[a.Name]).Count == 0)
                                {
                                    bool isPrimaryKey = false;
                                    foreach (PrimaryKeyMapping pkm in e.Ids)
                                    {
                                        if (pkm.Field == a.Field && pkm.Generator.Name == GeneratorMapping.GeneratorType.business)
                                        {
                                            isPrimaryKey = true;
                                            break;
                                        }
                                    }

                                    if (isPrimaryKey && ((ArrayList)table[a.Name]).Count == 0)
                                    {
                                        DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                        if (!queries.Contains(dEntry))
                                            queries.Add(dEntry);

                                        IDbDataParameter param = _Driver.CreateParameter(p.Name, a.DbType, c.ParentId);
                                        ((IDbCommand)dEntry.Value).Parameters.Add(param);

                                    }
                                    else
                                    {
                                        DictionaryEntry dEntry = FindDictionaryEntry(entry, queries);
                                        if (!queries.Contains(dEntry))
                                            queries.Add(dEntry);

                                        // if attributecommand not found : apply a default value
                                        // initialize value to parameter
                                        SerializableType serializableType = a.GetSerializableType(a.DbType, a.Type);
                                        object value = null;
                                        if (a.DefaultValue != null)
                                        {
                                            switch (serializableType)
                                            {
                                                case SerializableType.BinarySerialization:
                                                    value = Utils.SerializeToArray(a.DefaultValue);
                                                    break;
                                                case SerializableType.StringSerialization:
                                                    value = Utils.SerializeToString(a.DefaultValue);
                                                    break;
                                                case SerializableType.Standard:
                                                    if (a.DefaultValue != String.Empty)
                                                        value = a.DefaultValue;
                                                    break;
                                                case SerializableType.String:
                                                    value = Utils.ConvertToString(a.DefaultValue, a.Type);
                                                    break;
                                                case SerializableType.Int:
                                                    value = Convert.ToInt32(a.DefaultValue);
                                                    break;
                                            }
                                        }

                                        object formattedValue = _Dialect.PreProcessValue(value);

                                        IDbDataParameter param = _Driver.CreateParameter(p.Name, a.DbType, formattedValue);
                                        ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                    }
                                }



                                foreach (CreateAttributeCommand cc in (ArrayList)table[a.Name])
                                {
                                    DictionaryEntry dEntry;
                                    if (entry.IsAttributeGenericQuery)
                                        dEntry = FindDictionaryEntry((CacheQueryEntry)genericEntries[((ArrayList)table[a.Name]).IndexOf(cc)], queries);
                                    else
                                        dEntry = FindDictionaryEntry(entry, queries);


                                    if (!queries.Contains(dEntry))
                                        queries.Add(dEntry);

                                    // if attributecommand not find : apply a default value
                                    object commandValue = cc == null ? a.DefaultValue : cc.Value;
                                    // initialize value to parameter
                                    SerializableType serializableType = a.Type == null ? a.GetSerializableType(a.DbType, cc.Type) : a.GetSerializableType(a.DbType, a.Type);
                                    object value = null;
                                    if (commandValue != null)
                                    {
                                        switch (serializableType)
                                        {
                                            case SerializableType.BinarySerialization:
                                                value = Utils.SerializeToArray(commandValue);
                                                break;
                                            case SerializableType.StringSerialization:
                                                value = Utils.SerializeToString(commandValue);
                                                break;
                                            case SerializableType.Standard:
                                                value = commandValue;
                                                break;
                                            case SerializableType.String:
                                                value = Utils.ConvertToString(commandValue, cc.Type);
                                                break;
                                            case SerializableType.Int:
                                                value = Convert.ToInt32(commandValue);
                                                break;
                                        }
                                    }

                                    object formattedValue = _Dialect.PreProcessValue(value);


                                    IDbDataParameter param = a.CreateDbDataParameter(p.Name, formattedValue);
                                    ((IDbCommand)dEntry.Value).Parameters.Add(param);
                                }

                            }
                            break;

                    }
                }
            }



            foreach (DictionaryEntry dEntry in queries)
            {
                IDbCommand cmd = (IDbCommand)dEntry.Value;
                cmd.Transaction = _Transaction;

                if (cmd.Parameters.Contains("@FK_Entity") && _NewIds.ContainsKey((string)((IDbDataParameter)cmd.Parameters["@FK_Entity"]).Value))
                    ((IDbDataParameter)cmd.Parameters["@FK_Entity"]).Value = ConvertId(e.Ids[0].Generator, (string)((IDbDataParameter)cmd.Parameters["@FK_Entity"]).Value);

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

                cmd.ExecuteNonQuery();

                StringCollection ids = new StringCollection();
                foreach (PrimaryKeyMapping pmk in e.Ids)
                {
                    if (pmk.Generator.Name == GeneratorMapping.GeneratorType.business || pmk.Generator.Name == GeneratorMapping.GeneratorType.assigned)
                    {
                        AttributeMapping am = e.Attributes.FindByField(pmk.Field);

                        if (am == null)
                        {
                            if (e.DiscriminatorField == pmk.Field)
                            {
                                ids.Add(e.DiscriminatorValue);
                                continue;
                            }
                            if (pmk.Generator.Name == GeneratorMapping.GeneratorType.business)
                                throw new MappingNotFoundException(string.Format("The field [{0}] is not declared as an attribute in the entity [{1}] where the Id Generator type({2}) need it.)", pmk.Field, e.Table, pmk.Generator.Name));
                        }

                        // identifier l'attribute mapping de la clé primaire
                        foreach (CreateAttributeCommand cc in c.InnerCommands)
                        {
                            bool attributeCommandFound = false;

                            if (pmk.Generator.Name == GeneratorMapping.GeneratorType.business)
                                attributeCommandFound = am.Name == cc.Name;

                            if (pmk.Generator.Name == GeneratorMapping.GeneratorType.assigned)
                                attributeCommandFound = cc.Name == cc.ParentType + "Id" || cc.Name == "Id";

                            if (attributeCommandFound && !_NewIds.ContainsKey(c.ParentId))
                            {
                                ids.Add(cc.Value.ToString());
                                break;
                            }


                        }

                    }

                    if (!((CacheQueryEntry)dEntry.Key).IsAttributeGenericQuery && pmk.Generator.Name == GeneratorMapping.GeneratorType.native)
                    {
                        IDbCommand command = _Driver.CreateCommand(_Dialect.GetIdentitySelect(e.Table), _Connection, _Transaction);
                        using (IDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ids.Add(Convert.ToString(reader[0]));
                            }
                        }
                    }
                }


                if (ids.Count != 0)
                {
                    string[] idsArray = new string[ids.Count];
                    for (int i = 0; i < ids.Count; i++)
                        idsArray[i] = ids[i];

                    _NewIds.Add(c.ParentId, string.Join(SqlMapperProvider.IDSEP.ToString(), idsArray));
                }
            }
        }
Ejemplo n.º 5
0
 public abstract CompoundCreateCommand Visit(CompoundCreateCommand item);
Ejemplo n.º 6
0
        /// <summary>
        /// Creates Compound commands if necessary
        /// </summary>
        /// <param name="commands"></param>
        private Command[] OptimizeCommands(SyncEngine engine, IList<Entity> commands)
        {
            if (commands == null)
                throw new ArgumentNullException("commands");

            if (commands.Count == 0)
                return new Command[0];

            HashedList<Command> optimizedCommands = new HashedList<Command>();

            System.Collections.Generic.List<CompoundCreateCommand> createdCommands = new System.Collections.Generic.List<CompoundCreateCommand>();

            int j;

            for (int i = 0; i < commands.Count; i++)
            {
                Entity e = commands[i];

                string currentId = e.GetString(SyncUtils.PARENTID);
                int transaction = e.GetInt32(SyncUtils.TRANSACTION);

                switch (e.Type)
                {
                    case SyncUtils.CREATE_ENTITY:

                        string createType = e.GetString(SyncUtils.TYPE);

                        j = i + 1;

                        CompoundCreateCommand ccc = new CompoundCreateCommand(currentId, createType, new List<AttributeCommand>());

                        CompoundCreateCommand actual = createdCommands.Find(delegate(CompoundCreateCommand toFind)
                        {
                            return toFind.ClientId == ccc.ClientId &&
                                toFind.ParentId == ccc.ParentId &&
                                toFind.Type == ccc.Type;
                        });

                        if (actual == null)
                        {
                            createdCommands.Add(ccc);
                            optimizedCommands.Add(ccc);

                            while (j < commands.Count)
                            {

                                string subType = commands[j].GetString(SyncUtils.PARENTTYPE);
                                string subId = commands[j].GetString(SyncUtils.PARENTID);
                                int subTransaction = commands[j].GetInt32(SyncUtils.TRANSACTION);
                                string subCommand = commands[j].Type;

                                if (commands[j].Type == SyncUtils.CREATE_ATTRIBUTE && subId == currentId && subType == createType)
                                {
                                    if (subTransaction != transaction)
                                        break;

                                    ccc.InnerCommands.Add((AttributeCommand)engine.CreateCommand(commands[j]));
                                    commands.RemoveAt(j);
                                }
                                else
                                {
                                    j++;
                                }
                            }
                        }
                        else
                        {
                            optimizedCommands.Remove(actual);
                            optimizedCommands.Add(ccc);

                            createdCommands.Remove(actual);
                            createdCommands.Add(ccc);
                        }

                        break;

                    case SyncUtils.UPDATE_ATTRIBUTE:

                        string updateType = e.GetString(SyncUtils.PARENTTYPE);

                        j = i + 1;

                        CompoundUpdateCommand cuc = new CompoundUpdateCommand(currentId, updateType, new List<AttributeCommand>());
                        cuc.InnerCommands.Add((AttributeCommand)engine.CreateCommand(commands[i]));
                        optimizedCommands.Add(cuc);

                        while (j < commands.Count)
                        {
                            string subType = commands[j].GetString(SyncUtils.PARENTTYPE);
                            string subId = commands[j].GetString(SyncUtils.PARENTID);
                            int subTransaction = commands[j].GetInt32(SyncUtils.TRANSACTION);
                            string subCommand = commands[j].Type;

                            if (commands[j].Type == SyncUtils.UPDATE_ATTRIBUTE && subId == currentId && subType == updateType)
                            {
                                if (subTransaction != transaction)
                                    break;

                                cuc.InnerCommands.Add((AttributeCommand)engine.CreateCommand(commands[j]));
                                commands.RemoveAt(j);
                            }
                            else
                            {
                                j++;
                            }
                        }

                        break;

                    default:
                        optimizedCommands.Add(engine.CreateCommand(e));
                        break;

                }
            }

            Command[] result = new Command[optimizedCommands.Count];
            for (int i = 0; i < result.Length; i++)
                result[i] = (Command)optimizedCommands[i];

            return result;
        }