public void ProcessEntity(Identifier ident, bool isInRef) { // Restore current entity mapping context EntityMapping entityMapping = (EntityMapping)entityMappingContext.Peek(); // Initialization Table currentTable = new TableSource(entityMapping, entityMapping.Table, CreateUniqueTableAlias()); Column currentItem = new Column(entityMapping.Ids[0], currentTable.TableAlias, entityMapping.GetIdField(entityMapping.Ids[0]), entityMapping.GetIdFieldAs(entityMapping.Ids[0])); SelectStatement currentQuery = new SelectStatement(entityMapping); if (!isInRef) currentQuery.TableAlias = currentTable.TableAlias; // Save current context tableContext.Push(currentTable); sqlExpressionContext.Push(currentItem); queryContext.Push(currentQuery); if (entityMapping.DiscriminatorField != null && !isInRef) { InPredicate ip = new InPredicate(new Column(entityMapping, currentTable.TableAlias, entityMapping.DiscriminatorField)); string parentType = entityMapping.Type; if (_BaseType != null && _BaseType != parentType) { Evaluant.Uss.Models.Entity parentEntity = Model.GetEntity(_BaseType); while (Model.GetParent(parentEntity) != null) parentEntity = Model.GetParent(parentEntity); parentType = parentEntity.Type; } //Evaluant.Uss.Models.Entity parentType = Model.GetEntity(parentType); //Get all the children of the queried type IList children = _Model.GetTree(parentType); ip.SubQueries.Add(new Constant(entityMapping.DiscriminatorValue, DbType.AnsiString)); StringCollection processed = new StringCollection(); foreach (Evaluant.Uss.Models.Entity e in children) { if (e.Type == parentType) continue; EntityMapping current = _Mapping.Entities[e.Type, true]; string currentType = current.DiscriminatorValue; if (processed.Contains(currentType)) continue; processed.Add(currentType); ip.SubQueries.Add(new Constant(currentType, DbType.AnsiString)); } currentQuery.WhereClause.SearchCondition.Add(ip); } // Process constraint : an attribute constraint can modify the context if (ident.Constraint != null) { int stackSizeBefore = entityMappingContext.Count; ProcessConstraint(ident.Constraint); int stackSizeAfter = entityMappingContext.Count; for (int i = stackSizeAfter - 1; i > stackSizeBefore - 1; i--) { entityMappingContext.Pop(); entityModelContext.Pop(); } } }
public override void Visit(Call call) { if (call.Name == "id") { Table table = (Table)tableContext.Peek(); EntityMapping em = (EntityMapping)entityMappingContext.Peek(); if (em.Ids.Count == 1) { Column opRight = new Column(em.Ids[0], table.TableAlias, em.GetIdField(em.Ids[0])); InPredicate ip = new InPredicate(opRight); DbType type = DbType.Object; if (em.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.guid) type = DbType.AnsiString; else { // If generator is inherited => Get the parameters of the parent type EntityMapping parentEM = em; if (em.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.inherited) { Evaluant.Uss.Models.Entity current = Model.GetEntity(em.Type); while (Model.GetParent(current) != null) { current = Model.GetParent(current); parentEM = _Mapping.Entities[current.Type]; if (parentEM.Ids[0].Generator.Name != GeneratorMapping.GeneratorType.inherited) { parentEM.Ids[0].Generator.Params = _Mapping.Entities[current.Type].Ids[0].Generator.Params; break; } } } if (parentEM.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.guid) type = DbType.AnsiString; else type = (DbType)Enum.Parse(typeof(DbType), parentEM.Ids[0].Generator.GetParam(DBDialect.DBTYPE).Value); } foreach (Evaluant.OPath.Expressions.Constraint constraint in call.Operands) { if ((constraint as Value) != null) { if (((Value)constraint).Text == string.Empty) { ((Value)constraint).Text = null; ((Value)constraint).Type = GetValueEnum(type); } } constraint.Accept(this); if (((Constant)sqlExpressionContext.Peek()).Value == null) { sqlExpressionContext.Pop(); sqlExpressionContext.Push(new Constant(DBNull.Value, type)); } ip.SubQueries.Add(new Constant(((Constant)sqlExpressionContext.Pop()).Value, type)); } sqlExpressionContext.Push(ip); } else { // TODO : Logique des clés primaires for (int indexConstraint = 0; indexConstraint < call.Operands.Count; indexConstraint++) { Evaluant.OPath.Expressions.Constraint constraint = call.Operands[indexConstraint]; for (int i = 0; i < em.Ids.Count; i++) { PrimaryKeyMapping pkm = em.Ids[i]; DbType dbType = _Dialect.GetDbTypeToPrimaryKey(pkm.Generator); Value value = constraint as Value; if ((constraint as Value) != null) { if (value.Text == string.Empty) { value.Text = null; value.Type = GetValueEnum(dbType); } } string[] values = value.Text.Split(SqlMapperProvider.IDSEP); if (i >= values.Length) break; Value newValue = new Value(values[i], GetValueEnum(_Dialect.GetDbTypeToPrimaryKey(pkm.Generator))); newValue.Accept(this); if (((Constant)sqlExpressionContext.Peek()).Value == null) { sqlExpressionContext.Pop(); sqlExpressionContext.Push(new Constant(DBNull.Value, dbType)); } Column field = new Column(pkm, table.TableAlias, em.GetIdField(pkm)); sqlExpressionContext.Push(new BinaryLogicExpression(field, BinaryLogicOperator.Equals, (Constant)sqlExpressionContext.Pop())); if (i != 0) { BinaryLogicExpression leftOperand = (BinaryLogicExpression)sqlExpressionContext.Pop(); BinaryLogicExpression rightOperand = (BinaryLogicExpression)sqlExpressionContext.Pop(); sqlExpressionContext.Push(new BinaryLogicExpression(leftOperand, BinaryLogicOperator.And, rightOperand)); } } if (indexConstraint != 0) { BinaryLogicExpression leftOp = (BinaryLogicExpression)sqlExpressionContext.Pop(); BinaryLogicExpression rightOp = (BinaryLogicExpression)sqlExpressionContext.Pop(); sqlExpressionContext.Push(new BinaryLogicExpression(leftOp, BinaryLogicOperator.Or, rightOp)); } } } } }
public virtual void Visit(InPredicate predicate) { predicate.Column.Accept(this); _Query.Append(SPACE); _Query.Append(IN).Append(OPENBRACE); if(predicate.SubQueries.Count > 0) { if(predicate.SubQueries[0] is SelectStatement) predicate.SubQueries[0].Accept(this); if(predicate.SubQueries[0] is Constant) { foreach(Constant val in predicate.SubQueries) { val.Accept(this); _Query.Append(COMMA); } _Query.Remove(_Query.Length - 2, 2); } } _Query.Append(CLOSEBRACE); }