Exemple #1
0
		public virtual CacheQueryEntry Clone()
		{
			CacheQueryEntry queryEntry = new CacheQueryEntry(_Query);
			foreach(Parameter cloneParam in _Parameters)
			{
				queryEntry.Parameters.Add(cloneParam.Clone());
			}
		
			return queryEntry;
		}
        public Hashtable InitializeCompoundUpdateCommand(EntityMapping entity, ArrayList updateCommands)
        {
            Hashtable updateCompoundQueryEntries = new Hashtable();

            if (entity.Attributes != null)
            {
                // Loops through attributes so as to create as many SQL Update Command as Tables
                foreach (UpdateAttributeCommand a in updateCommands)
                {
                    AttributeMapping attributeMapping = entity.Attributes[a.Name];
                    if (attributeMapping == null)
                        throw new SqlMapperException(string.Format("Cannot find the attribute '{0}' of the entity '{1}' in your mapping file", a.Name, entity.Type));

                    CacheQueryEntry entry = null;

                    if (attributeMapping.Discriminator == null)
                    {
                        // Creates an UpdateCommand for each Table and add the Where clause for the command
                        if (!updateCompoundQueryEntries.Contains(attributeMapping.Table))
                        {
                            CacheQueryEntry cacheEntry = new CacheQueryEntry(new UpdateCommand(attributeMapping, attributeMapping.Table));
                            updateCompoundQueryEntries.Add(attributeMapping.Table, cacheEntry);

                            if ((attributeMapping.ParentField == null || attributeMapping.ParentField == string.Empty) && attributeMapping.Table != entity.Table)
                            {
                                attributeMapping.ParentField = entity.IdFields;
                                Parameter param = new Parameter(attributeMapping, "FK_Entity");
                                cacheEntry.Parameters.Add(param);
                                ((UpdateCommand)cacheEntry.Query).WhereClause.SearchCondition.Add(new BinaryLogicExpression(new Column(attributeMapping, attributeMapping.ParentField), BinaryLogicOperator.Equals, param));
                            }
                        }

                        entry = (CacheQueryEntry)updateCompoundQueryEntries[attributeMapping.Table];
                    }
                    else
                    {
                        // Initialization 
                        entry = new CacheQueryEntry(new UpdateCommand(attributeMapping, attributeMapping.Table));

                        // A tag attribute mapping which name = "*", is an implicit generic query (a mapping for all attributes don't describe)
                        // it is differente to a explicit generic mapping (a mapping foreach attribute)
                        entry.IsAttributeGenericQuery = attributeMapping.Name == "*";
                        updateCompoundQueryEntries.Add(attributeMapping.Table, entry);
                    }

                    UpdateCommand query = (UpdateCommand)entry.Query;
                    ArrayList parameters = entry.Parameters;

                    if (!query.ColumnValueCollection.Contains(attributeMapping.Field))
                    {
                        Parameter param = new Parameter(attributeMapping, attributeMapping.Field);
                        parameters.Add(param);
                        query.ColumnValueCollection.Add(attributeMapping.Field, param);

                        if (attributeMapping.Discriminator != null)
                        {
                            if (attributeMapping.Name != "*")
                            {
                                query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                                    new Column(attributeMapping, attributeMapping.Discriminator),
                                    BinaryLogicOperator.Equals,
                                    new Constant(attributeMapping.DiscriminatorValue == null ? attributeMapping.Name : attributeMapping.DiscriminatorValue, DbType.String)));


                                param = new Parameter(attributeMapping, "FK_Entity");
                                parameters.Add(param);
                                query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(new Column(attributeMapping, attributeMapping.ParentField), BinaryLogicOperator.Equals, param));
                            }
                            else
                            {
                                param = new Parameter(attributeMapping, "AttributeDiscriminator");
                                parameters.Add(param);
                                query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(new Column(attributeMapping, attributeMapping.Discriminator), BinaryLogicOperator.Equals, param));

                                param = new Parameter(attributeMapping, "FK_Entity");
                                parameters.Add(param);
                                query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(new Column(attributeMapping, attributeMapping.ParentField), BinaryLogicOperator.Equals, param));
                            }
                        }

                        if (attributeMapping.ParentField == null && !entry.ContainsParameter("EntityId") && !entry.ContainsParameter("Id0"))
                        {
                            if (entity.Ids.Count == 1)
                            {
                                param = new Parameter(entity.Ids[0], "EntityId");
                                parameters.Add(param);

                                query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                                    new Column(entity, entity.IdFields),
                                    BinaryLogicOperator.Equals,
                                    param));
                            }
                            else
                            {
                                for (int i = 0; i < entity.Ids.Count; i++)
                                {
                                    PrimaryKeyMapping pkId = entity.Ids[i];
                                    param = new Parameter(pkId, string.Concat("Id", i.ToString()));
                                    parameters.Add(param);

                                    query.WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                                        new Column(entity, entity.GetIdField(pkId)),
                                        BinaryLogicOperator.Equals,
                                        param));
                                }
                            }
                        }
                    }
                }
            }

            return updateCompoundQueryEntries;
        }
        private DictionaryEntry FindDictionaryEntry(CacheQueryEntry entry, ArrayList queries)
        {
            foreach (DictionaryEntry d in queries)
            {
                if (d.Key == entry)
                    return d;
            }

            return new DictionaryEntry(entry, _Driver.CreateCommand(_Dialect.RenderQueries(entry.Query, _Driver)[0], _Connection));
        }