/// <summary> /// Processes the attributes. /// </summary> /// <param name="entityMap">Entity mapping whitch contains the attribute</param> /// <param name="name">Name of the attribute</param> /// <param name="loadAttributes"></param> /// <param name="checkAlias">Indicate if returned column must have an computed alias name or not</param> /// <returns>Return a <see cref="Column"/></returns> public ISQLExpression ProcessAttributes(EntityMapping entityMap, string name, bool loadAttributes, bool checkAlias) { if (name == "Id") { Table currentTable = (Table)tableContext.Peek(); return new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0])); } else { // Exception management : attribute mapping AttributeMapping attributeMapping = entityMap.Attributes[name]; //Attempt to get the attribute in the children types if (attributeMapping == null) foreach (Evaluant.Uss.Models.Entity e in _Model.GetTree(entityMap.Type)) { EntityMapping em = _Mapping.Entities[e.Type]; if ((attributeMapping = em.Attributes[name]) != null) break; } //Attempt to get the attribute in the parent types if (attributeMapping == null) { Evaluant.Uss.Models.Entity cur = _Model.GetEntity(entityMap.Type); while (_Model.GetParent(cur) != null) { cur = _Model.GetParent(cur); EntityMapping current = _Mapping.Entities[cur.Type]; if (current.Attributes[name] != null) attributeMapping = current.Attributes[name]; } } if (attributeMapping == null) throw new UniversalStorageException(String.Format("The attribute '{0}' of the entity '{1}' is not defined in your mapping file", name, entityMap.Type)); // If the attribute is contained in entity's table : the current query doesn't change if (attributeMapping.Table == entityMap.Table) { Table currentTable = (Table)tableContext.Peek(); Column c = new Column(attributeMapping, currentTable.TableAlias, attributeMapping.Field); //if (entityMap.Ids[attributeMapping.Field] != null) // c.ColumnName = entityMap.GetIdField(entityMap.Ids[attributeMapping.Field]); if (checkAlias) { string attributeFullName = _Model.GetFullAttributeName(attributeMapping.Name, attributeMapping.ParentEntity.Type); //if (entityMap.Ids[attributeMapping.Field] != null) // attributeFullName = entityMap.GetIdFieldAs(entityMap.Ids[attributeMapping.Field]); //c.ColumnName = (_AliasColumnMapping.Contains(attributeFullName)) ? _AliasColumnMapping[attributeFullName].ToString() : ""; if (_AliasColumnMapping.Contains(attributeFullName)) c.ColumnName = _AliasColumnMapping[attributeFullName].ToString(); } return c; } // Else : create a new select query else { // Get the current context Table currentTable = (Table)tableContext.Pop(); SelectStatement mainQuery = (SelectStatement)queryContext.Pop(); // Create a new query SelectStatement currentQuery = new SelectStatement(attributeMapping); Table attributeTable = new TableSource(attributeMapping, attributeMapping.Table, CreateUniqueAttributeAlias()); string atrbId = attributeMapping.Table == null || attributeMapping.Table == entityMap.Table ? entityMap.GetIdField(entityMap.Ids[0]) : attributeMapping.ParentField; if (loadAttributes) { return this.ProcessAttributesWithLoadAttribute(mainQuery, currentQuery, currentTable, entityMap, attributeTable, attributeMapping, atrbId); } else { if (_IsFirstAttribute) { _IsFirstAttribute = false; // if current table is a JoinedTable : // from clause = mainQuery INNER JOIN attributeTable if (currentTable.GetType() == typeof(JoinedTable)) { JoinedTable lastJoinTable = new JoinedTable(mainQuery, loadAttributes ? TypeJoinedTable.LeftOuter : TypeJoinedTable.Inner, attributeTable); lastJoinTable.TableAlias = mainQuery.TableAlias; BinaryLogicExpression onCondition = new BinaryLogicExpression( new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId)); if (mainQuery.WhereClause.SearchCondition.Count != 0) { foreach (ILogicExpression expression in mainQuery.WhereClause.SearchCondition) { onCondition = new BinaryLogicExpression(onCondition, BinaryLogicOperator.And, expression); } } lastJoinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(entityMap.Ids[0], attributeTable.TableAlias, atrbId))); currentQuery.TableAlias = mainQuery.TableAlias; currentQuery.FromClause.Add(lastJoinTable); currentQuery.WhereClause.SearchCondition.Add(new BinaryLogicExpression( new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Discriminator), BinaryLogicOperator.Equals, new Constant(name, DbType.AnsiString))); tableContext.Push(lastJoinTable); queryContext.Push(currentQuery); return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field); } // else // from clause = currentTable INNER JOIN attributeTable else { JoinedTable joinTable = new JoinedTable(currentTable, loadAttributes ? TypeJoinedTable.LeftOuter : TypeJoinedTable.Inner, attributeTable); joinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId))); currentQuery.TableAlias = CreateUniqueTableAlias(); currentQuery.FromClause.Add(joinTable); currentQuery.WhereClause.SearchCondition = mainQuery.WhereClause.SearchCondition; tableContext.Push(joinTable); } } else { Table rightTable = ((JoinedTable)currentTable).RigthTable; JoinedTable joinTable = new JoinedTable(attributeTable, TypeJoinedTable.Inner, rightTable); joinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(attributeMapping, rightTable.TableAlias, atrbId), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId))); ((JoinedTable)currentTable).RigthTable = joinTable; currentQuery.SelectList.Add(new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0]))); currentQuery.TableAlias = currentTable.TableAlias; currentQuery.FromClause.Add(currentTable); tableContext.Push(currentTable); } queryContext.Push(currentQuery); return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field); } } } }
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 ISQLExpression ProcessAttributesWithLoadAttribute(SelectStatement mainQuery, SelectStatement currentQuery, Table currentTable, EntityMapping entityMap, Table attributeTable, AttributeMapping attributeMapping, string atrbId) { if (_IsFirstAttribute) { _IsFirstAttribute = false; // if current table is a JoinedTable : // from clause = mainQuery INNER JOIN attributeTable if (currentTable is JoinedTable) { JoinedTable lastJoinTable = new JoinedTable(mainQuery, TypeJoinedTable.LeftOuter, attributeTable); lastJoinTable.TableAlias = mainQuery.TableAlias; BinaryLogicExpression onCondition = new BinaryLogicExpression( new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId)); if (mainQuery.WhereClause.SearchCondition.Count != 0) { foreach (ILogicExpression expression in mainQuery.WhereClause.SearchCondition) { onCondition = new BinaryLogicExpression(onCondition, BinaryLogicOperator.And, expression); } } lastJoinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(entityMap.Ids[0], attributeTable.TableAlias, atrbId))); currentQuery.TableAlias = mainQuery.TableAlias; currentQuery.FromClause.Add(lastJoinTable); tableContext.Push(lastJoinTable); for (int i = 0; i < 2; i++) { Column column = mainQuery.SelectList[i] as Column; if (column != null && column.ColumnName == entityMap.Ids[0].Field && entityMap.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.inherited) { column.Alias = null; currentQuery.SelectList.Add(new Column(column.TagMapping, lastJoinTable.TableAlias, column.ColumnName, entityMap.GetIdFieldAs(entityMap.Ids[0]))); } else { currentQuery.SelectList.Add(mainQuery.SelectList[i]); } } if (entityMap.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.inherited) { for (int i = 2; i < mainQuery.SelectList.Count; i++) { Column column = mainQuery.SelectList[i] as Column; if (column != null) { currentQuery.SelectList.Add(new Column(column.TagMapping, lastJoinTable.TableAlias, column.GetSelectName())); } } } if (attributeMapping.Discriminator != null) { currentQuery.SelectList.Add(new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Discriminator)); } queryContext.Push(currentQuery); string alias = (attributeMapping.Field != attributeMapping.Name) ? attributeMapping.Name : String.Empty; return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field); } // else // from clause = currentTable INNER JOIN attributeTable else { JoinedTable joinTable = new JoinedTable(currentTable, TypeJoinedTable.LeftOuter, attributeTable); joinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0])), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId))); currentQuery.TableAlias = currentTable.TableAlias; currentQuery.FromClause.Add(joinTable); currentQuery.WhereClause.SearchCondition = mainQuery.WhereClause.SearchCondition; foreach (ISQLExpression exp in mainQuery.SelectList) { if (!currentQuery.SelectList.Contains(exp)) currentQuery.SelectList.Add(exp); } tableContext.Push(joinTable); } } else { Table rightTable = ((JoinedTable)currentTable).RigthTable; JoinedTable joinTable = new JoinedTable(rightTable, TypeJoinedTable.LeftOuter, attributeTable); joinTable.SearchConditions.Add(new BinaryLogicExpression( new Column(attributeMapping, rightTable.TableAlias, atrbId), BinaryLogicOperator.Equals, new Column(attributeMapping, attributeTable.TableAlias, atrbId))); ((JoinedTable)currentTable).RigthTable = joinTable; currentQuery.TableAlias = currentTable.TableAlias; currentQuery.FromClause.Add(currentTable); currentQuery.WhereClause.SearchCondition = mainQuery.WhereClause.SearchCondition; tableContext.Push(currentTable); } foreach (ISQLExpression exp in mainQuery.SelectList) { if (!currentQuery.SelectList.Contains(exp)) { currentQuery.SelectList.Add(exp); } } if (attributeMapping.Discriminator != null) { currentQuery.SelectList.Add(new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Discriminator)); } queryContext.Push(currentQuery); string alias1 = (attributeMapping.Field != attributeMapping.Name) ? attributeMapping.Name : String.Empty; return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field, alias1); }