private static ObjectQueryState BuildSelectOrSelectValue(ObjectQueryState query, string alias, string projection, ObjectParameter[] parameters, string projectOp, Type elementType) { Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(alias), "Invalid alias"); Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(projection), "Invalid projection"); string queryText = GetCommandText(query); // Build the new query string - "<project op> <projection> FROM (<this query>) AS <alias>" int queryLength = projectOp.Length + projection.Length + _fromOp.Length + queryText.Length + _asOp.Length + alias.Length; StringBuilder builder = new StringBuilder(queryLength); builder.Append(projectOp); builder.Append(projection); builder.Append(_fromOp); builder.Append(queryText); builder.Append(_asOp); builder.Append(alias); // Create a new EntitySqlQueryImplementation that uses the new query as its command text. // Span should not be carried over from a Select or SelectValue operation. return(NewBuilderQuery(query, elementType, builder, null, MergeParameters(query.ObjectContext, query.Parameters, parameters))); }
internal static ObjectQueryState GroupBy(ObjectQueryState query, string alias, string keys, string projection, ObjectParameter[] parameters) { Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(alias), "Invalid alias"); Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(alias), "Invalid keys"); Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(projection), "Invalid projection"); string queryText = GetCommandText(query); // Build the new query string: // "SELECT <projection> FROM (<this query>) AS <alias> GROUP BY <keys>" int queryLength = _selectOp.Length + projection.Length + _fromOp.Length + queryText.Length + _asOp.Length + alias.Length + _groupByOp.Length + keys.Length; StringBuilder builder = new StringBuilder(queryLength); builder.Append(_selectOp); builder.Append(projection); builder.Append(_fromOp); builder.Append(queryText); builder.Append(_asOp); builder.Append(alias); builder.Append(_groupByOp); builder.Append(keys); // Create a new EntitySqlQueryImplementation that uses the new query as its command text. // Span should not be carried over from a GroupBy operation. return(NewBuilderQuery(query, typeof(DbDataRecord), builder, null, MergeParameters(query.ObjectContext, query.Parameters, parameters))); }
// SetOp helper - note that this doesn't merge Spans, since Except uses the original query's Span // while Intersect/Union/UnionAll use the merged Span. private static ObjectQueryState BuildSetOp(ObjectQueryState leftQuery, ObjectQueryState rightQuery, Span newSpan, string setOp) { // Assert that the arguments aren't null (should have been verified by ObjectQuery) Debug.Assert(leftQuery != null, "Left query is null?"); Debug.Assert(rightQuery != null, "Right query is null?"); Debug.Assert(leftQuery.ElementType.Equals(rightQuery.ElementType), "Incompatible element types in arguments to Except<T>/Intersect<T>/Union<T>/UnionAll<T>?"); // Retrieve the left and right arguments to the set operation - // this will throw if either input query is not an Entity-SQL query. string left = GetCommandText(leftQuery); string right = GetCommandText(rightQuery); // ObjectQuery arguments must be associated with the same ObjectContext instance as the implemented query if (!object.ReferenceEquals(leftQuery.ObjectContext, rightQuery.ObjectContext)) { throw EntityUtil.Argument(System.Data.Entity.Strings.ObjectQuery_QueryBuilder_InvalidQueryArgument, "query"); } // Create a string builder only large enough to contain the new query text int queryLength = _setOpProlog.Length + left.Length + setOp.Length + right.Length + _setOpEpilog.Length; StringBuilder builder = new StringBuilder(queryLength); // Build the new query builder.Append(_setOpProlog); builder.Append(left); builder.Append(setOp); builder.Append(right); builder.Append(_setOpEpilog); // Create a new query implementation and apply the state of this implementation to it. // The Span of the query argument will be merged into the new query's Span by the caller, iff the Set Op is NOT Except. // See the Except, Intersect, Union and UnionAll methods in this class for examples. return(NewBuilderQuery(leftQuery, leftQuery.ElementType, builder, newSpan, MergeParameters(leftQuery.Parameters, rightQuery.Parameters))); }
internal static ObjectQueryState OfType(ObjectQueryState query, EdmType newType, Type clrOfType) { Debug.Assert(newType != null, "OfType cannot be null"); Debug.Assert(Helper.IsEntityType(newType) || Helper.IsComplexType(newType), "OfType must be Entity or Complex type"); string queryText = GetCommandText(query); // Build the new query string - "OFTYPE((<query>), [<type namespace>].[<type name>])" int queryLength = _ofTypeProlog.Length + queryText.Length + _ofTypeInfix.Length + newType.NamespaceName.Length + (newType.NamespaceName != string.Empty ? _ofTypeInfix2.Length : 0) + newType.Name.Length + _ofTypeEpilog.Length; StringBuilder builder = new StringBuilder(queryLength); builder.Append(_ofTypeProlog); builder.Append(queryText); builder.Append(_ofTypeInfix); if (newType.NamespaceName != string.Empty) { builder.Append(newType.NamespaceName); builder.Append(_ofTypeInfix2); } builder.Append(newType.Name); builder.Append(_ofTypeEpilog); // Create a new EntitySqlQueryImplementation that uses the new query as its command text. // Span is carried over, no adjustment is needed return(NewBuilderQuery(query, clrOfType, builder, query.Span, ObjectParameterCollection.DeepCopy(query.Parameters))); }
/// <summary> /// Helper method used to create an ObjectQuery based on an underlying ObjectQueryState instance. /// Although not called directly, this method must be public to be reliably callable from <see cref="CreateQuery()"/> using reflection. /// </summary> /// <typeparam name="TResultType">The required element type of the new ObjectQuery</typeparam> /// <param name="queryState">The underlying ObjectQueryState instance that should back the returned ObjectQuery</param> /// <returns>A new ObjectQuery based on the specified query state, with the specified element type</returns> public static ObjectQuery <TResultType> CreateObjectQuery <TResultType>(ObjectQueryState queryState) { Debug.Assert(queryState != null, "Query state is required"); Debug.Assert(typeof(TResultType) == queryState.ElementType, "Element type mismatch"); return(new ObjectQuery <TResultType>(queryState)); }
internal static ObjectQueryState UnionAll(ObjectQueryState leftQuery, ObjectQueryState rightQuery) { // Ensure the Spans of the query arguments are merged into the new query's Span. Span newSpan = Span.CopyUnion(leftQuery.Span, rightQuery.Span); // Call the SetOp helper. return(BuildSetOp(leftQuery, rightQuery, newSpan, _unionAllOp)); }
/// <summary> /// Sets the values the <see cref="PlanCachingEnabled"/> and <see cref="UserSpecifiedMergeOption"/> properties on /// <paramref name="other"/> to match the values of the corresponding properties on this instance. /// </summary> /// <param name="other">The query state to which this instances settings should be applied.</param> internal void ApplySettingsTo(ObjectQueryState other) { other.PlanCachingEnabled = this.PlanCachingEnabled; other.UserSpecifiedMergeOption = this.UserSpecifiedMergeOption; // _cachedPlan is intentionally not copied over - since the parameters of 'other' would have to be locked as // soon as its execution plan was set, and that may not be appropriate at the time ApplySettingsTo is called. }
/// <summary> /// Helper method to extract the Entity-SQL command text from an <see cref="ObjectQueryState"/> instance if that /// instance models an Entity-SQL-backed ObjectQuery, or to throw an exception indicating that query builder methods /// are not supported on this query. /// </summary> /// <param name="query">The instance from which the Entity-SQL command text should be retrieved</param> /// <returns>The Entity-SQL command text, if the specified query state instance is based on Entity-SQL</returns> /// <exception cref="NotSupportedException"> /// If the specified instance is not based on Entity-SQL command text, and so does not support Entity-SQL query builder methods /// </exception> private static string GetCommandText(ObjectQueryState query) { string commandText = null; if (!query.TryGetCommandText(out commandText)) { throw EntityUtil.NotSupported(System.Data.Entity.Strings.ObjectQuery_QueryBuilder_NotSupportedLinqSource); } return(commandText); }
internal static ObjectQueryState Top(ObjectQueryState query, string alias, string count, ObjectParameter[] parameters) { int queryLength = count.Length; string queryText = GetCommandText(query); bool limitAllowed = ((EntitySqlQueryState)query).AllowsLimitSubclause; if (limitAllowed) { // Build the new query string: // <this query> LIMIT <count> queryLength += (queryText.Length + _limitOp.Length // + count.Length is added above ); } else { // Build the new query string: // "SELECT VALUE TOP(<count>) <alias> FROM (<this query>) AS <alias>" queryLength += (_topOp.Length + // count.Length + is added above _topInfix.Length + alias.Length + _fromOp.Length + queryText.Length + _asOp.Length + alias.Length); } StringBuilder builder = new StringBuilder(queryLength); if (limitAllowed) { builder.Append(queryText); builder.Append(_limitOp); builder.Append(count); } else { builder.Append(_topOp); builder.Append(count); builder.Append(_topInfix); builder.Append(alias); builder.Append(_fromOp); builder.Append(queryText); builder.Append(_asOp); builder.Append(alias); } // Create a new EntitySqlQueryImplementation that uses the new query as its command text. // Span is carried over, no adjustment is needed. return(NewBuilderQuery(query, query.ElementType, builder, query.Span, MergeParameters(query.ObjectContext, query.Parameters, parameters))); }
internal static ObjectQueryState Distinct(ObjectQueryState query) { // Build the new query string - "SET(<this query>)" string queryText = GetCommandText(query); StringBuilder builder = new StringBuilder(_distinctProlog.Length + queryText.Length + _distinctEpilog.Length); builder.Append(_distinctProlog); builder.Append(queryText); builder.Append(_distinctEpilog); // Span is carried over, no adjustment is needed return(NewBuilderQuery(query, query.ElementType, builder, query.Span, ObjectParameterCollection.DeepCopy(query.Parameters))); }
private static ObjectQueryState BuildOrderByOrWhere(ObjectQueryState query, string alias, string predicateOrKeys, ObjectParameter[] parameters, string op, string skipCount, bool allowsLimit) { Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(alias), "Invalid alias"); Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(predicateOrKeys), "Invalid predicate/keys"); Debug.Assert(null == skipCount || op == _orderByOp, "Skip clause used with WHERE operator?"); string queryText = GetCommandText(query); // Build the new query string: // Either: "SELECT VALUE <alias> FROM (<this query>) AS <alias> WHERE <predicate>" // (for Where) // Or: "SELECT VALUE <alias> FROM (<this query>) AS <alias> ORDER BY <keys> <optional: SKIP <skip>>" // Depending on the value of 'op' int queryLength = _selectValueOp.Length + alias.Length + _fromOp.Length + queryText.Length + _asOp.Length + alias.Length + op.Length + predicateOrKeys.Length; if (skipCount != null) { queryLength += (_skipOp.Length + skipCount.Length); } StringBuilder builder = new StringBuilder(queryLength); builder.Append(_selectValueOp); builder.Append(alias); builder.Append(_fromOp); builder.Append(queryText); builder.Append(_asOp); builder.Append(alias); builder.Append(op); builder.Append(predicateOrKeys); if (skipCount != null) { builder.Append(_skipOp); builder.Append(skipCount); } // Create a new EntitySqlQueryImplementation that uses the new query as its command text. // Span is carried over, no adjustment is needed. return(NewBuilderQuery(query, query.ElementType, builder, allowsLimit, query.Span, MergeParameters(query.ObjectContext, query.Parameters, parameters))); }
private static ObjectQueryState NewBuilderQuery(ObjectQueryState sourceQuery, Type elementType, StringBuilder queryText, bool allowsLimit, Span newSpan, IEnumerable <ObjectParameter> enumerableParams) { ObjectParameterCollection queryParams = enumerableParams as ObjectParameterCollection; if (queryParams == null && enumerableParams != null) { queryParams = new ObjectParameterCollection(sourceQuery.ObjectContext.Perspective); foreach (ObjectParameter objectParam in enumerableParams) { queryParams.Add(objectParam); } } EntitySqlQueryState newState = new EntitySqlQueryState(elementType, queryText.ToString(), allowsLimit, sourceQuery.ObjectContext, queryParams, newSpan); sourceQuery.ApplySettingsTo(newState); return(newState); }
internal static ObjectQueryState Where(ObjectQueryState query, string alias, string predicate, ObjectParameter[] parameters) { return(BuildOrderByOrWhere(query, alias, predicate, parameters, _whereOp, null, false)); }
private static ObjectQueryState NewBuilderQuery(ObjectQueryState sourceQuery, Type elementType, StringBuilder queryText, Span newSpan, IEnumerable <ObjectParameter> enumerableParams) { return(NewBuilderQuery(sourceQuery, elementType, queryText, false, newSpan, enumerableParams)); }
internal static ObjectQueryState Skip(ObjectQueryState query, string alias, string keys, string count, ObjectParameter[] parameters) { Debug.Assert(!StringUtil.IsNullOrEmptyOrWhiteSpace(count), "Invalid skip count"); return(BuildOrderByOrWhere(query, alias, keys, parameters, _orderByOp, count, true)); }
internal static ObjectQueryState SelectValue(ObjectQueryState query, string alias, string projection, ObjectParameter[] parameters, Type projectedType) { return(BuildSelectOrSelectValue(query, alias, projection, parameters, _selectValueOp, projectedType)); }
internal static ObjectQueryState Select(ObjectQueryState query, string alias, string projection, ObjectParameter[] parameters) { return(BuildSelectOrSelectValue(query, alias, projection, parameters, _selectOp, typeof(DbDataRecord))); }
internal static ObjectQueryState OrderBy(ObjectQueryState query, string alias, string keys, ObjectParameter[] parameters) { return(BuildOrderByOrWhere(query, alias, keys, parameters, _orderByOp, null, true)); }
internal static ObjectQueryState Except(ObjectQueryState leftQuery, ObjectQueryState rightQuery) { // Call the SetOp helper. // Span is taken from the leftmost query. return(EntitySqlQueryBuilder.BuildSetOp(leftQuery, rightQuery, leftQuery.Span, _exceptOp)); }