/// <summary> /// Adds the elements of an array to the end of this ColumnCollection. /// </summary> /// <param name="items"> /// The array whose elements are to be added to the end of this ColumnCollection. /// </param> public virtual void AddRange(OrderByClauseColumn[] items) { foreach (OrderByClauseColumn item in items) { this.List.Add(item); } }
public ISQLExpression TransformToSql(Path path, string[] attributes, string[] orderby, bool lastIsAttribute) { // Get all the entity of the hierarchy (parent and children) string baseType = path.Identifiers[0].Value; IList tree = _Model.GetTree(baseType); _UserAttributes = new StringCollection(); // attributes == null => don't get attributes if (attributes != null) { // Get the specified class and store all the inherited classes Evaluant.Uss.Models.Entity parent = null; Evaluant.Uss.Models.EntityCollection parents = new Evaluant.Uss.Models.EntityCollection(); if (path.Identifiers.Count == 1) parent = _Model.GetEntity(path.Identifiers[0].Value); else { string refName = path.Identifiers[path.Identifiers.Count - 1].Value; string firstType = path.Identifiers[0].Value; string lastType = firstType; string nextRefName = string.Empty; for (int i = 0; i < path.Identifiers.Count - 1; i++) { nextRefName = path.Identifiers[i + 1].Value; // The next identifier isn't a reference but an attribute if (_Model.GetReference(lastType, nextRefName) == null && _Model.GetAttribute(lastType, nextRefName) != null) continue; lastType = _Model.GetReference(lastType, nextRefName, true).ChildType; } parent = _Model.GetEntity(lastType); } if (parent == null) throw new Evaluant.Uss.Models.ModelElementNotFoundException(String.Format("The type [{0}] was not found. Check your metadata.", baseType)); if (attributes.Length == 0) { // attributes.Length == 0 => get all attributes _UserAttributes.AddRange(_Model.GetFullAttributesNames(parent.Type)); } else { // attributes.Length > 0 => get the user defined attributes foreach (string attName in attributes) { _UserAttributes.Add(_Model.GetFullAttributeName(attName, parent.Type)); } } } UnionStatement sqlQuery = new UnionStatement(); // Process each entity of the tree and put the corresponding sql query // into a UnionStatement StringCollection processedTables = new StringCollection(); _BaseType = path.Identifiers[0].Value; Hashtable expressions = new Hashtable(); string commonIdField = string.Empty; foreach (Evaluant.Uss.Models.Entity entity in tree) { // If the entity doesn't have a mapping => don't process it if (_Mapping.Entities[entity.Type] == null) continue; path.Identifiers[0].Value = entity.Type; EntityMapping entityMapping = _Mapping.Entities[entity.Type]; if (processedTables.Contains(entityMapping.Table)) continue; processedTables.Add(entityMapping.Table); _ExprLevel.Push(PATH); SelectStatement exp = (SelectStatement)TransformToSql(path, _UserAttributes, true, lastIsAttribute); _ExprLevel.Pop(); // process alias name calculation only once _IsAliasColumnComputed = true; expressions.Add(entity.Type, exp); string parentType = string.Empty; if (_Model.GetParent(_Model.GetEntity(entity.Type)) != null) parentType = _Model.GetParent(_Model.GetEntity(entity.Type)).Type; if (parentType == string.Empty || entityMapping.Ids[0].Generator.Name != GeneratorMapping.GeneratorType.inherited || parentType != _BaseType) continue; EntityMapping parentMapping = _Mapping.Entities[parentType]; SelectStatement sel = (SelectStatement)expressions[parentType]; SelectStatement exludeClause = new SelectStatement(null); exludeClause.SelectedAllColumns = true; exludeClause.FromClause.Add(new TableSource(entityMapping, entityMapping.Table)); string parentIdField = parentMapping.GetIdField(parentMapping.Ids[0]); if (path.Identifiers.Count > 1) parentIdField = "ParentId"; exludeClause.WhereClause.SearchCondition.Add(new BinaryLogicExpression( new Column(parentMapping.Ids[0], sel.TableAlias, parentIdField), BinaryLogicOperator.Equals, new Column(entityMapping.Ids[0], entityMapping.Table, entityMapping.GetIdField(entityMapping.Ids[0])))); ExistsPredicate ex = new ExistsPredicate(exludeClause); UnaryLogicExpression ua = new UnaryLogicExpression(UnaryLogicOperator.Not, ex); sel.WhereClause.SearchCondition.Add(ua); //sqlQuery.SelectExpressions.Add(exp); } //If there are many tables in the union statement, make the id fields aliases identical bool isFirstSelect = true; ArrayList idAliases = new ArrayList(); foreach (ISQLExpression exp in expressions.Values) { SelectStatement innerSelect = (SelectStatement)exp; if (isFirstSelect) { foreach (ISQLExpression innerExp in innerSelect.SelectList) { if (innerExp is Column && ((Column)innerExp).Alias.StartsWith(EntityMapping.PREFIX_ID)) idAliases.Add(((Column)innerExp).Alias); } isFirstSelect = false; } else { int i = 0; foreach (ISQLExpression innerExp in innerSelect.SelectList) { if (innerExp is Column && ((Column)innerExp).Alias.StartsWith(EntityMapping.PREFIX_ID)) { if (idAliases.Count > i) ((Column)innerExp).Alias = (string)idAliases[i]; else throw new UniversalStorageException(string.Format("entity Id fields do not meet the one of its parent '{0}'", _BaseType)); i++; } } } //Add the SelectStatement to the unionStatement sqlQuery.SelectExpressions.Add(innerSelect); } if (entityMappingContext.Count == 0) { throw new MappingNotFoundException(String.Format("Type not found [{0}]", baseType)); } EntityMapping em = (EntityMapping)entityMappingContext.Peek(); if (orderby != null) { for (int i = 0; i < orderby.Length; i++) { string order = orderby[i].Trim(); if (order != string.Empty) { bool isDesc = false; OrderByClauseColumn col = null; int indexOfDirection = 0; if ((indexOfDirection = order.ToLower().IndexOf(" desc")) > 0) { order = order.Substring(0, indexOfDirection).Trim(); isDesc = true; } else if ((indexOfDirection = order.ToLower().IndexOf(" asc")) > 0) order = order.Substring(0, indexOfDirection).Trim(); if (order == "Id") { col = new OrderByClauseColumn(em.GetIdField(em.Ids[0]), true); } else { AttributeMapping orderMapping = em.Attributes[order, true]; string orderColName = orderMapping.Name; string fullAttributeName = _Model.GetFullAttributeName(orderMapping.Name, orderMapping.ParentEntity.Type); if (_AliasColumnMapping.Contains(fullAttributeName)) orderColName = _AliasColumnMapping[fullAttributeName].ToString(); col = new OrderByClauseColumn(orderColName, isDesc); } sqlQuery.OrderByClause.Add(col); } } } if (sqlQuery.OrderByClause.Count == 0 && !lastIsAttribute && orderby != null) { // Id position = 1 if loading single Entity (Person or Person[Name = 'p1'] // Id position = 2 if loading referenced Entity (Person.Partners or Person[Name = 'p1'].Partners) int idPosition = (path.Identifiers.Count == 1) ? 1 : 2; if (idPosition == 2) idPosition = _Mapping.Entities[path.Identifiers[0].Value].Ids.Count + 1; Column columnId = (Column)sqlQuery.SelectExpressions[0].SelectList[idPosition]; // get the Id alias of the first SelectStatement in case of UnionStatement if (sqlQuery.SelectExpressions[0] is UnionStatement) { columnId = (Column)((UnionStatement)sqlQuery.SelectExpressions[0]).SelectExpressions[0].SelectList[idPosition]; } EntityMapping entityMap = null; if (columnId.TagMapping is PrimaryKeyMapping) entityMap = ((PrimaryKeyMapping)columnId.TagMapping).ParentEntity; if (columnId.TagMapping is RuleMapping) { if (idPosition == 2) entityMap = _Mapping.Entities[((RuleMapping)columnId.TagMapping).ParentReference.EntityChild]; else entityMap = ((RuleMapping)columnId.TagMapping).ParentReference.EntityParent; } if (entityMap != null) { SelectStatement selectQuery = sqlQuery; if (sqlQuery is UnionStatement && ((UnionStatement)sqlQuery).SelectExpressions.Count > 0) selectQuery = ((UnionStatement)sqlQuery).SelectExpressions[0] as SelectStatement; foreach (PrimaryKeyMapping pkm in entityMap.Ids) { OrderByClauseColumn orderCol = null; foreach (ISQLExpression exp in selectQuery.SelectList) { if (exp is Column && exp.TagMapping == pkm) { Column col = exp as Column; orderCol = new OrderByClauseColumn(col.Alias); break; } } if (orderCol == null) { AttributeMapping attribute = entityMap.Attributes.FindByField(pkm.Field); string idFieldAlias = pkm.ParentEntity.GetIdFieldAs(pkm); foreach (string alias in _AliasColumnMapping.Keys) { if (alias.Contains(".") && attribute != null) { if (alias.Split('.')[1] == attribute.Name) { orderCol = new OrderByClauseColumn(_AliasColumnMapping[alias].ToString()); break; } } else if (alias == idFieldAlias) { orderCol = new OrderByClauseColumn(_AliasColumnMapping[alias].ToString()); break; } } if (orderCol == null) orderCol = new OrderByClauseColumn(em.GetIdFieldAs(pkm)); } if (orderCol != null) sqlQuery.OrderByClause.Add(orderCol); } } } return sqlQuery; }
/// <summary> /// Removes the first occurrence of a specific Column from this ColumnCollection. /// </summary> /// <param name="value"> /// The Column value to remove from this ColumnCollection. /// </param> public virtual void Remove(OrderByClauseColumn value) { this.List.Remove(value); }
/// <summary> /// Initializes a new instance of the ColumnCollection class, containing elements /// copied from an array. /// </summary> /// <param name="items"> /// The array whose elements are to be added to the new ColumnCollection. /// </param> public OrderByClauseColumnCollection(OrderByClauseColumn[] items) { this.AddRange(items); }
/// <summary> /// Inserts an element into the ColumnCollection at the specified index /// </summary> /// <param name="index"> /// The index at which the Column is to be inserted. /// </param> /// <param name="value"> /// The Column to insert. /// </param> public virtual void Insert(int index, OrderByClauseColumn value) { this.List.Insert(index, value); }
/// <summary> /// Return the zero-based index of the first occurrence of a specific value /// in this ColumnCollection /// </summary> /// <param name="value"> /// The Column value to locate in the ColumnCollection. /// </param> /// <returns> /// The zero-based index of the first occurrence of the _ELEMENT value if found; /// -1 otherwise. /// </returns> public virtual int IndexOf(OrderByClauseColumn value) { return this.List.IndexOf(value); }
/// <summary> /// Determines whether a specfic Column value is in this ColumnCollection. /// </summary> /// <param name="value"> /// The Column value to locate in this ColumnCollection. /// </param> /// <returns> /// true if value is found in this ColumnCollection; /// false otherwise. /// </returns> public virtual bool Contains(OrderByClauseColumn value) { return this.List.Contains(value); }
/// <summary> /// Adds an instance of type Column to the end of this ColumnCollection. /// </summary> /// <param name="value"> /// The Column to be added to the end of this ColumnCollection. /// </param> public virtual void Add(OrderByClauseColumn value) { this.List.Add(value); }
public virtual void Visit(OrderByClauseColumn column) { if (!string.IsNullOrEmpty(column.TableName)) { //if (!string.IsNullOrEmpty(schema)) // _Query.Append(FormatAttribute(schema + DOT + column.TableName)).Append(DOT); //else _Query.Append(FormatAttribute(column.TableName)).Append(DOT); } _Query.Append(FormatAttribute(column.ColumnName)).Append(SPACE); if (column.ColumnName != string.Empty && column.Desc) _Query.Append(DESC); }