/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } Validate(); IEnumerable <PredicateToken> predicateTokens = searchTokens.FetchPredicateToken(Token); if (predicateTokens.Count() > 0) { // Assume the relationship direction as the first. this.ReverseRelation = predicateTokens.FirstOrDefault().ReverseRelation; StringBuilder transactionSqlBuilder = new StringBuilder(); AppendCriteriaTSql(transactionSqlBuilder, predicateTokens); return(transactionSqlBuilder.ToString()); } else { throw new SearchException(string.Format(CultureInfo.CurrentCulture, Resources.SEARCH_INVALID_PREDICATE_TOKEN, Token)); } }
/// <summary> /// Joins the TSQL. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="clause">The clause.</param> /// <param name="leftChildTSql">The left child T SQL.</param> /// <param name="rightChildTSql">The right child T SQL.</param> /// <returns>Joins the transact sql.</returns> private string JoinTsql( SearchTokens searchTokens, string clause, string leftChildTSql, string rightChildTSql) { if (string.IsNullOrEmpty(rightChildTSql)) { return(string.Empty); } else { StringBuilder transactionSqlBuilder = new StringBuilder(); if (LeftChild.ReverseRelation) { transactionSqlBuilder.AppendFormat(CultureInfo.InvariantCulture, SearchConstants.TSQL_REVERSE_PREDICATE_QUERY, clause, leftChildTSql, rightChildTSql); } else { transactionSqlBuilder.AppendFormat(CultureInfo.InvariantCulture, SearchConstants.TSQL_PREDICATE_QUERY, clause, leftChildTSql, rightChildTSql); } transactionSqlBuilder.Append(SearchConstants.SPACE); transactionSqlBuilder.Append(SearchConstants.AND); transactionSqlBuilder.Append(SearchConstants.SPACE); base.AppendsWhereClause(transactionSqlBuilder, searchTokens); return(transactionSqlBuilder.ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="Zentity.Platform.Parser"/> class. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> public Parser(SearchTokens searchTokens) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens"); } this.searchTokens = searchTokens; }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } return(CreateTsql(searchTokens, String.Empty)); }
/// <summary> /// Creates the Transact-SQL. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="clause">The clause.</param> /// <returns>The transact sql.</returns> private string CreateTsql(SearchTokens searchTokens, string clause) { // Need to capture in variables since LeftChild.ReverseRelation is set in the ConvertToTSql method. string leftChildTSql = LeftChild.ConvertToTSql(searchTokens, false, null, false); string rightChildTSql = RightChild.ConvertToTSql(searchTokens, false, null, false); return(JoinTsql(searchTokens, SearchConstants.COMMA + clause, leftChildTSql, rightChildTSql)); }
/// <summary> /// Initializes a new instance of the <see cref="Interpreter"/> class. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <param name="maximumResultCount">Maximum number of results to return in paged search.</param> public Interpreter(SearchTokens searchTokens, int maximumResultCount) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens"); } this.searchTokens = searchTokens; MaximumResultCount = maximumResultCount; }
/// <summary> /// Inserts the sort column. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="columnName">Name of the column.</param> /// <returns>Search tokens after inserting the sort column.</returns> private string InsertSortColumn(SearchTokens searchTokens, string columnName) { if (!String.Equals(columnName, SearchConstants.TSQL_ID, StringComparison.OrdinalIgnoreCase)) { return(CreateTsql(searchTokens, SearchConstants.TSQL_SUB + columnName)); } else { return(ConvertToTSql(searchTokens)); } }
/// <summary> /// Creates the TSQL. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="clause">The clause.</param> /// <returns>The t-sql.</returns> private string CreateTsql(SearchTokens searchTokens, string clause) { StringBuilder transactionSqlBuilder = new StringBuilder(); transactionSqlBuilder.AppendFormat( CultureInfo.InvariantCulture, SearchConstants.TSQL_RESOURCE_QUERY, clause); AppendsWhereClause(transactionSqlBuilder, searchTokens); return(transactionSqlBuilder.ToString()); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } string leftQuery = (LeftChild == null) ? null : LeftChild.ConvertToTSql(searchTokens); string rightQuery = (RightChild == null) ? null : RightChild.ConvertToTSql(searchTokens); return(JoinQueries(leftQuery, SearchConstants.TSQL_EXCEPT, rightQuery)); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } // Need to capture in variables since LeftChild.ReverseRelation is set in the ConvertToTSql method. string leftChildTSql = LeftChild.ConvertToTSql(searchTokens); string rightChildTSql = RightChild.ConvertToTSql(searchTokens); return(JoinTsql(searchTokens, String.Empty, leftChildTSql, rightChildTSql)); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <param name="insertClause">If true, insert the specified clause in TSQL.</param> /// <param name="clause">Sort column name or similarity clause to be inserted.</param> /// <param name="isSimilarityClause">If true, insert the specified similarity clause in /// TSQL else insert sort clause.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens, bool insertClause, string clause, bool isSimilarityClause) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } string leftQuery = (LeftChild == null) ? null : LeftChild.ConvertToTSql(searchTokens, insertClause, clause, isSimilarityClause); string rightQuery = (RightChild == null) ? null : RightChild.ConvertToTSql(searchTokens, insertClause, clause, isSimilarityClause); return(JoinQueries(leftQuery, SearchConstants.TSQL_UNION, rightQuery)); }
/// <summary> /// Fetches the column name based on the resource property. /// </summary> /// <param name="propertyName">Property name.</param> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch /// tokens with.</param> /// <param name="resourceTypeFullName">Resource type full name.</param> /// <returns>Column name exclosed within box brackets.</returns> public static string GetColumnName(string propertyName, SearchTokens searchTokens, string resourceTypeFullName) { ScalarProperty scalarProperty = searchTokens.FetchPropertyToken(propertyName, resourceTypeFullName); if (scalarProperty != null) { return(Utility.EscapeColumnName(scalarProperty.ColumnName)); } else { throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Resources.SEARCH_INVALID_SORT_PROPERTY, propertyName)); } }
/// <summary> /// Appends the where clause of the query. /// </summary> /// <param name="transactionSqlBuilder"><see cref="StringBuilder" /> instance to append T-SQL to.</param> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> public override void AppendsWhereClause(StringBuilder transactionSqlBuilder, SearchTokens searchTokens) { ValidateInputs(); IEnumerable <ScalarProperty> properties = ExtractTokenInformation(searchTokens); if (properties.Count() != 0) { transactionSqlBuilder.Append(SearchConstants.OPEN_ROUND_BRACKET); AppendPropertiesTSql(transactionSqlBuilder, properties); transactionSqlBuilder.Append(SearchConstants.CLOSE_ROUND_BRACKET); transactionSqlBuilder.Append(SearchConstants.SPACE); transactionSqlBuilder.Append(SearchConstants.AND); transactionSqlBuilder.Append(SearchConstants.SPACE); base.AppendsWhereClause(transactionSqlBuilder, searchTokens); } else { transactionSqlBuilder.Remove(0, transactionSqlBuilder.Length); } }
/// <summary> /// Extracts the data type and properties of the token. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <returns>List of <see cref="ScalarProperty"/>.</returns> private IEnumerable <ScalarProperty> ExtractTokenInformation(SearchTokens searchTokens) { IEnumerable <ScalarProperty> properties; switch (TokenType) { case ExpressionTokenType.SpecialToken: { SpecialToken token = searchTokens.FetchSpecialToken(ExpressionToken, ResourceTypeFullName); if (token == null) { throw new SearchException(string.Format(CultureInfo.CurrentCulture, Resources.SEARCH_INVALID_SPECIAL_TOKEN, ExpressionToken)); } properties = token.Properties; } break; case ExpressionTokenType.PropertyToken: { ScalarProperty token = searchTokens.FetchPropertyToken(ExpressionToken, ResourceTypeFullName); if (token == null) { throw new SearchException(string.Format(CultureInfo.CurrentCulture, Resources.SEARCH_INVALID_PROPERTY_TOKEN, ExpressionToken)); } properties = new ScalarProperty[] { token }; } break; case ExpressionTokenType.ImplicitPropertiesToken: default: { IEnumerable <ScalarProperty> tokens = searchTokens.FetchImplicitProperties(ResourceTypeFullName); properties = tokens; } break; } return(properties); }
/// <summary> /// Appends the T-SQL equivalent of resource type. /// </summary> /// <param name="transactionSqlBuilder"> /// <see cref="StringBuilder" /> instance to append T-SQL to.</param> /// <param name="resourceTypeFullName">Resource type full name.</param> /// <param name="searchTokens"> /// <see cref="SearchTokens" /> instance to fetch tokens with.</param> public static void AppendResourceTypesTSql( StringBuilder transactionSqlBuilder, string resourceTypeFullName, SearchTokens searchTokens) { transactionSqlBuilder.Append(SearchConstants.OPEN_ROUND_BRACKET); ResourceType type = searchTokens.FetchResourceType(resourceTypeFullName); IEnumerable <string> excludedResourceTypeFullNames = SearchTokens.FetchExcludedResourceTypeFullNames(); if (!excludedResourceTypeFullNames .Contains(resourceTypeFullName.ToLower())) { AppendResourceTypeTSql(transactionSqlBuilder, type.Id); } else { // If all the types in hierarchy are in excluded list, // query formed should return zero results, // hence attaching a random Guid as a resource Id. AppendResourceTypeTSql(transactionSqlBuilder, Guid.NewGuid()); } IEnumerable <ResourceType> derivedTypes = searchTokens.FetchResourceDerivedTypes(type) .Where(resourceType => !excludedResourceTypeFullNames .Contains(resourceType.FullName.ToLower())); IEnumerable <Guid> derivedTypeIds = derivedTypes .Select(resourceType => resourceType.Id); foreach (Guid derivedTypeId in derivedTypeIds) { transactionSqlBuilder.Append(SearchConstants.OR); AppendResourceTypeTSql(transactionSqlBuilder, derivedTypeId); } transactionSqlBuilder.Append(SearchConstants.CLOSE_ROUND_BRACKET); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <param name="insertClause">If true, insert the specified clause in TSQL.</param> /// <param name="clause">Sort column name or similarity clause to be inserted.</param> /// <param name="isSimilarityClause">If true, insert the specified similarity clause in /// TSQL else insert sort clause.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens, bool insertClause, string clause, bool isSimilarityClause) { if (insertClause) { if (searchTokens == null) { throw new ArgumentNullException("searchTokens", Resources.EXCEPTION_ARGUMENTINVALID); } if (!isSimilarityClause) { return(InsertSortColumn(searchTokens, clause)); } else { return(InsertSimilarityClause(searchTokens, clause)); } } else { return(ConvertToTSql(searchTokens)); } }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <returns>T-SQL equivalent of the node.</returns> public override string ConvertToTSql(SearchTokens searchTokens) { throw new NotImplementedException(); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <param name="insertClause">If true, insert the specified clause in TSQL.</param> /// <param name="clause">Sort column name or similarity clause to be inserted.</param> /// <param name="isSimilarityClause">If true, insert the specified similarity clause in /// TSQL else insert sort clause.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens, bool insertClause, string clause, bool isSimilarityClause) { throw new NotImplementedException(); }
/// <summary> /// Gets the t-sql with the insert similarity clause query. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="similarityClause">The similarity clause.</param> /// <returns>The insert similarity clause query.</returns> private string InsertSimilarityClause(SearchTokens searchTokens, string similarityClause) { return(CreateTsql(searchTokens, SearchConstants.COMMA + similarityClause)); }
/// <summary> /// Gets the t-sql with the insert sort column query. /// </summary> /// <param name="searchTokens">The search tokens.</param> /// <param name="columnName">Name of the column.</param> /// <returns>Insert sort column query.</returns> private string InsertSortColumn(SearchTokens searchTokens, string columnName) { return(CreateTsql(searchTokens, SearchConstants.COMMA + SearchConstants.TSQL_SUB + columnName)); }
/// <summary> /// Appends the where clause of the query. /// </summary> /// <param name="transactionSqlBuilder"><see cref="StringBuilder" /> instance to append T-SQL to.</param> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> public virtual void AppendsWhereClause(StringBuilder transactionSqlBuilder, SearchTokens searchTokens) { Utility.AppendResourceTypesTSql(transactionSqlBuilder, ResourceTypeFullName, searchTokens); }
/// <summary> /// Converts the node to T-SQL. /// </summary> /// <param name="searchTokens"><see cref="SearchTokens" /> instance to fetch tokens with.</param> /// <param name="insertClause">If true, insert the specified clause in TSQL.</param> /// <param name="clause">Sort column name or similarity clause to be inserted.</param> /// <param name="isSimilarityClause">If true, insert the specified similarity clause in /// TSQL else insert sort clause.</param> /// <returns>T-SQL equivalent of the node.</returns> /// <exception cref="ArgumentNullException">Thrown when parameter is null.</exception> public override string ConvertToTSql(SearchTokens searchTokens, bool insertClause, string clause, bool isSimilarityClause) { return(ConvertToTSql(searchTokens)); }