private static Tuple <EntityAttribute, string> LocateAttribute(EntityMetadata entityMetadata, IEnumerable <EntityAttribute> attributes, ProjectionField field) { var attributeName = field.Name; if (!attributeName.Contains('.')) { var resultAttribute = attributes.FirstOrDefault(f => f.Name.Equals(attributeName)); if (resultAttribute == null) { return(null); } return(new Tuple <EntityAttribute, string>(resultAttribute, null)); } string currentAttributeName = attributeName; string resultName; EntityMetadata innerMetadata = entityMetadata; string context = ""; do { var relationshipName = EntityUtil.GetRelationshipName(currentAttributeName, out resultName); context += relationshipName; innerMetadata = innerMetadata.RelatedEntityMetadata(relationshipName); currentAttributeName = resultName; } while (currentAttributeName.Contains("_") && innerMetadata != null); if (innerMetadata == null) { return(null); } var attribute = innerMetadata.Attributes(NoCollections).FirstOrDefault(f => f.Name.EqualsIc(resultName)); return(new Tuple <EntityAttribute, string>(attribute, context)); }
public static string BuildSearchSort(EntityMetadata entityMetadata, SearchRequestDto dto) { var searchSort = dto.SearchSort; if (String.IsNullOrWhiteSpace(searchSort)) { if (entityMetadata.HasUnion()) { return(" order by {0} desc".Fmt(entityMetadata.Schema.IdAttribute.Name)); } return(String.Format(" order by {0}.{1} desc", entityMetadata.Name, entityMetadata.Schema.IdAttribute.Name)); } var suffix = dto.SearchAscending ? " asc " : " desc "; if (searchSort.EndsWith("asc") || searchSort.EndsWith("desc")) { suffix = ""; } var attrs = entityMetadata.Attributes(EntityMetadata.AttributesMode.NoCollections); var attribute = attrs.FirstOrDefault(f => f.Name.Equals(dto.SearchSort.Trim(), StringComparison.CurrentCultureIgnoreCase)); if (attribute != null && attribute.Query != null) { return(GetQuerySortBy(entityMetadata, attribute, suffix)); } if (!searchSort.Contains(".")) { //if union we cannot use full qualified names if (!dto.ExpressionSort && !entityMetadata.HasUnion()) { return(String.Format(" order by {0}.{1} {2}", entityMetadata.Name, searchSort, suffix)); } return(String.Format(" order by {0} {1}", searchSort, suffix)); } if (entityMetadata.HasUnion()) { //this might replace sr_.ticketid for sr_ticketid, which is the right alias for paginated queries with union... searchSort = searchSort.Replace(".", ""); } return(String.Format(" order by {0} {1}", searchSort, suffix)); }
//_ used in db2 private string FixKey(string key, EntityMetadata entityMetadata) { if (entityMetadata.Attributes(EntityMetadata.AttributesMode.NoCollections).Any(a => a.Name.EqualsIc(key))) { //IF the entity has a non collection attribute declared containing _, let´s keep it, cause it could be the name of the column actually return(key.ToLower()); } // TODO: This needs to be revisited when we integrate any DB2 customer. // TODO: Not working for current customer because they could have attributes that are with underscore like feature_request - KSW-104 if (key.Contains("_") && !key.Contains(".")) { if (key.IndexOf("_", System.StringComparison.Ordinal) != key.LastIndexOf("_", System.StringComparison.Ordinal)) { //more then one _ ==> replace only last return(key.ReplaceLastOccurrence("_", "_.").ToLower()); } return(key.Replace("_", "_.").ToLower()); } return(key.ToLower()); }
public static string BuildSelectAttributesClause(EntityMetadata entityMetadata, QueryCacheKey.QueryMode queryMode , SearchRequestDto dto = null) { var buffer = new StringBuilder(); if (queryMode == QueryCacheKey.QueryMode.Count) { return(CountClause); } buffer.AppendFormat("select "); if (entityMetadata.FetchLimit() != null && queryMode == QueryCacheKey.QueryMode.Sync) { buffer.Append(string.Format(" top({0}) ", entityMetadata.FetchLimit())); } var attributes = entityMetadata.Attributes(NoCollections) as IList <EntityAttribute> ?? entityMetadata.Attributes(NoCollections).ToList(); var hasProjection = dto != null && dto.ProjectionFields.Count > 0; if (hasProjection) { foreach (ProjectionField field in dto.ProjectionFields) { if (field.Name.StartsWith("#")) { if (field.Name.StartsWith("#null")) { //this way we can map null attributes, that can be used for unions //see changeunionschema of hapag´s metadata.xml buffer.AppendFormat("null" + SelectSeparator); } //this is an unmapped attribute continue; } var result = LocateAttribute(entityMetadata, attributes, field); if (!field.Name.Contains('.') && result == null) { //this field is not mapped continue; } string aliasAttribute; if (result != null && result.Item1.Query != null) { aliasAttribute = AliasAttribute(entityMetadata, field.Alias, result.Item1, result.Item2); } else { aliasAttribute = AliasAttribute(entityMetadata, field); } buffer.AppendFormat(aliasAttribute + SelectSeparator); } } else { for (var i = 0; i < attributes.Count; i++) { var entityAttribute = attributes[i]; if (entityAttribute.Name.StartsWith("#null")) { //this way we can map null attributes, that can be used for unions //see changeunionschema of hapag´s metadata.xml buffer.AppendFormat("null" + SelectSeparator); } else { var aliasAttribute = AliasAttribute(entityMetadata, entityAttribute); buffer.AppendFormat(aliasAttribute + SelectSeparator); } } } return(buffer.ToString().Substring(0, buffer.Length - SelectSeparator.Count()) + " "); }